FileDocCategorySizeDatePackage
ConstantObject.javaAPI DocExample4172Sun Dec 14 22:47:32 GMT 2003oreilly.hcj.constants

ConstantObject

public abstract class ConstantObject extends Object implements Serializable
Defines a class of objects that are constant and thus immutable.

Objects descending from this class cannot change unless there is a code change. That makes them uneditable by the user and thus constant. Naturally the descendant classes shouldn't have public constructors.

author
Robert Simmons jr.
version
$Revision: 1.13 $
see
DataModelObjectTest

Fields Summary
private static final Map
CONSTANTS_MASTER_INDEX
Holds the grand constants index map. Contains references to all constants indexes that are instantiated indexed by constant name.
private static final long
serialVersionUID
Use serialVersionUID for interoperability.
private static final String
ERR_DUP_NAME
Error message when user passes a name that is already defined for this class.
private final String
name
Holds value of property name.
Constructors Summary
protected ConstantObject(String name)
Creates a new instance of ConstantObject.

param
name The name for the constant object.
throws
NullPointerException If null is given for the constant name.
throws
IllegalArgumentException If the user passes a null name.


	                                  	 
	    
		if (name == null) {
			throw new NullPointerException("The name may not be null.");  //$NON-NLS-1$
		}
		Map constMap = (Map)CONSTANTS_MASTER_INDEX.get(getClass());
		if (constMap == null) {
			constMap = new HashMap();
			CONSTANTS_MASTER_INDEX.put(getClass(), constMap);
		}
		if (constMap.containsKey(name)) {
			throw new IllegalArgumentException(ERR_DUP_NAME);
		}
		this.name = name;
		constMap.put(name, this);
	
Methods Summary
public final java.lang.StringgetName()
Getter for property name.

return
Value of property name.

		return this.name;
	
public static final oreilly.hcj.constants.ConstantObjectlookup(java.lang.Class dataType, java.lang.String name)
Get the constant for the given class and id.

param
dataType The class for which to retrieve the index.
param
name The name of the constant.
return
The constant object for the given class and name.

		return (ConstantObject)((Map)CONSTANTS_MASTER_INDEX.get(dataType)).get(name);
	
public static final java.util.Maplookup(java.lang.Class dataType)
Get the constants index for the given class.

param
dataType The class for which to retrieve the index.
return
An unmodifiable map of constants for the given class.

		return Collections.unmodifiableMap((Map)CONSTANTS_MASTER_INDEX.get(dataType));
	
protected java.lang.ObjectreadResolve()
Resolve a read in object.

return
The resolved object read in.
throws
ObjectStreamException if there is a problem reading the object.
throws
RuntimeException If the read object doesnt exist.

		Object result = lookup(getClass(), getName());
		if (result == null) {
			throw new RuntimeException("Constant not found for name: " + getName());  //$NON-NLS-1$
		}
		return result;
	
public java.lang.StringtoString()

see
java.lang.Object#toString()

		return this.getClass()
		           .getName() + "." + this.name;