📄 configurations.java
字号:
package source.org.apache.java.util;
/*
* Copyright (c) 1997-1999 The Java Apache Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Java Apache
* Project for use in the Apache JServ servlet engine project
* <http://java.apache.org/>."
*
* 4. The names "Apache JServ", "Apache JServ Servlet Engine" and
* "Java Apache Project" must not be used to endorse or promote products
* derived from this software without prior written permission.
*
* 5. Products derived from this software may not be called "Apache JServ"
* nor may "Apache" nor "Apache JServ" appear in their names without
* prior written permission of the Java Apache Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Java Apache
* Project for use in the Apache JServ servlet engine project
* <http://java.apache.org/>."
*
* THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Java Apache Group. For more information
* on the Java Apache Project and the Apache JServ Servlet Engine project,
* please see <http://java.apache.org/>.
*
*/
import java.util.*;
import java.io.*;
/**
* This class is used to encapsulate properties and addresses
* the need for a flexible, portable and fast configurations
* repository.
*
* <p>While properties are just considered as strings, configurations
* have methods to return different types such as <b>int</b> and
* <b>long</b>.
*
* <p>Performance is needed to avoid the use of properties only at
* startup to fill variables: configurations encapsulate properties
* in the sense that objects retrieved by parsing property strings
* are stored for faster reuse. This allows a program to use
* configurations instead of global variables in a central repository,
* that, if updated, will reflect instantly the changes throughout
* the whole application.
*
* <p>The behavior of this class is syntax indipendent because
* it's only an encapsulator.
* This allows greater flexibility throught the use of syntax abstraction
* and faster transition between different configuration syntax.
*
* @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
* @version $Revision: 1.2 $ $Date: 2000/08/08 14:01:44 $
*/
public class Configurations {
/**
* Default configurations repository.
*/
private Configurations defaults;
/**
* Configuration repository.
*/
private ConfigurationsRepository repository;
/**
* Creates an empty configuration repository
* with no default values.
*/
public Configurations() {
this(null, null);
}
/**
* Creates an empty configuration repository with
* the specified defaults.
*
* @param defaults the default values repository.
*/
public Configurations(Configurations defaults) {
this(null, defaults);
}
/**
* Creates a configuration repository encapsulating
* the given properties with no default values.
*
* @param properties the properties to encapsulate.
*/
public Configurations(ConfigurationsRepository properties) {
this(properties, null);
}
/**
* Merge the given properties object as configurations.
*
* @param properties the properties file to merge
*/
public Configurations(ConfigurationsRepository properties,
Configurations defaults) {
this.repository = properties;
this.defaults = defaults;
}
/**
* Get a boolean associated with the given configuration key.
*
* @param key the configuration key.
* @return the associated boolean.
* @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 Boolean.
*/
public boolean getBoolean(String key) {
Boolean b = this.getBoolean(key, (Boolean) 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 defaul value.
* @return the associated boolean 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 Boolean.
*/
public Boolean getBoolean(String key, Boolean defaultValue) {
Object value = repository.get(key);
if (value instanceof Boolean) {
return (Boolean) value;
} else if (value instanceof String) {
Boolean b = new Boolean((String) value);
repository.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");
}
}
/**
* Get a boolean associated with the given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated boolean.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Boolean.
*/
public boolean getBoolean(String key, boolean defaultValue) {
return this.getBoolean(key, new Boolean(defaultValue)).booleanValue();
}
/**
* Get a byte associated with the given configuration key.
*
* @param key the configuration key.
* @return the associated byte.
* @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 Byte.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public byte getByte(String key) {
Byte b = this.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 defaul value.
* @return the associated byte.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Byte.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public byte getByte(String key, byte defaultValue) {
return this.getByte(key, new Byte(defaultValue)).byteValue();
}
/**
* Get a byte associated with the given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated byte 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 Byte.
* @exception 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 = repository.get(key);
if (value instanceof Byte) {
return (Byte) value;
} else if (value instanceof String) {
Byte b = new Byte((String) value);
repository.put(key, b);
return b;
} else if (value == null) {
if (defaults != null) {
return defaults.getByte(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException(key
+ " doesn't map to a Byte object");
}
}
/**
* Get a double associated with the given configuration key.
*
* @param key the configuration key.
* @return the associated double.
* @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 Double.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public double getDouble(String key) {
Double d = this.getDouble(key, null);
if (d != null) {
return d.doubleValue();
} else {
throw new NoSuchElementException(key
+ " doesn't map to an existing object");
}
}
/**
* Get a double associated with the given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated double.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Double.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public double getDouble(String key, double defaultValue) {
return this.getDouble(key, new Double(defaultValue)).doubleValue();
}
/**
* Get a double associated with the given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated double 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 Double.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public Double getDouble(String key, Double defaultValue) {
Object value = repository.get(key);
if (value instanceof Double) {
return (Double) value;
} else if (value instanceof String) {
Double d = new Double((String) value);
repository.put(key, d);
return d;
} else if (value == null) {
if (defaults != null) {
return defaults.getDouble(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException(key
+ " doesn't map to a Double object");
}
}
/**
* Get a float associated with the given configuration key.
*
* @param key the configuration key.
* @return the associated float.
* @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 Float.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public float getFloat(String key) {
Float f = this.getFloat(key, null);
if (f != null) {
return f.floatValue();
} else {
throw new NoSuchElementException(key
+ " doesn't map to an existing object");
}
}
/**
* Get a float associated with the given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated float.
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Float.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public float getFloat(String key, float defaultValue) {
return this.getFloat(key, new Float(defaultValue)).floatValue();
}
/**
* Get a float associated with the given configuration key.
*
* @param key the configuration key.
* @param defaultValue the defaul value.
* @return the associated float 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 Float.
* @exception NumberFormatException is thrown if the value
* mapped by the key has not a valid number format.
*/
public Float getFloat(String key, Float defaultValue) {
Object value = repository.get(key);
if (value instanceof Float) {
return (Float) value;
} else if (value instanceof String) {
Float f = new Float((String) value);
repository.put(key, f);
return f;
} else if (value == null) {
if (defaults != null) {
return defaults.getFloat(key, defaultValue);
} else {
return defaultValue;
}
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -