cvs commit: spice/components/salt/src/java/org/realityforge/salt/config DefaultParameters.java Freezable.java ParameterException.java Parameters.java ParametersUtil.java
Peter Donald <[email protected]>
| Newsgroups | gmane.comp.java.spice.cvs |
|---|---|
| Message-ID | <[email protected]> |
donaldp 03/10/29 14:38:59
Added: components/salt/src/java/org/realityforge/salt/config
DefaultParameters.java Freezable.java
ParameterException.java Parameters.java
ParametersUtil.java
Log:
migrate parameters object and supporting infrastructure from the DNA project
Revision Changes Path
1.1 spice/components/salt/src/java/org/realityforge/salt/config/DefaultParameters.java
Index: DefaultParameters.java
===================================================================
/*
* Copyright (C) The Spice Group. All rights reserved.
*
* This software is published under the terms of the Spice
* Software License version 1.1, a copy of which has been included
* with this distribution in the LICENSE.txt file.
*/
package org.realityforge.salt.config;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
/**
* Parameters implementation backed by a Properties object.
* The developer should create the DefaultParameters,
* associate parameters and then invoke {@link #makeReadOnly()}
* before passing the Parameters to the client component.
*
* @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2003/10/29 22:38:59 $
*/
public class DefaultParameters
implements Parameters
{
/**
* Constant for separator between parameters
* and child parameters.
*/
private static final String SEPARATOR = ".";
/**
* Constant for empty prefix.
*/
private static final String EMPTY_PREFIX = "";
/**
* Flag indicating whether resource has been
* made read-only yet.
*/
private boolean m_readOnly;
/**
* The key-value pairs contained by parameters object.
*/
private final Properties m_parameters = new Properties();
/**
* The child parameters objects created from with
* parameters object.
*/
private final Set m_children = new HashSet();
/**
* The prefix associated with parameters object.
*/
private final String m_prefix;
/**
* Create a parameters object with empty prefix.
*/
public DefaultParameters()
{
this( EMPTY_PREFIX );
}
/**
* Create a parameters object with specified prefix.
*
* @param prefix the prefix
*/
public DefaultParameters( final String prefix )
{
if( null == prefix )
{
throw new NullPointerException( "prefix" );
}
m_prefix = prefix;
}
/**
* Return the names of all the parameters.
*
* @return the names of all the parameters.
*/
public String[] getParameterNames()
{
final Set set = getParameters().keySet();
return (String[])set.toArray( new String[ set.size() ] );
}
/**
* Return true of parameter with specified name exists.
*
* @param name the name
* @return true of parameter with specified name exists.
*/
public boolean isParameter( final String name )
{
if( null == name )
{
throw new NullPointerException( "name" );
}
return getParameters().containsKey( name );
}
/**
* Return value of parameter with specified name.
*
* @param name the name
* @return the value
* @throws org.realityforge.salt.config.ParameterException if unable to locate parameter
*/
public String getParameter( final String name )
throws ParameterException
{
if( null == name )
{
throw new NullPointerException( "name" );
}
final String property = getParameters().getProperty( name );
if( null == property )
{
final String message =
"Unable to locate parameter named " + name;
throw new ParameterException( message, name );
}
return property;
}
/**
* Return value of parameter with specified name.
*
* @param name the name
* @param defaultValue the defaultValue if specified parameter
* does not exist
* @return the value
*/
public String getParameter( final String name,
final String defaultValue )
{
if( null == name )
{
throw new NullPointerException( "name" );
}
return getParameters().getProperty( name, defaultValue );
}
/**
* Return value of parameter with specified name as a boolean.
*
* @param name the name
* @return the value
* @throws org.realityforge.salt.config.ParameterException if unable to locate parameter
* or parameter can not be converted to correct type
*/
public boolean getParameterAsBoolean( final String name )
throws ParameterException
{
return getParameter( name ).equals( "true" );
}
/**
* Return value of parameter with specified name as a boolean.
*
* @param name the name
* @param defaultValue the defaultValue if specified parameter
* does not exist or parameter can not be converted to
* the correct type
* @return the value
*/
public boolean getParameterAsBoolean( final String name,
final boolean defaultValue )
{
final String value = getParameter( name, null );
if( null == value )
{
return defaultValue;
}
else
{
return value.equals( "true" );
}
}
/**
* Return value of parameter with specified name as an integer.
*
* @param name the name
* @return the value
* @throws org.realityforge.salt.config.ParameterException if unable to locate parameter
* or parameter can not be converted to correct type
*/
public int getParameterAsInteger( final String name )
throws ParameterException
{
final String value = getParameter( name );
try
{
return Integer.parseInt( value );
}
catch( final NumberFormatException nfe )
{
final String prefixedName = prefixedName( name );
final String message =
"Unable to parse parameter named " + prefixedName +
" with value '" + value + "'";
throw new ParameterException( message, prefixedName, nfe );
}
}
/**
* Return value of parameter with specified name as an integer.
*
* @param name the name
* @param defaultValue the defaultValue if specified parameter
* does not exist or parameter can not be converted to
* the correct type
* @return the value
*/
public int getParameterAsInteger( final String name,
final int defaultValue )
{
final String value = getParameter( name, null );
if( null == value )
{
return defaultValue;
}
else
{
try
{
return Integer.parseInt( value );
}
catch( final NumberFormatException nfe )
{
return defaultValue;
}
}
}
/**
* Return value of parameter with specified name as a long.
*
* @param name the name
* @return the value
* @throws org.realityforge.salt.config.ParameterException if unable to locate parameter
* or parameter can not be converted to correct type
*/
public long getParameterAsLong( final String name )
throws ParameterException
{
final String value = getParameter( name );
try
{
return Long.parseLong( value );
}
catch( final NumberFormatException nfe )
{
final String prefixedName = prefixedName( name );
final String message =
"Unable to parse parameter named " + prefixedName +
" with value '" + value + "'";
throw new ParameterException( message, prefixedName, nfe );
}
}
/**
* Return value of parameter with specified name as a long.
*
* @param name the name
* @param defaultValue the defaultValue if specified parameter
* does not exist or parameter can not be converted to
* the correct type
* @return the value
*/
public long getParameterAsLong( final String name,
final long defaultValue )
{
final String value = getParameter( name, null );
if( null == value )
{
return defaultValue;
}
else
{
try
{
return Long.parseLong( value );
}
catch( final NumberFormatException nfe )
{
return defaultValue;
}
}
}
/**
* Return value of parameter with specified name as a float.
*
* @param name the name
* @return the value
* @throws org.realityforge.salt.config.ParameterException if unable to locate parameter
* or parameter can not be converted to correct type
*/
public float getParameterAsFloat( final String name )
throws ParameterException
{
final String value = getParameter( name );
try
{
return Float.parseFloat( value );
}
catch( final NumberFormatException nfe )
{
final String prefixedName = prefixedName( name );
final String message =
"Unable to parse parameter named " + name +
" with value '" + value + "'";
throw new ParameterException( message, prefixedName, nfe );
}
}
/**
* Return value of parameter with specified name as a float.
*
* @param name the name
* @param defaultValue the defaultValue if specified parameter
* does not exist or parameter can not be converted to
* the correct type
* @return the value
*/
public float getParameterAsFloat( final String name,
final float defaultValue )
{
final String value = getParameter( name, null );
if( null == value )
{
return defaultValue;
}
else
{
try
{
return Float.parseFloat( value );
}
catch( final NumberFormatException nfe )
{
return defaultValue;
}
}
}
/**
* Return a Parameters object that represents a
* subset of parameters with specified prefix. The child
* parameters has a prefix with the separator ('.') appended.
* ie. if the prefix was "foo" then the parameter
* "foo.baz" would be included in child Parameters object
* using the key "baz".
*
* @param prefix the prefix
* @return the parameters object
*/
public Parameters getChildParameters( final String prefix )
{
if( null == prefix )
{
throw new NullPointerException( "prefix" );
}
final String prefixAndSeparator = prefix + SEPARATOR;
final int length = prefix.length() + 1;
final String child = prefixedName( prefix );
final DefaultParameters parameters = new DefaultParameters( child );
final Iterator iterator = getParameters().keySet().iterator();
while( iterator.hasNext() )
{
final String key = (String)iterator.next();
if( key.startsWith( prefixAndSeparator ) )
{
final String value = getParameter( key, null );
final String newKey = key.substring( length );
parameters.setParameter( newKey, value );
}
}
parameters.makeReadOnly();
getChildren().add( parameters );
return parameters;
}
/**
* Return name that may be prefixed with full property
* name unless prefix is empty.
*
* @param name the name
* @return the name with prefix decorated
*/
private String prefixedName( final String name )
{
if( getPrefix().equals( EMPTY_PREFIX ) )
{
return name;
}
else
{
return getPrefix() + SEPARATOR + name;
}
}
/**
* Mark the resource and all child parameter
* objects as read only.
*/
public void makeReadOnly()
{
m_readOnly = true;
final Iterator iterator = getChildren().iterator();
while( iterator.hasNext() )
{
final Object child = iterator.next();
if( child instanceof Freezable )
{
( (Freezable)child ).makeReadOnly();
}
}
}
/**
* Set parameter with specified name to specified value.
*
* @param name the parameter name
* @param value the parameter value
*/
public void setParameter( final String name,
final String value )
{
if( null == name )
{
throw new NullPointerException( "name" );
}
if( null == value )
{
throw new NullPointerException( "value" );
}
checkWriteable();
getParameters().setProperty( name, value );
}
/**
* Return the backing properties object associated with parameters.
*
* @return the backing properties object associated with parameters.
*/
protected final Properties getParameters()
{
return m_parameters;
}
/**
* Return the prefix associated with Parameters object.
*
* @return the prefix associated with Parameters object.
*/
protected final String getPrefix()
{
return m_prefix;
}
/**
* Return the set of child parameter objects.
*
* @return the set of child parameter objects
*/
protected final Set getChildren()
{
return m_children;
}
/**
* Check if the resource has been "frozen"
* and thus is read only. If read-only then
* throw an IllegalStateException.
*
* @throws java.lang.IllegalStateException if resource is read-only
*/
protected final void checkWriteable()
{
if( m_readOnly )
{
final String message =
"Resource (" + this + ") is read only and can not be modified";
throw new IllegalStateException( message );
}
}
/**
* Return true if resource has been made read-only or frozen.
*
* @return true if resource has been made read-only or
* frozen, false otherwise.
*/
protected final boolean isReadOnly()
{
return m_readOnly;
}
}
1.1 spice/components/salt/src/java/org/realityforge/salt/config/Freezable.java
Index: Freezable.java
===================================================================
/*
* Copyright (C) The Spice Group. All rights reserved.
*
* This software is published under the terms of the Spice
* Software License version 1.1, a copy of which has been included
* with this distribution in the LICENSE.txt file.
*/
package org.realityforge.salt.config;
/**
* This interface is used internally to salt config
* implementation to indicate which classes can be "frozen"
* and be made read-only after being mutable.
*
* @version $Revision: 1.1 $ $Date: 2003/10/29 22:38:59 $
*/
public interface Freezable
{
/**
* Make resource read-only.
*/
void makeReadOnly();
}
1.1 spice/components/salt/src/java/org/realityforge/salt/config/ParameterException.java
Index: ParameterException.java
===================================================================
/*
* Copyright (C) The JContainer Group. All rights reserved.
*
* This software is published under the terms of the JContainer
* Software License version 1.1, a copy of which has been included
* with this distribution in the LICENSE.txt file.
*/
package org.realityforge.salt.config;
/**
* The ParameterException is used to signal a problem
* retrieving a parameter from the Parameters object.
*
* @version $Revision: 1.1 $ $Date: 2003/10/29 22:38:59 $
*/
public class ParameterException
extends Exception
{
/**
* The exception that caused this exception if any.
*/
private final Throwable m_cause;
/**
* The parameter key that caused the problem.
*/
private final String m_key;
/**
* Create a ParameterException with specified
* message and key.
*
* @param message the message
* @param key the key
*/
public ParameterException( final String message,
final String key )
{
this( message, key, null );
}
/**
* Create a ParameterException with specified
* message, key and cause.
*
* @param message the message
* @param key the key
* @param cause the cause
*/
public ParameterException( final String message,
final String key,
final Throwable cause )
{
super( message );
m_key = key;
m_cause = cause;
}
/**
* Return the parameter key that caused the problem.
*
* @return the parameter key that caused the problem.
*/
public String getKey()
{
return m_key;
}
/**
* Return the exception that caused this exception if any.
*
* @return the exception that caused this exception if any.
*/
public Throwable getCause()
{
return m_cause;
}
}
1.1 spice/components/salt/src/java/org/realityforge/salt/config/Parameters.java
Index: Parameters.java
===================================================================
/*
* Copyright (C) The Spice Group. All rights reserved.
*
* This software is published under the terms of the Spice
* Software License version 1.1, a copy of which has been included
* with this distribution in the LICENSE.txt file.
*/
package org.realityforge.salt.config;
/**
* Parameters present flat configuration data. Contained
* in the Parameters object is a set of name-value pairs.
*
* @version $Revision: 1.1 $ $Date: 2003/10/29 22:38:59 $
*/
public interface Parameters
{
/**
* Return the names of all the parameters.
*
* @return the names of all the parameters.
*/
String[] getParameterNames();
/**
* Return true of parameter with specified name exists.
*
* @param name the name
* @return true of parameter with specified name exists.
*/
boolean isParameter( String name );
/**
* Return value of parameter with specified name.
*
* @param name the name
* @return the value
* @throws org.realityforge.salt.config.ParameterException if unable to locate parameter
*/
String getParameter( String name )
throws ParameterException;
/**
* Return value of parameter with specified name.
*
* @param name the name
* @param defaultValue the defaultValue if specified parameter
* does not exist
* @return the value
*/
String getParameter( String name, String defaultValue );
/**
* Return value of parameter with specified name as an integer.
*
* @param name the name
* @return the value
* @throws org.realityforge.salt.config.ParameterException if unable to locate parameter
* or parameter can not be converted to correct type
*/
int getParameterAsInteger( String name )
throws ParameterException;
/**
* Return value of parameter with specified name as an integer.
*
* @param name the name
* @param defaultValue the defaultValue if specified parameter
* does not exist or parameter can not be converted to
* the correct type
* @return the value
*/
int getParameterAsInteger( String name, int defaultValue );
/**
* Return value of parameter with specified name as a long.
*
* @param name the name
* @return the value
* @throws org.realityforge.salt.config.ParameterException if unable to locate parameter
* or parameter can not be converted to correct type
*/
long getParameterAsLong( String name )
throws ParameterException;
/**
* Return value of parameter with specified name as a long.
*
* @param name the name
* @param defaultValue the defaultValue if specified parameter
* does not exist or parameter can not be converted to
* the correct type
* @return the value
*/
long getParameterAsLong( String name, long defaultValue );
/**
* Return value of parameter with specified name as a boolean.
*
* @param name the name
* @return the value
* @throws org.realityforge.salt.config.ParameterException if unable to locate parameter
* or parameter can not be converted to correct type
*/
boolean getParameterAsBoolean( String name )
throws ParameterException;
/**
* Return value of parameter with specified name as a boolean.
*
* @param name the name
* @param defaultValue the defaultValue if specified parameter
* does not exist or parameter can not be converted to
* the correct type
* @return the value
*/
boolean getParameterAsBoolean( String name, boolean defaultValue );
/**
* Return value of parameter with specified name as a float.
*
* @param name the name
* @return the value
* @throws org.realityforge.salt.config.ParameterException if unable to locate parameter
* or parameter can not be converted to correct type
*/
float getParameterAsFloat( String name )
throws ParameterException;
/**
* Return value of parameter with specified name as a float.
*
* @param name the name
* @param defaultValue the defaultValue if specified parameter
* does not exist or parameter can not be converted to
* the correct type
* @return the value
*/
float getParameterAsFloat( String name, float defaultValue );
/**
* Return a Parameters object that represents a
* subset of parameters with specified prefix. The child
* parameters has a prefix with the separator ('.') appended.
* ie. if the prefix was "foo" then the parameter
* "foo.baz" would be included in child Parameters object
* using the key "baz".
*
* @param prefix the prefix
* @return the parameters object
*/
Parameters getChildParameters( String prefix );
}
1.1 spice/components/salt/src/java/org/realityforge/salt/config/ParametersUtil.java
Index: ParametersUtil.java
===================================================================
/*
* Copyright (C) The Spice Group. All rights reserved.
*
* This software is published under the terms of the Spice
* Software License version 1.1, a copy of which has been included
* with this distribution in the LICENSE.txt file.
*/
package org.realityforge.salt.config;
import java.util.Iterator;
import java.util.Properties;
/**
* Class containing utility methods to work with Parameters
* objects.
*
* @version $Revision: 1.1 $ $Date: 2003/10/29 22:38:59 $
*/
public class ParametersUtil
{
/**
* Create a Parameters object from aproperties object.
*
* @param properties the properties object
* @return the new Parameters object
*/
public static Parameters fromProperties( final Properties properties )
{
final DefaultParameters parameters = new DefaultParameters();
final Iterator iterator = properties.keySet().iterator();
while( iterator.hasNext() )
{
final String name = (String)iterator.next();
final String value = properties.getProperty( name );
parameters.setParameter( name, value );
}
return parameters;
}
/**
* Create a Parameters object that is the result of merging
* the two parameters objects. If the same key appears in both
* then the value will be the value in parameters2 parameter.
*
* @param parameters1 the first parameters object
* @param parameters2 the second parameters object
* @return the new Parameters object
*/
public static Parameters merge( final Parameters parameters1,
final Parameters parameters2 )
{
final DefaultParameters parameters = new DefaultParameters();
copy( parameters, parameters1 );
copy( parameters, parameters2 );
return parameters;
}
/**
* Copy parameters from input into output.
*
* @param output the output parameters
* @param input the input parameters
*/
static void copy( final DefaultParameters output,
final Parameters input )
{
final String[] names = input.getParameterNames();
for( int i = 0; i < names.length; i++ )
{
final String name = names[ i ];
final String value = input.getParameter( name, null );
output.setParameter( name, value );
}
}
}
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/