com.sas.collection
Class StaticDictionary

com.sas.collection.StaticDictionary
All Implemented Interfaces:
com.sas.beans.PropertyChangeSource, com.sas.beans.VetoableChangeSource, com.sas.collection.ContentsChangedListener, com.sas.collection.ContentsChangedSource, StaticCollectionInterface, StaticDictionaryInterface, 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

public class StaticDictionary
implements StaticDictionaryInterface

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

   private DictionaryInterface members;  // or perhaps private Dictionary members
   public StaticDictionaryInterface getMembers()
   { return members; }
 
but someone can always bypass this by checking if a StaticDictionaryInterface is really a DictionaryInterface and gain update access to your internal dictionary:
 StaticDictionaryInterface sc = yourObject.getMembers();
 if (sc instanceof DictionaryInterface)
 {
    DictionaryInterface c = (DictionaryInterface) sc;
    // wreak havoc with your Dictionary
 }
 
The StaticDictionary class provides an elegant way around this by creating an object which cannot be cast to a Dictionary or DictionaryInterface:
   private DictionaryInterface members;
   public StaticDictionaryInterface getMembers()
   { return new StaticDictionary(members); }
 

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

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

See Also:
Serialized Form

Field Summary
 
Fields inherited from class com.sas.collection.BaseCollection
allContentsChangedListeners, mappingObject
 
Constructor Summary
StaticDictionary(StaticDictionaryInterface dictionary)
          Create a StaticDictionary which provides read-only access to a Dictionary.
 
Method Summary
 boolean containsKey(java.lang.Object key)
          Test if the dictionary contains a specific key.
 void contentsChanged(com.sas.collection.ContentsChangedEvent event)
          Handle a change event from the original collection.
 java.lang.Object get(java.lang.Object key)
          Fetch the item corresponding to a key.
 java.lang.Object getKey(java.lang.Object value)
          Get the key corresponding to an item.
 java.util.Enumeration getKeys()
          Return an enumeration of all the keys.
 java.util.Enumeration getKeys(java.lang.Object value)
          Return an Enumeration of all the keys for a given item.
 
Methods inherited from class com.sas.collection.StaticCollection
apply, contains, count, equals, 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.StaticDictionaryInterface
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.Countable
count
 
Methods inherited from interface com.sas.util.Enumerable
getItems
 

Constructor Detail

StaticDictionary

public StaticDictionary(StaticDictionaryInterface dictionary)
Create a StaticDictionary which provides read-only access to a Dictionary.

Parameters:
collection - a collection. Note that the parameter type is StaticDictionaryInterface but normally the actual collection passed will implement DictionaryInterface or some other interface which provides update access to the collection.
Method Detail

containsKey

public boolean containsKey(java.lang.Object key)
Test if the dictionary contains a specific key.

Specified by:
containsKey in interface StaticDictionaryInterface
Parameters:
key - the key to search for
Returns:
true if and only an item with the specified key exists in the dictionary.

get

public java.lang.Object get(java.lang.Object key)
Fetch the item corresponding to a key.

Specified by:
get in interface StaticDictionaryInterface
Parameters:
key - the Object key with which the item was inserted.
Returns:
the item in the collection which corresponds to the given key.
Throws:
java.util.NoSuchElementException - when there is no entry for the specified key. (Note that null is a valid value for a key.)

getKey

public java.lang.Object getKey(java.lang.Object value)
Get the key corresponding to an item. Note that a dictionary may contain the same item via multiple keys. If so, this method returns one, but the interface does not specify which one (i.e. it does not have to be the first one entered in the dictionary, or the first one as found in the elements() enumeration.

Specified by:
getKey in interface StaticDictionaryInterface
Parameters:
item - the item whose key you wish to find.
Returns:
the key for the item, or throws NoSuchElementException if the item is not found
Throws:
java.util.NoSuchElementException - Thrown if item not found

getKeys

public java.util.Enumeration getKeys(java.lang.Object value)
Return an Enumeration of all the keys for a given item. The order is unspecified. If the item does not exist, an empty Enumeration is returned.

Specified by:
getKeys in interface StaticDictionaryInterface
Parameters:
item - the item to search for
Returns:
an Enumeration of the keys corresponding to the item.

getKeys

public java.util.Enumeration getKeys()
Description copied from interface: StaticDictionaryInterface
Return an enumeration of all the keys.

Specified by:
getKeys in interface StaticDictionaryInterface
Returns:
an Enumeration of this dictionary's keys.

contentsChanged

public void contentsChanged(com.sas.collection.ContentsChangedEvent event)
Handle a change event from the original collection. A new ContentsChangedEvent is fired, with this dictionary as the source (since this dictionary 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 StaticCollection
Parameters:
event - an event from the original source.



Copyright © 2009 SAS Institute Inc. All Rights Reserved.