com.sas.collection
Class StaticOrderedCollection

com.sas.collection.StaticOrderedCollection
All Implemented Interfaces:
com.sas.beans.PropertyChangeSource, com.sas.beans.VetoableChangeSource, com.sas.collection.ContentsChangedListener, com.sas.collection.ContentsChangedSource, StaticCollectionInterface, StaticOrderedCollectionInterface, ComponentInterface, LinkPropertiesInterface, ModelInterface, com.sas.PublicClonable, com.sas.util.Countable, com.sas.util.Enumerable, com.sas.util.EventGateInterface, IndexedGetInterface, ViewInterface, MultipleValueEventSourceInterface, java.beans.PropertyChangeListener, java.io.ObjectInputValidation, java.io.Serializable, java.lang.Cloneable, java.util.EventListener

public class StaticOrderedCollection
implements StaticOrderedCollectionInterface

A StaticOrderedCollection is a class which hides the update methods of a OrderedCollection object. This is useful if you have an object which has an internal OrderedCollection and you would like to make available read-only access to that OrderedCollection. To do so, you could provide a public method which return the type StaticOrderedCollectionInterface:

   private OrderedCollectionInterface members;
   // or perhaps private OrderedCollection members
   public StaticOrderedCollectionInterface getMembers()
   { return new StaticOrderedCollection(members); }
 
but someone can always bypass this by checking if a StaticOrderedCollectionInterface is really a OrderedCollectionInterface:
 StaticOrderedCollectionInterface sc = yourObject.getMembers();
 if (sc instanceof OrderedCollectionInterface)
 {
    OrderedCollectionInterface c = (OrderedCollectionInterface) sc;
    // wreak havoc with your OrderedCollection
 }
 
The StaticOrderedCollection class provides an elegant way around this by creating an object which cannot be cast to a OrderedCollection or OrderedCollectionInterface.

A recommended way for your to use the StaticOrderedCollection class is to only create it once and always return that reference.

   private StaticOrderedCollection sc;
   public StaticOrderedCollectionInterface getMembers()
   {
     if (sc == null)
        sc = new StaticOrderedCollection(members);
     return sc;
   }
 
Note, however, that if you create a new members collection, you will need to create a new StaticOrderedCollection object as well, since there is no way to change the OrderedCollectionInterface that the StaticOrderedCollection refers to. Also, be aware that in such cases, other object which hold references to your old StaticOrderedCollection object will have stale object references.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class com.sas.collection.BaseCollection
allContentsChangedListeners, mappingObject
 
Constructor Summary
StaticOrderedCollection(StaticOrderedCollectionInterface collection)
          Create a StaticOrderedCollection which provides read-only access to an OrderedCollection.
 
Method Summary
 boolean equals(java.lang.Object object)
          Test if this collection is equal to another static collection
 boolean equals(StaticOrderedCollectionInterface orderedCollection)
          Compare the current items with those in another collection and tell whether the collections are identical.
 java.lang.Object get(int index)
          Fetch an item at a index.
 int getIndex(java.lang.Object element, int startIndex)
          Returns the index of the first occurrence of the item.
 java.util.Enumeration getItems(int start, int end)
          Returns an enumeration of a subset of the items from a collection.
 int getLastIndex(java.lang.Object element, int startIndex)
          Returns the index of the last occurrence of the item.
 
Methods inherited from class com.sas.collection.StaticCollection
apply, contains, contentsChanged, count, getItems, hashCode, toString
 
Methods inherited from class com.sas.collection.CollectionMirror
addContentsChangedListener, clone, getSource, removeContentsChangedListener
 
Methods inherited from class com.sas.collection.BaseCollection
anyContentsChangedListeners, disableEvents, enableEvents, enableEvents, fireContentsChanged, map, unmap
 
