123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.scriptplugin;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Properties;
-
- /**
- * Properties file that allows for getting/setting of typed properties.
- */
- public class TypedProperties extends Properties {
-
- /** A version number for this class. */
- private static final long serialVersionUID = 200711071;
- /** Is this properties file Case Sensitive */
- private boolean caseSensitive = true;
-
- /**
- * Creates an empty property list with no default values.
- */
- public TypedProperties() {
- }
-
- /**
- * Creates an empty property list with the specified defaults.
- *
- * @param defaults The Defaults
- */
- public TypedProperties(final Properties defaults) {
- super(defaults);
- }
-
- /**
- * Set case sensitivity of this properties file.
- *
- * @param value True/False for the case sensitivity of this file
- */
- public void setCaseSensitivity(final boolean value) {
- // Set all existing values to lowercase.
- if (!value) {
- for (final Object property : keySet()) {
- if (property instanceof String) {
- final String propertyName = (String) property;
- if (!propertyName.equals(propertyName.toLowerCase())) {
- super.setProperty(propertyName.toLowerCase(), getProperty(propertyName));
- super.remove(propertyName);
- }
- }
- }
- }
- caseSensitive = value;
- }
-
- /**
- * Load properties from an InputStream. After loading, setCaseSensitivity(caseSensitive) is
- * called. If this properties file is meant to be case Insensitive, all non-lowercase property
- * names will be lowercased.
- *
- * @param inStream InputStream to load from.
- *
- * @throws IOException If there is a problem reading from the Input Stream
- */
- @Override
- public synchronized void load(final InputStream inStream) throws IOException {
- super.load(inStream);
- setCaseSensitivity(caseSensitive);
- }
-
- /**
- * Get a property from the config
- *
- * @param key key for property
- *
- * @return the requested property, or null if not defined
- */
- @Override
- public String getProperty(final String key) {
- if (caseSensitive) {
- return super.getProperty(key);
- } else {
- return super.getProperty(key.toLowerCase());
- }
- }
-
- /**
- * Get a property from the config
- *
- * @param key key for property
- * @param fallback Value to return if key is not found
- *
- * @return the requested property, or the fallback value if not defined
- */
- @Override
- public String getProperty(final String key, final String fallback) {
- if (caseSensitive) {
- return super.getProperty(key, fallback);
- } else {
- return super.getProperty(key.toLowerCase(), fallback);
- }
- }
-
- /**
- * Set a property in the config
- *
- * @param key key for property
- * @param value Value for property
- *
- * @return Old value of property
- */
- @Override
- public synchronized Object setProperty(final String key, final String value) {
- if (!caseSensitive) {
- return super.setProperty(key.toLowerCase(), value);
- } else {
- return super.setProperty(key, value);
- }
- }
-
- /**
- * Check if a property exists
- *
- * @param key key for property
- *
- * @return True if the property exists, else false
- */
- public boolean hasProperty(final String key) {
- return getProperty(key) != null;
- }
-
- /**
- * Get a Byte property from the config
- *
- * @param key key for property
- * @param fallback Value to return if key is not found
- *
- * @return the requested property, or the fallback value if not defined
- */
- public byte getByteProperty(final String key, final byte fallback) {
- try {
- return Byte.parseByte(getProperty(key, Byte.toString(fallback)));
- } catch (final NumberFormatException nfe) {
- return fallback;
- }
- }
-
- /**
- * Set a Byte property in the config
- *
- * @param key key for property
- * @param value Value for property
- */
- public void setByteProperty(final String key, final byte value) {
- setProperty(key, Byte.toString(value));
- }
-
- /**
- * Get a Short property from the config
- *
- * @param key key for property
- * @param fallback Value to return if key is not found
- *
- * @return the requested property, or the fallback value if not defined
- */
- public short getShortProperty(final String key, final short fallback) {
- try {
- return Short.parseShort(getProperty(key, Short.toString(fallback)));
- } catch (final NumberFormatException nfe) {
- return fallback;
- }
- }
-
- /**
- * Set a Short property in the config
- *
- * @param key key for property
- * @param value Value for property
- */
- public void setShortProperty(final String key, final short value) {
- setProperty(key, Short.toString(value));
- }
-
- /**
- * Get an integer property from the config
- *
- * @param key key for property
- * @param fallback Value to return if key is not found
- *
- * @return the requested property, or the fallback value if not defined
- */
- public int getIntProperty(final String key, final int fallback) {
- try {
- return Integer.parseInt(getProperty(key, Integer.toString(fallback)));
- } catch (final NumberFormatException nfe) {
- return fallback;
- }
- }
-
- /**
- * Set an integer property in the config
- *
- * @param key key for property
- * @param value Value for property
- */
- public void setIntProperty(final String key, final int value) {
- setProperty(key, Integer.toString(value));
- }
-
- /**
- * Get a Long property from the config
- *
- * @param key key for property
- * @param fallback Value to return if key is not found
- *
- * @return the requested property, or the fallback value if not defined
- */
- public long getLongProperty(final String key, final long fallback) {
- try {
- return Long.parseLong(getProperty(key, Long.toString(fallback)));
- } catch (final NumberFormatException nfe) {
- return fallback;
- }
- }
-
- /**
- * Set a Long property in the config
- *
- * @param key key for property
- * @param value Value for property
- */
- public void setLongProperty(final String key, final long value) {
- setProperty(key, Long.toString(value));
- }
-
- /**
- * Get a float property from the config
- *
- * @param key key for property
- * @param fallback Value to return if key is not found
- *
- * @return the requested property, or the fallback value if not defined
- */
- public float getFloatProperty(final String key, final float fallback) {
- try {
- return Float.parseFloat(getProperty(key, Float.toString(fallback)));
- } catch (final NumberFormatException nfe) {
- return fallback;
- }
- }
-
- /**
- * Set a float property in the config
- *
- * @param key key for property
- * @param value Value for property
- */
- public void setFloatProperty(final String key, final float value) {
- setProperty(key, Float.toString(value));
- }
-
- /**
- * Get a double property from the config
- *
- * @param key key for property
- * @param fallback Value to return if key is not found
- *
- * @return the requested property, or the fallback value if not defined
- */
- public double getDoubleProperty(final String key, final double fallback) {
- try {
- return Double.parseDouble(getProperty(key, Double.toString(fallback)));
- } catch (final NumberFormatException nfe) {
- return fallback;
- }
- }
-
- /**
- * Set a double property in the config
- *
- * @param key key for property
- * @param value Value for property
- */
- public void setDoubleProperty(final String key, final double value) {
- setProperty(key, Double.toString(value));
- }
-
- /**
- * Get a boolean property from the config
- *
- * @param key key for property
- * @param fallback Value to return if key is not found
- *
- * @return the requested property, or the fallback value if not defined
- */
- public boolean getBoolProperty(final String key, final boolean fallback) {
- return Boolean.parseBoolean(getProperty(key, Boolean.toString(fallback)));
- }
-
- /**
- * Set a Boolean property in the config
- *
- * @param key key for property
- * @param value Value for property
- */
- public void setBoolProperty(final String key, final boolean value) {
- setProperty(key, Boolean.toString(value));
- }
-
- /**
- * Get a Char property from the config
- *
- * @param key key for property
- * @param fallback Value to return if key is not found
- *
- * @return the requested property, or the fallback value if not defined
- */
- public char getCharProperty(final String key, final char fallback) {
- final String res = getProperty(key, Character.toString(fallback));
- if (res == null || res.isEmpty()) {
- return fallback;
- } else {
- return res.charAt(0);
- }
- }
-
- /**
- * Set a Char property in the config
- *
- * @param key key for property
- * @param value Value for property
- */
- public void setCharProperty(final String key, final char value) {
- setProperty(key, Character.toString(value));
- }
-
- /**
- * Get a List property from the config.
- *
- * @param key key for property
- * @param fallback List to return if key is not found
- *
- * @return the requested property, or the fallback value if not defined
- */
- public List<String> getListProperty(final String key, final List<String> fallback) {
- final String res = getProperty(key, "");
- if (res == null || res.isEmpty()) {
- return fallback;
- } else {
- final String[] bits = res.split("\n");
- final List<String> result = new ArrayList<>();
- Collections.addAll(result, bits);
- return result;
- }
- }
-
- /**
- * Set a List property in the config
- *
- * @param key key for property
- * @param value Value for property
- */
- public void setListProperty(final String key, final Iterable<String> value) {
- final StringBuilder val = new StringBuilder();
- final String lf = "\n";
- boolean first = true;
- for (final String bit : value) {
- if (first) {
- first = false;
- } else {
- val.append(lf);
- }
- val.append(bit);
- }
- setProperty(key, val.toString());
- }
-
- }
|