FileDocCategorySizeDatePackage
MutableObject.javaAPI DocExample8222Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.datamodeling

MutableObject

public abstract class MutableObject extends Object implements Serializable
Base class for data model objects that are mutable.

All data model objects that are not constant descend from this class. It provides services for handling these objects such as firing property change events.

author
Robert Simmons jr.
version
$Revision: 1.10 $

Fields Summary
private static final long
serialVersionUID
Use serialVersionUID for interoperability.
private static final Map
CONSTRAINT_CACHE
Holds a cache of constraints asked for. The cache is a structure of a Map of Maps. The outter map is keyed by class and contains a value which is another map. These maps are keyed by property name and have the constraint instances as values.
protected final transient oreilly.hcj.references.PropertyChangeSupport
propertyChangeSupport
Utility field used by bound properties.
Constructors Summary
protected MutableObject()
Creates a new instance of MutableObject


	       	 
	  
	
Methods Summary
public voidaddPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list.

param
listener The listener to add.

		propertyChangeSupport.addPropertyChangeListener(listener);
	
public voidaddPropertyChangeListener(java.lang.String property, java.beans.PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list for a specific property.

param
property The property to listen to.
param
listener The listener to add.

		propertyChangeSupport.addPropertyChangeListener(property, listener);
	
protected static final java.util.MapbuildConstraintMap(java.lang.Class dataType)
Creates a constraint map for the given class.

param
dataType The data type to create the map for.
return
The constructed unmodifiable constraint map.
throws
IllegalAccessException If there is a security violation.

		final int modifiers = Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC;

		// --
		Map constraintMap = new HashMap();
		final Field[] fields = dataType.getFields();
		Object value = null;
		for (int idx = 0; idx < fields.length; idx++) {
			if ((fields[idx].getModifiers() & modifiers) == modifiers) {
				value = fields[idx].get(null);
				if (value instanceof ObjectConstraint) {
					constraintMap.put(((ObjectConstraint)value).getName(), value);
				}
			}
		}
		return Collections.unmodifiableMap(constraintMap);
	
public abstract booleanequals(java.lang.Object obj)

see
java.lang.Object#equals(java.lang.Object)

public static oreilly.hcj.datamodeling.constraints.ObjectConstraintgetConstraint(java.lang.Class dataType, java.lang.String name)
Fetch the named constraint for the given data type.

param
dataType The class type for which to look up the constraint.
param
name The name of the constraitn to look up.
return
The named constraint or null.

		Map constraintMap = getConstraintMap(dataType);
		return (ObjectConstraint)constraintMap.get(name);
	
public static final java.util.MapgetConstraintMap(java.lang.Class dataType)
Return the cached constraints or index the constraints of a given class if they aren't already indexed.

param
dataType The class type for which to index the constraints.
return
The constraitn map for the given data type.
throws
RuntimeException If there is a problem indexing the constraints. Please see contained exception if this occurs.

		try {
			Map constraintMap = (Map)CONSTRAINT_CACHE.get(dataType);
			if (constraintMap == null) {
				constraintMap = buildConstraintMap(dataType);
				CONSTRAINT_CACHE.put(dataType, constraintMap);
				return constraintMap;
			}
			return Collections.unmodifiableMap(constraintMap);
		} catch (final IllegalAccessException ex) {
			throw new RuntimeException(ex);
		}
	
public abstract inthashCode()

see
java.lang.Object#hashCode()

public voidremovePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes a PropertyChangeListener to the listener list.

param
listener The listener to add.

		propertyChangeSupport.removePropertyChangeListener(listener);
	
public voidremovePropertyChangeListener(java.lang.String property, java.beans.PropertyChangeListener listener)
Removes a PropertyChangeListener to the listener list for a specific property.

param
property The property to listen to.
param
listener The listener to add.

		propertyChangeSupport.removePropertyChangeListener(property, listener);
	
public java.lang.StringtoString()
{@inheritDoc} Creates as string containing the class name and the readable properties of the object.

		try {
			final BeanInfo info = Introspector.getBeanInfo(this.getClass(), Object.class);
			final PropertyDescriptor[] props = info.getPropertyDescriptors();
			final StringBuffer buf = new StringBuffer(500);
			Object value = null;
			buf.append(getClass().getName());
			buf.append("@");  //$NON-NLS-1$
			buf.append(hashCode());
			buf.append("={");  //$NON-NLS-1$
			for (int idx = 0; idx < props.length; idx++) {
				if (idx != 0) {
					buf.append(", ");  //$NON-NLS-1$
				}
				buf.append(props[idx].getName());
				buf.append("=");  //$NON-NLS-1$
				if (props[idx].getReadMethod() != null) {
					value = props[idx].getReadMethod()
						              .invoke(this, null);
					if (value instanceof MutableObject) {
						buf.append("@");  //$NON-NLS-1$
						buf.append(value.hashCode());
					} else if (value instanceof Collection) {
						buf.append("{");  //$NON-NLS-1$
						for (Iterator iter = ((Collection)value).iterator();
						     iter.hasNext();) {
							Object element = iter.next();
							if (element instanceof MutableObject) {
								buf.append("@");  //$NON-NLS-1$
								buf.append(element.hashCode());
							} else {
								buf.append(element.toString());
							}
						}
						buf.append("}");  //$NON-NLS-1$
					} else if (value instanceof Map) {
						buf.append("{");  //$NON-NLS-1$
						Map map = (Map)value;
						for (Iterator iter = map.keySet()
							                    .iterator(); iter.hasNext();) {
							Object key = iter.next();
							Object element = map.get(key);
							buf.append(key.toString() + "=");
							if (element instanceof MutableObject) {
								buf.append("@");  //$NON-NLS-1$
								buf.append(element.hashCode());
							} else {
								buf.append(element.toString());
							}
						}
						buf.append("}");  //$NON-NLS-1$
					} else {
						buf.append(value);
					}
				}
			}
			buf.append("}");  //$NON-NLS-1$
			return buf.toString();
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}