Methods inherited from class com.sas.Component
addLink, addPropertyChangeListener, addVetoableChangeListener, anyPropertyChangeListeners, attachModel, attachView, beansIsDesignTime, beansSetDesignTime, clone, detachModel, detachView, dumpComponent, firePropertyChange, firePropertyChange, fireVetoableChange, getComponentDescription, getComponentSupportInfo, getEventMethod, getEventValues, getExtendedBeanInfo, getLinkInfo, getModelInterface, getRequiredInterfaces, getResources, getStringResource, getViewInterfaceSupportInfo, initialize, initializeComponent, isDesignTime, isLinked, propertyChange, queryLinks, queryLinks, refresh, removeAllLinks, removeInterfaceTraps, removeLink, removePropertyChangeListener, removeVetoableChangeListener, setComponentDescription, setComponentSupportInfo, setDefaultValues, setLinkInfo, setModelInterface, setRequiredInterfaces, setViewInterfaceSupportInfo, supportsListenerInterface, supportsRequiredInterfaces, trapInterfaceEvents, validateObject
 
Methods inherited from interface com.sas.collection.StaticOrderedCollectionInterface
clone
 
Methods inherited from interface com.sas.collection.StaticCollectionInterface
apply, contains
 
Methods inherited from interface com.sas.collection.ContentsChangedSource
addContentsChangedListener, removeContentsChangedListener
 
Methods inherited from interface com.sas.beans.PropertyChangeSource
addPropertyChangeListener, removePropertyChangeListener
 
Methods inherited from interface com.sas.util.Enumerable
getItems
 
Methods inherited from interface com.sas.util.Countable
count
 

Constructor Detail

StaticOrderedCollection

public StaticOrderedCollection(StaticOrderedCollectionInterface collection)
Create a StaticOrderedCollection which provides read-only access to an OrderedCollection.

Parameters:
collection - a collection. Note that the parameter type is StaticOrderedCollectionInterface but normally the actual collection passed will implement OrderedCollectionInterface.
Method Detail

equals

public boolean equals(StaticOrderedCollectionInterface orderedCollection)
Compare the current items with those in another collection and tell whether the collections are identical. The equals method is used to compare collection items.

Specified by:
equals in interface StaticOrderedCollectionInterface
Parameters:
orderedCollection - Another ordered collection to be compared to.
Returns:
True if identical, False otherwise.

get

public java.lang.Object get(int index)
Fetch an item at a index.

Specified by:
get in interface IndexedGetInterface
Parameters:
index - the position of the item, zero-based indexing.
Returns:
the item associated with the index value
Throws:
java.lang.IndexOutOfBoundsException - if index is not in the range [0, count()-1].

getIndex

public int getIndex(java.lang.Object element,
                    int startIndex)
Returns the index of the first occurrence of the item. The search proceeds from startIndex to the end of the list; the index of the first item that matches the element is returned.

Specified by:
getIndex in interface StaticOrderedCollectionInterface
Parameters:
element - item to search for
startIndex - the position where the search should start
Returns:
the zero-based index where the list item was first found, or -1 if the item is not found.
Throws:
java.lang.IndexOutOfBoundsException - if the index is not in the range [0..count()-1].
java.lang.ClassCastException - In some implementations, the objects that are stored may be restricted to a particular type (such as String).

getItems

public java.util.Enumeration getItems(int start,
                                      int end)
Returns an enumeration of a subset of the items from a collection.

Specified by:
getItems in interface StaticOrderedCollectionInterface
Parameters:
start - the index of the first item, zero based.
end - one past the index of the last item to include in the enumeration, zero based. Note that the range of elements returned in the Enumeration does not include the endthe item; this is consistent with substring(start, end) in java.lang.String, for example.
Returns:
an enumeration of the requested elements.
Throws:
java.lang.IndexOutOfBoundsException - if start is not in the range [0, count()-1], or start>last or end is not in the range [0, count()]

getLastIndex

public int getLastIndex(java.lang.Object element,
                        int startIndex)
Returns the index of the last occurrence of the item. The search proceeds from the startIndex to the start of the list until an item is found.

Specified by:
getLastIndex in interface StaticOrderedCollectionInterface
Parameters:
element - the item to search for
startIndex - the position where the search should start
Returns:
the zero-based index of the last item matching element, or -1 if the item is not found.
Throws:
java.lang.IndexOutOfBoundsException - if the index is not in the range [0..count()-1]
java.lang.ClassCastException - In some implementations, the objects that are stored may be restricted to a particular type (such as String). (a RuntimeException)

equals

public boolean equals(java.lang.Object object)
Test if this collection is equal to another static collection

Overrides:
equals in class StaticCollection
Returns:
true if this object equals another object, else return false



Copyright © 2009 SAS Institute Inc. All Rights Reserved.