📄 abstractconfiguration.java
字号:
{ Long l = getLong(key, null); if (l != null) { return l.longValue(); } else { throw new NoSuchElementException( '\'' + key + "' doesn't map to an existing object"); } } /** * Get a long associated with the given configuration key. * * @param key The configuration key. * @param defaultValue The default value. * * @return The associated long. * * @throws ClassCastException is thrown if the key maps to an * object that is not a Long. * @throws NumberFormatException is thrown if the value mapped * by the key has not a valid number format. */ public long getLong(String key, long defaultValue) { return getLong(key, new Long(defaultValue)).longValue(); } /** * Get a long associated with the given configuration key. * * @param key The configuration key. * @param defaultValue The default value. * * @return The associated long if key is found and has valid * format, default value otherwise. * * @throws ClassCastException is thrown if the key maps to an * object that is not a Long. * @throws NumberFormatException is thrown if the value mapped * by the key has not a valid number format. */ public Long getLong(String key, Long defaultValue) { Object value = resolveContainerStore(key); if (value instanceof Long) { return (Long) value; } else if (value instanceof String) { Long l = new Long((String) value); return l; } else if (value == null) { if (defaults != null) { return defaults.getLong(key, defaultValue); } else { log.warn("Use Long default value for key '" + key + "' (" + defaultValue + ")"); return defaultValue; } } else { throw new ClassCastException( '\'' + key + "' doesn't map to a Long object"); } } /** * Get a short associated with the given configuration key. * * @param key The configuration key. * * @return The associated short. * * @throws NoSuchElementException is thrown if the key doesn't * map to an existing object. * @throws ClassCastException is thrown if the key maps to an * object that is not a Short. * @throws NumberFormatException is thrown if the value mapped * by the key has not a valid number format. */ public short getShort(String key) { Short s = getShort(key, null); if (s != null) { return s.shortValue(); } else { throw new NoSuchElementException( '\'' + key + "' doesn't map to an existing object"); } } /** * Get a short associated with the given configuration key. * * @param key The configuration key. * @param defaultValue The default value. * * @return The associated short. * * @throws ClassCastException is thrown if the key maps to an * object that is not a Short. * @throws NumberFormatException is thrown if the value mapped * by the key has not a valid number format. */ public short getShort(String key, short defaultValue) { return getShort(key, new Short(defaultValue)).shortValue(); } /** * Get a short associated with the given configuration key. * * @param key The configuration key. * @param defaultValue The default value. * * @return The associated short if key is found and has valid * format, default value otherwise. * * @throws ClassCastException is thrown if the key maps to an * object that is not a Short. * @throws NumberFormatException is thrown if the value mapped * by the key has not a valid number format. */ public Short getShort(String key, Short defaultValue) { Object value = resolveContainerStore(key); if (value instanceof Short) { return (Short) value; } else if (value instanceof String) { Short s = new Short((String) value); return s; } else if (value == null) { if (defaults != null) { return defaults.getShort(key, defaultValue); } else { log.warn("Use Short default value for key '" + key + "' (" + defaultValue + ")"); return defaultValue; } } else { throw new ClassCastException( '\'' + key + "' doesn't map to a Short object"); } } /** * Get a string associated with the given configuration key. * * @param key The configuration key. * * @return The associated string. * * @throws ClassCastException is thrown if the key maps to an object that * is not a String. * @throws NoSuchElementException is thrown if the key doesn't * map to an existing object. */ public String getString(String key) { String s = getString(key, null); if (s != null) { return s; } else { throw new NoSuchElementException( '\'' + key + "' doesn't map to an existing object"); } } /** * Get a string associated with the given configuration key. * * @param key The configuration key. * @param defaultValue The default value. * * @return The associated string if key is found, default value otherwise. * * @throws ClassCastException is thrown if the key maps to an object that * is not a String. */ public String getString(String key, String defaultValue) { Object value = resolveContainerStore(key); if (value instanceof String) { return interpolate((String) value); } else if (value == null) { if (defaults != null) { return interpolate(defaults.getString(key, defaultValue)); } else { log.warn("Use String default value for key '" + key + "' (" + defaultValue + ")"); return interpolate(defaultValue); } } else { throw new ClassCastException( '\'' + key + "' doesn't map to a String object"); } } /** * Get an array of strings associated with the given configuration * key. * * @param key The configuration key. * * @return The associated string array if key is found. * * @throws ClassCastException is thrown if the key maps to an * object that is not a String/Vector of Strings. */ public String[] getStringArray(String key) { Object value = getPropertyDirect(key); String[] tokens; if (value instanceof String) { tokens = new String[1]; tokens[0] = interpolate((String) value); } else if (value instanceof Container) { tokens = new String[((Container) value).size()]; for (int i = 0; i < tokens.length; i++) { tokens[i] = interpolate((String) ((Container) value).get(i)); } } else if (value == null) { if (defaults != null) { tokens = defaults.getStringArray(key); } else { tokens = new String[0]; } } else { throw new ClassCastException( '\'' + key + "' doesn't map to a String/Vector object"); } return tokens; } /** * Get a Vector of strings associated with the given configuration key. * * @param key The configuration key. * * @return The associated Vector. * * @throws ClassCastException is thrown if the key maps to an * object that is not a Vector. * @throws NoSuchElementException is thrown if the key doesn't * map to an existing object. */ public Vector getVector(String key) { Vector v = getVector(key, null); if (v != null) { return v; } else { throw new NoSuchElementException( '\'' + key + "' doesn't map to an existing object"); } } /** * Get a Vector of strings associated with the given configuration key. * * @param key The configuration key. * @param defaultValue The default value. * * @return The associated Vector. * * @throws ClassCastException is thrown if the key maps to an * object that is not a Vector. */ public Vector getVector(String key, Vector defaultValue) { Object value = getPropertyDirect(key); Vector v = null; if (value instanceof String) { v = new Vector(1); v.addElement(value); } else if (value instanceof Container) { v = ((Container) value).asVector(); } else if (value == null) { if (defaults != null) { v = defaults.getVector(key, defaultValue); } else { v = ((defaultValue == null) ? new Vector() : defaultValue); } } else { throw new ClassCastException( '\'' + key + "' doesn't map to a Vector object: " + value + ", a " + value.getClass().getName()); } return v; } /** * Returns an object from the store described by the key. * If the value is a Container object, replace it with the * first object in the container * * @param key The property key. * * @return value Value, transparently resolving a possible * Container dependency. */ private Object resolveContainerStore(String key) { Object value = getPropertyDirect(key); if (value != null && value instanceof Container) { value = ((Container) value).get(0); } return value; } /** * This class divides into tokens a property value. Token * separator is "," but commas into the property value are escaped * using the backslash in front. */ class PropertiesTokenizer extends StringTokenizer { /** The property delimiter used while parsing (a comma). */ static final String DELIMITER = ","; /** * Constructor. * * @param string A String. */ public PropertiesTokenizer(String string) { super(string, DELIMITER); } /** * Check whether the object has more tokens. * * @return True if the object has more tokens. */ public boolean hasMoreTokens() { return super.hasMoreTokens(); } /** * Get next token. * * @return A String. */ public String nextToken() { StringBuffer buffer = new StringBuffer(); while (hasMoreTokens()) { String token = super.nextToken(); if (token.endsWith("\\")) { buffer.append(token.substring(0, token.length() - 1)); buffer.append(DELIMITER); } else { buffer.append(token); break; } } return buffer.toString().trim(); } } // class PropertiesTokenizer /** * Private Wrapper class for Vector, so we can distinguish between * Vector objects and our container */ static class Container { /** We're wrapping a List object (A vector) */ private List l = null; /** * C'tor */ public Container() { l = new Vector(INITIAL_LIST_SIZE); } /** * Add an Object to the Container * * @param o The Object */ public void add(Object o) { l.add(o); } /** * Returns the current size of the Container * * @return The Number of elements in the container */ public int size() { return l.size(); } /** * Returns the Element at an index * * @param index The Index * @return The element at that index */ public Object get(int index) { return l.get(index); } /** * Returns an Iterator over the container objects * * @return An Iterator */ public Iterator iterator() { return l.iterator(); } /** * Returns the Elements of the Container as * a Vector. This is not the internal vector * element but a shallow copy of the internal * list. You may modify the returned list without * modifying the container. * * @return A Vector containing the elements of the Container. */ public Vector asVector() { Vector v = new Vector(l.size()); for (Iterator it = l.iterator(); it.hasNext();) { v.add(it.next()); } return v; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -