📄 extendedproperties.java
字号:
/**
* Get the list of the keys contained in the configuration
* repository that match the specified prefix.
*
* @param prefix the prefix to match
* @return an Iterator of keys that match the prefix
*/
public Iterator getKeys(String prefix) {
Iterator keys = getKeys();
ArrayList matchingKeys = new ArrayList();
while (keys.hasNext()) {
Object key = keys.next();
if (key instanceof String && ((String) key).startsWith(prefix)) {
matchingKeys.add(key);
}
}
return matchingKeys.iterator();
}
/**
* Create an ExtendedProperties object that is a subset
* of this one. Take into account duplicate keys
* by using the setProperty() in ExtendedProperties.
*
* @param prefix the prefix to get a subset for
* @return a new independent ExtendedProperties
*/
public ExtendedProperties subset(String prefix) {
ExtendedProperties c = new ExtendedProperties();
Iterator keys = getKeys();
boolean validSubset = false;
while (keys.hasNext()) {
Object key = keys.next();
if (key instanceof String && ((String) key).startsWith(prefix)) {
if (!validSubset) {
validSubset = true;
}
/*
* Check to make sure that c.subset(prefix) doesn't
* blow up when there is only a single property
* with the key prefix. This is not a useful
* subset but it is a valid subset.
*/
String newKey = null;
if (((String) key).length() == prefix.length()) {
newKey = prefix;
} else {
newKey = ((String) key).substring(prefix.length() + 1);
}
/*
* use addPropertyDirect() - this will plug the data as
* is into the Map, but will also do the right thing
* re key accounting
*/
c.addPropertyDirect(newKey, get(key));
}
}
if (validSubset) {
return c;
} else {
return null;
}
}
/**
* Display the configuration for debugging purposes to System.out.
*/
public void display() {
Iterator i = getKeys();
while (i.hasNext()) {
String key = (String) i.next();
Object value = get(key);
System.out.println(key + " => " + value);
}
}
/**
* 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.
*/
public String getString(String key) {
return 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.
* @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 = get(key);
if (value instanceof String) {
return interpolate((String) value);
} else if (value == null) {
if (defaults != null) {
return interpolate(defaults.getString(key, defaultValue));
} else {
return interpolate(defaultValue);
}
} else if (value instanceof Vector) {
return interpolate((String) ((Vector) value).get(0));
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a String 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.
* @throws ClassCastException is thrown if the key maps to an
* object that is not a String/Vector.
* @throws 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.
* @throws ClassCastException is thrown if the key maps to an
* object that is not a String/Vector.
* @throws 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 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.
*/
public String[] getStringArray(String key) {
Object value = 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.
* @throws ClassCastException is thrown if the key maps to an
* object that is not a Vector.
*/
public Vector getVector(String key) {
return getVector(key, null);
}
/**
* 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 = get(key);
if (value instanceof Vector) {
return (Vector) value;
} else if (value instanceof String) {
Vector v = new Vector(1);
v.addElement(value);
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");
}
}
/**
* Get a boolean associated with the given configuration key.
*
* @param key The configuration key.
* @return The associated boolean.
* @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 Boolean.
*/
public boolean getBoolean(String key) {
Boolean b = getBoolean(key, null);
if (b != null) {
return b.booleanValue();
} else {
throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object");
}
}
/**
* Get a boolean associated with the given configuration key.
*
* @param key The configuration key.
* @param defaultValue The default value.
* @return The associated boolean.
* @throws ClassCastException is thrown if the key maps to an
* object that is not a Boolean.
*/
public boolean getBoolean(String key, boolean defaultValue) {
return getBoolean(key, new Boolean(defaultValue)).booleanValue();
}
/**
* Get a boolean associated with the given configuration key.
*
* @param key The configuration key.
* @param defaultValue The default value.
* @return The associated boolean 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 Boolean.
*/
public Boolean getBoolean(String key, Boolean defaultValue) {
Object value = get(key);
if (value instanceof Boolean) {
return (Boolean) value;
} else if (value instanceof String) {
String s = testBoolean((String) value);
Boolean b = new Boolean(s);
put(key, b);
return b;
} else if (value == null) {
if (defaults != null) {
return defaults.getBoolean(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a Boolean object");
}
}
/**
* Test whether the string represent by value maps to a boolean
* value or not. We will allow <code>true</code>, <code>on</code>,
* and <code>yes</code> for a <code>true</code> boolean value, and
* <code>false</code>, <code>off</code>, and <code>no</code> for
* <code>false</code> boolean values. Case of value to test for
* boolean status is ignored.
*
* @param value the value to test for boolean state
* @return <code>true</code> or <code>false</code> if the supplied
* text maps to a boolean value, or <code>null</code> otherwise.
*/
public String testBoolean(String value) {
String s = value.toLowerCase();
if (s.equals("true") || s.equals("on") || s.equals("yes")) {
return "true";
} else if (s.equals("false") || s.equals("off") || s.equals("no")) {
return "false";
} else {
return null;
}
}
/**
* Get a byte associated with the given configuration key.
*
* @param key The configuration key.
* @return The associated byte.
* @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 Byte.
* @throws NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
public byte getByte(String key) {
Byte b = getByte(key, null);
if (b != null) {
return b.byteValue();
} else {
throw new NoSuchElementException('\'' + key + " doesn't map to an existing object");
}
}
/**
* Get a byte associated with the given configuration key.
*
* @param key The configuration key.
* @param defaultValue The default value.
* @return The associated byte.
* @throws ClassCastException is thrown if the key maps to an
* object that is not a Byte.
* @throws NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
public byte getByte(String key, byte defaultValue) {
return getByte(key, new Byte(defaultValue)).byteValue();
}
/**
* Get a byte associated with the given configuration key.
*
* @param key The configuration key.
* @param defaultValue The default value.
* @return The associated byte 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 Byte.
* @throws NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
public Byte getByte(String key, Byte defaultValue) {
Object value = get(key);
if (value instanceof Byte) {
return (Byte) value;
} else if (value instanceof String) {
Byte b = new Byte((String) value);
put(key, b);
return b;
} else if (value == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -