SoftNet-Consult Java Utility Library

com.softnetConsult.utils.attributes
Class AttributeManager

java.lang.Object
  extended by com.softnetConsult.utils.attributes.AttributeManager

public final class AttributeManager
extends java.lang.Object

This class allows creating a behaviour similar to java.util.Properties for a Java-compilable collection of values; in other words it provides an interface between the java.util.Properties-API and a collection of attributes implemented as a set of public variables of a class. Assume, a program uses a number of configuration attributes, e.g. width and height. The values could be stored in a common Java properties file and loaded as needed. However, every time a variable value is needed, a request to the Map underlying the Properties object must be generated. In addition, a conversion from String is often necessary (e.g. int height = Integer.parseString(myProperties.getProperty(“colour”))) . This results in an ugly and slow code. Instead, one may have a class MySettings implements Attributes that contains two public static variables: width and height. AttributeManager provides an API for an automatic conversion between such a container of public attributes and a Properties object. Note that properties congregated in a Properties object are freely accessible (public equivalent). If full-scale encapsulation to private level is required, a purpose-build class must be created.

This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.
(http://java-tools.sourceforge.net)
Copyright (c) 2007-2008 SoftNet-Consult.
Copyright (c) 2007-2008 G. Paperin.
All rights reserved.

File: AttributeManager.java
Library API version: "2.02"
Java compliance version: "1.5"

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following terms and conditions are met:

1. Redistributions of source code must retain the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above acknowledgement of the SoftNet-Consult Java Utility Library project, 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 or any derived software must display the following acknowledgement:
This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.

THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version:
"2.02"
Author:
Greg Paperin (http://www.paperin.org)

Field Summary
static char LIST_SEPARATOR
          Separator used when printing a list of attributes.
 
Constructor Summary
private AttributeManager()
          Prevents instances of this class from being created as this class contains only static utility methods.
 
Method Summary
static java.lang.String asLine(java.lang.Class<? extends Attributes> attributesClass)
          Converts an attribute collection to a human readable form.
static java.util.Properties asProperties(java.lang.Class<? extends Attributes> attributesClass)
          Converts an attribute collection to a set of properties.
static java.lang.String asString(java.lang.String separator, java.lang.Class<? extends Attributes> attributesClass)
          Converts an attribute collection to a human readable form.
static java.lang.String[] asStrings(java.lang.Class<? extends Attributes> attributesClass)
          Converts an attribute collection to a human readable form.
static java.util.Map<java.lang.String,java.lang.String> getNameValueMap(java.lang.Class<? extends Attributes> attributesClass)
          Takes an attribute class object and returns a mapping from the names of variables defined for the specified class (not object of that class) and their current (static) values.
static boolean load(java.lang.Class<? extends Attributes> attributesClass, java.lang.String fileName)
          Sets the values of the public static variables of an attribute class to the values loaded from the specified properties file.
static boolean save(java.lang.Class<? extends Attributes> attributesClass, java.lang.String fileName)
          Saves the values of static public variables of an attribute collection class to a Java properties file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LIST_SEPARATOR

public static final char LIST_SEPARATOR
Separator used when printing a list of attributes.

See Also:
Constant Field Values
Constructor Detail

AttributeManager

private AttributeManager()
Prevents instances of this class from being created as this class contains only static utility methods.

Method Detail

getNameValueMap

public static java.util.Map<java.lang.String,java.lang.String> getNameValueMap(java.lang.Class<? extends Attributes> attributesClass)
Takes an attribute class object and returns a mapping from the names of variables defined for the specified class (not object of that class) and their current (static) values.

Parameters:
attributesClass - An attribute class.
Returns:
A mapping from the names of variables defined for the specified class and their current (static) values represented as Strings.

load

public static boolean load(java.lang.Class<? extends Attributes> attributesClass,
                           java.lang.String fileName)
Sets the values of the public static variables of an attribute class to the values loaded from the specified properties file.

Parameters:
attributesClass - An attributes collection.
fileName - A file name.
Returns:
false if an IOException occured while reading the file, true otherwise.

save

public static boolean save(java.lang.Class<? extends Attributes> attributesClass,
                           java.lang.String fileName)
Saves the values of static public variables of an attribute collection class to a Java properties file.

Parameters:
attributesClass - An attribute collection.
fileName - A properties file name.
Returns:
false if an IOException occured while writing the file, true otherwise.

asProperties

public static final java.util.Properties asProperties(java.lang.Class<? extends Attributes> attributesClass)
Converts an attribute collection to a set of properties.

Parameters:
attributesClass - An attribute collection class.
Returns:
A set of properties representing the current values of the attributes as Strings.

asStrings

public static final java.lang.String[] asStrings(java.lang.Class<? extends Attributes> attributesClass)
Converts an attribute collection to a human readable form.

Parameters:
attributesClass - An attribute collection class.
Returns:
An array of strings containing for each attribute of the collection a string of form name=value.

asString

public static final java.lang.String asString(java.lang.String separator,
                                              java.lang.Class<? extends Attributes> attributesClass)
Converts an attribute collection to a human readable form.

Parameters:
separator - Separator between name/value pairs.
attributesClass - An attribute collection class.
Returns:
A String containing containing for each attribute of the collection a sub-string of form name=value, with all such pairs separated by the specified separator.

asLine

public static final java.lang.String asLine(java.lang.Class<? extends Attributes> attributesClass)
Converts an attribute collection to a human readable form.

Parameters:
attributesClass - An attribute collection class.
Returns:
A String containing containing for each attribute of the collection a sub-string of form name=value, with all such pairs separated by the default separator.

SoftNet-Consult Java Utility Library is a member of SourceForge.net