com.sas.collection
Class StaticCollection

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

public class StaticCollection
implements StaticCollectionInterface

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

   private CollectionInterface members; or perhaps private Collection members
   public StaticCollectionInterface getMembers()
   { return members; }
 
but someone could by the static typing this by checking if the StaticCollectionInterface is really a CollectionInterface, and gain update access to your internal collection:
 StaticCollectionInterface sc = yourObject.getMembers();
 if (sc instanceof CollectionInterface)
 {
    CollectionInterface c = (CollectionInterface) sc;
    // wreak havoc with your collection
 }
 
The StaticCollection class provides an elegant way around this by creating an object which cannot be cast to a Collection or CollectionInterface.
   public StaticCollectionInterface getMembers()
   { return new StaticCollection(members); }
 

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

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

See Also:
Serialized Form

Field Summary
 
Fields inherited from class com.sas.collection.BaseCollection
allContentsChangedListeners, mappingObject
 
Constructor Summary
StaticCollection(StaticCollectionInterface collection)
          Create a StaticCollection which provides read-only access to a Collection.
 
Method Summary
 void apply(com.sas.util.ApplyInterface action)
          Apply an action to all elements in the collection
 boolean contains(java.lang.Object object)
          Test if a collection contains an object.
 void contentsChanged(com.sas.collection.ContentsChangedEvent event)
          Handle a change event from the original collection.
 int count()
          Return the number of items in the collection.
 boolean equals(java.lang.Object object)
          Test if this collection is equal to another static collection
 java.util.Enumeration getItems()
          Get an enumeration of the collection contents.
 int hashCode()
          Return the object's hashcode
 java.lang.String toString()
          Convert this StaticOrderedCollectionInterface to a String
 
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.StaticCollectionInterface
clone
 
Methods inherited from interface com.sas.collection.ContentsChangedSource
addContentsChangedListener, removeContentsChangedListener
 
Methods inherited from interface com.sas.beans.PropertyChangeSource
addPropertyChangeListener, removePropertyChangeListener
 

Constructor Detail

StaticCollection

public StaticCollection(StaticCollectionInterface collection)
Create a StaticCollection which provides read-only access to a Collection.

Parameters:
collection - a collection. Note that the parameter type is StaticCollectionInterface but normally the actual collection passed will implement CollectionInterface or some other modifiable collection interface.
Method Detail

apply

public void apply(com.sas.util.ApplyInterface action)
Apply an action to all elements in the collection

Specified by:
apply in interface StaticCollectionInterface
Overrides:
apply in class BaseCollection
Parameters:
action - an action to apply to all the collection elements. This does not change the collection, but it may change the objects held within the collection.

contains

public boolean contains(java.lang.Object object)
Test if a collection contains an object.

Specified by:
contains in interface StaticCollectionInterface
Overrides:
contains in class BaseCollection
Parameters:
object - an object to find in the collection.
Returns:
true if the collection contains the object, false if not.
See Also:
StaticCollectionInterface.contains(java.lang.Object)

count

public int count()
Return the number of items in the collection.

Specified by:
count in interface com.sas.util.Countable
Specified by:
count in class BaseCollection
Returns:
the number of items in the collection

getItems

public java.util.Enumeration getItems()
Get an enumeration of the collection contents.

Specified by:
getItems in interface com.sas.util.Enumerable
Specified by:
getItems in class BaseCollection
Returns:
an enumeration of the collection contents
See Also:
Enumerable.getItems()

toString

public java.lang.String toString()
Convert this StaticOrderedCollectionInterface to a String

Overrides:
toString in class CollectionMirror

hashCode

public int hashCode()
Return the object's hashcode

Overrides:
hashCode in class CollectionMirror
Returns:
the underlying component's hash code.

equals

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

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

contentsChanged

public void contentsChanged(com.sas.collection.ContentsChangedEvent event)
Handle a change event from the original collection. A new ContentsChangedEvent is fired, with this collection as the source (since this collection is what the listener is listening to, we should not simply resend the event, since it's getSource() method would then reveal the identity of the actual collection we're mirroring.)

Specified by:
contentsChanged in interface com.sas.collection.ContentsChangedListener
Overrides:
contentsChanged in class CollectionMirror
Parameters:
event - an event from the original source.



Copyright © 2009 SAS Institute Inc. All Rights Reserved.