📄 configurations.java
字号:
throw new ClassCastException(key
+ " doesn't map to a Float object");
}
}
/**
* Get a int associated with the given configuration key.
*
* @param key the configuration key.
* @return the associated int.
* @exception NoSuchElementException is thrown if the key doesn't
* map to an existing object.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Integer.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public int getInteger(String key) {
Integer i = this.getInteger(key, null);
if (i != null) {
return i.intValue();
} else {
throw new NoSuchElementException(key
+ " doesn't map to an existing object");
}
}
/**
* Get a int associated with the given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated int.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Integer.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public int getInteger(String key, int defaultValue) {
return this.getInteger(key, new Integer(defaultValue)).intValue();
}
/**
* Get a int associated with the given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated int if key is found and has valid format,
* default value otherwise.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Integer.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public Integer getInteger(String key, Integer defaultValue) {
Object value = repository.get(key);
if (value instanceof Integer) {
return (Integer) value;
} else if (value instanceof String) {
Integer i = new Integer((String) value);
repository.put(key, i);
return i;
} else if (value == null) {
if (defaults != null) {
return defaults.getInteger(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException(key
+ " doesn't map to a Integer object");
}
}
/**
* Get the list of the keys contained in the
* configuration repository.
*/
public Enumeration getKeys() {
return this.repository.keys();
}
/**
* Get a list of strings associated with the
* given configuration key.
*
* @param key the configuration key.
* @return the associated list.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Vector.
*/
public Enumeration getList(String key) {
return this.getVector(key, null).elements();
}
/**
* Get a long associated with the given configuration key.
*
* @param key the configuration key.
* @return the associated long.
* @exception NoSuchElementException is thrown if the key doesn't
* map to an existing object.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Long.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public long getLong(String key) {
Long l = this.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 defaul value.
* @return the associated long.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Long.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public long getLong(String key, long defaultValue) {
return this.getLong(key, new Long(defaultValue)).longValue();
}
/**
* Get a long associated with the given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated long if key is found and has valid format,
* default value otherwise.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Long.
* @exception 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 = repository.get(key);
if (value instanceof Long) {
return (Long) value;
} else if (value instanceof String) {
Long l = new Long((String) value);
repository.put(key, l);
return l;
} else if (value == null) {
if (defaults != null) {
return defaults.getLong(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException(key
+ " doesn't map to a Long object");
}
}
/**
* Get a list of properties associated with the given configuration key.
*
* @param key the configuration key.
* @return the associated properties if key is found
* @exception ClassCastException is thrown if the key maps to an
* object that is not a String/Vector
* @exception IllegalArgumentException if one of the tokens is
* malformed (does not contain an equals sign)
*/
public Properties getProperties(String key) {
return getProperties(key, new Properties());
}
/**
* Get a list of properties associated with the given configuration key.
*
* @param key the configuration key.
* @return the associated properties if key is found
* @exception ClassCastException is thrown if the key maps to an
* object that is not a String/Vector
* @exception IllegalArgumentException if one of the tokens is
* malformed (does not contain an equals sign)
*/
public Properties getProperties(String key, Properties defaults) {
// Grab an array of the tokens for this key
String[] tokens = getStringArray(key);
// Each token is of the form 'key=value'.
Properties props = new Properties(defaults);
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i];
int equalSign = token.indexOf('=');
if (equalSign > 0) {
String pkey = token.substring(0, equalSign).trim();
String pvalue = token.substring(equalSign + 1).trim();
props.put(pkey, pvalue);
} else {
throw new IllegalArgumentException("'" + token + "' does not contain an equals sign");
}
}
return props;
}
/**
* Get encapsulated configuration repository.
*/
public Hashtable getRepository() {
return this.repository;
}
/**
* Get a short associated with the given configuration key.
*
* @param key the configuration key.
* @return the associated short.
* @exception NoSuchElementException is thrown if the key doesn't
* map to an existing object.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Short.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public short getShort(String key) {
Short s = this.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 defaul value.
* @return the associated short if key is found and has valid format,
* default value otherwise.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Short.
* @exception 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 = repository.get(key);
if (value instanceof Short) {
return (Short) value;
} else if (value instanceof String) {
Short s = new Short((String) value);
repository.put(key, s);
return s;
} else if (value == null) {
if (defaults != null) {
return defaults.getShort(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException(key
+ " doesn't map to a Short object");
}
}
/**
* Get a short associated with the given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated short.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Short.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public short getShort(String key, short defaultValue) {
return this.getShort(key, new Short(defaultValue)).shortValue();
}
/**
* Get a string associated with the given configuration key.
*
* @param key the configuration key.
* @return the associated string.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a String.
*/
public String getString(String key) {
return this.getString(key, null);
}
/**
* 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.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a String.
*/
public String getString(String key, String defaultValue) {
Object value = repository.get(key);
if (value instanceof String) {
return (String) value;
} else if (value == null) {
if (defaults != null) {
return defaults.getString(key, defaultValue);
} else {
return 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,
* @exception ClassCastException is thrown if the key maps to an
* object that is not a String/Vector
*/
public String[] getStringArray(String key) {
Object value = repository.get(key);
// What's your vector, Victor?
Vector vector;
if (value instanceof String) {
vector = new Vector(1);
vector.addElement(value);
} else if (value instanceof Vector) {
vector = (Vector)value;
} else if (value == null) {
if (defaults != null) {
return defaults.getStringArray(key);
} else {
return new String[0];
}
} else {
throw new ClassCastException(key + " doesn't map to a String/Vector object");
}
String[] tokens = new String[vector.size()];
for (int i = 0; i < tokens.length; i++)
tokens[i] = (String)vector.elementAt(i);
return tokens;
}
/**
* Get a Vector of strings associated with the
* given configuration key.
*
* @param key the configuration key.
* @return the associated Vector.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Vector.
*/
public Vector getVector(String key) {
return this.getVector(key, null);
}
/**
* Get a Vector of strings associated with the
* given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated Vector.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Vector.
*/
public Vector getVector(String key, Vector defaultValue) {
Object value = repository.get(key);
if (value instanceof Vector) {
return (Vector) value;
} else if (value instanceof String) {
Vector v = new Vector(1);
v.addElement((String) value);
repository.put(key, v);
return v;
} else if (value == null) {
if (defaults != null) {
return defaults.getVector(key, defaultValue);
} else {
return ((defaultValue == null)
? new Vector() : defaultValue);
}
} else {
throw new ClassCastException(key
+ " doesn't map to a Vector object");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -