Path and ContextList The Value Objects

15.5.2 Path and ContextList

Path and ContextList are simpler objects than AttributeSet . They are both subclasses of SerializableList and are designed to hold an ordered sequence of values. In the case of Path , the ordered sequence is a sequence of instances of String ; in the case of ContextList , the sequence is a sequence of instances of Context . SerializableList is a container whose main role is to provide a wrapper around ArrayLists . Recall that we dont want our Remote interfaces to directly use the Javasoft collection classes and provide an implementation of equals . This implementation of equals is a little strange in one respect™order is very important. Paths are more than a collection of strings; the path usrgamesbin is different than the path usrbingames and they are not equal. Heres the code for SerializableList : public abstract class SerializableList implements Serializable { protected ArrayList _containedObjects; public SerializableList { here so we can deserialize } public SerializableListCollection objects { _containedObjects = new ArrayListobjects.size ; Iterator i = objects.iterator ; while i.hasNext { _containedObjects.addi.next ; } } public synchronized int getSize { return _containedObjects.size ; } public synchronized Object getint index { return _containedObjects.getindex; } public synchronized boolean equalsObject otherObject { if false == containerIsOfSameTypeotherObject { return false; } SerializableList otherComparableList = SerializableList otherObject; int size = _containedObjects.size ; if size = otherComparableList.getSize { return false; } for int i = 0; i size; i++ { if false == equalObjects_containedObjects.geti, otherComparableList geti { return false; } } return true; } protected abstract boolean containerIsOfSameTypeObject object; protected abstract boolean equalObjectsObject firstObject, Object secondObject; } Once we have SerializableList , ContextList is simple. ContextList simply provides the obvious implementations of containerIsOfSameType and equalObjects . Heres the entire implementation of ContextList : public class ContextList extends SerializableList { private ContextList { here so we can deserialize } public ContextListList components { supercomponents; } protected boolean containerIsOfSameTypeObject object { return object instanceof ContextList; } protected boolean equalObjectsObject firstObject, Object secondObject { Context firstContext = Context firstObject; Context secondContext = Context secondObject; return firstContext.equalssecondContext; } } Path is only slightly more complicated. In addition to extending SerializableList , Path needs to support two additional list operations, corresponding to the path resolution operations used by contexts. Namely, an instance of Path needs to be able to return its first path component, which is a string, and the remainder of the path. For example, usrbingames has as its first path component usr , and bingames as the remainder. In addition, Path has a static convenience method to make it easy to assemble instances of Path from strings. Heres the code for Path : public class Path extends SerializableList { public static Path buildPathString[] components { ArrayList arrayList = new ArrayListcomponents.length; for int i = 0; i components.length; i++ { if null = components[i] 0=components[i].length { arrayList.addcomponents[i]; } } return new PatharrayList; } private Path { here so we can deserialize } public PathList components { supercomponents; } public synchronized String getFirstComponent { if _containedObjects.size == 0 { return null; } return String _containedObjects.get0; } public synchronized Path getSubPath { if _containedObjects.size == 0 { return null; } ArrayList subPathComponents = new ArrayList_containedObjects; subPathComponents.remove0; return new PathsubPathComponents; } public synchronized boolean isEmpty { return 0==getSize ; } protected boolean containerIsOfSameTypeObject object { return object instanceof Path; } protected boolean equalObjectsObject firstObject, Object secondObject { String firstString = String firstObject; String secondString = String secondObject; return firstString.equalssecondString; } protected int compareObjectsObject firstObject, Object secondObject { String firstString = String firstObject; String secondString = String secondObject; return firstString.compareTosecondString; } } Note that Path suffers from the same bandwidth problems that AttributeSet has. Yet the same solution works: we could replace all the instances of String with instances of Integer and boost Path s performance significantly. The trick of replacing strings with their hashcodes is well worth remembering.

15.6 ContextImpl