com.sas.collection
Class StaticStringList

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

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

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

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

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

See Also:
Serialized Form

Field Summary
 
Fields inherited from class com.sas.collection.BaseCollection
allContentsChangedListeners, mappingObject
 
Constructor Summary
StaticStringList(StaticStringListInterface strings)
          Create a StaticStringList which provides read-only access to another string list.
 
Method Summary
 boolean contains(java.lang.Object object)
          Test if a collection contains an object.
 int count()
          Return the number of items in this string collection.
 boolean equals(StaticStringListInterface list)
          Compare the current items with those in another StaticStringList and tell whether the lists are identical.
 int getIndex(java.lang.String item, int startIndex)
          Returns the index associated with the item (searches forward)
 java.util.Enumeration getItems()
          Get an enumeration of this collection's contents.
 int getLastIndex(java.lang.String item, int startIndex)
          Returns the index associated with the item (searches backward)
 java.lang.String getString(int index)
          Fetch a string at a index.
 java.lang.String[] getStringItems()
          Return the Strings in this string list as an array of String.
 StaticStringListInterface getStringItems(int start, int last)
          Extract a sublist of items from this string list
 
Methods inherited from class com.sas.collection.CollectionMirror
addContentsChangedListener, clone, contentsChanged, equals, getSource, hashCode, removeContentsChangedListener, toString
 
Methods inherited from class com.sas.collection.BaseCollection
anyContentsChangedListeners, apply, 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.StaticStringListInterface
clone
 
Methods inherited from interface com.sas.collection.ContentsChangedSource
addContentsChangedListener, removeContentsChangedListener
 
Methods inherited from interface com.sas.beans.PropertyChangeSource
addPropertyChangeListener, removePropertyChangeListener
 

Constructor Detail

StaticStringList

public StaticStringList(StaticStringListInterface strings)
Create a StaticStringList which provides read-only access to another string list.

Parameters:
strings - a string list. Note that the parameter type is StaticStringListInterface but normally the actual collection passed will implement StringListInterface
Method Detail

equals

public boolean equals(StaticStringListInterface list)
Compare the current items with those in another StaticStringList and tell whether the lists are identical. The equals method is used to compare the Strings.

Specified by:
equals in interface StaticStringListInterface
Parameters:
list - Another string list to be compared to.
Returns:
true if identical, false otherwise.

getIndex

public int getIndex(java.lang.String item,
                    int startIndex)
Returns the index associated with the item (searches forward)

Specified by:
getIndex in interface StaticStringListInterface
Parameters:
item - item to search for
startIndex - index where search should start
Returns:
the zero-based index corresponding to the list item or -1 if the item is not found
Throws:
java.lang.IndexOutOfBoundsException - if the start index is not in the range [0..count()-1]

getStringItems

public java.lang.String[] getStringItems()
Return the Strings in this string list as an array of String.

Specified by:
getStringItems in interface StaticStringListInterface
Returns:
an arracy of the String values in this string list.

getStringItems

public StaticStringListInterface getStringItems(int start,
                                                int last)
Extract a sublist of items from this string list

Specified by:
getStringItems in interface StaticStringListInterface
Parameters:
start - the index of the first item, zero based.
last - the index of the last item, zero based.
Returns:
a list of the selected elements.
Throws:
java.lang.IndexOutOfBoundsException - if start or last are not in the range [0, count()-1].
See Also:
StaticOrderedCollectionInterface.getItems(int, int), String.substring(int, int)

getLastIndex

public int getLastIndex(java.lang.String item,
                        int startIndex)
Returns the index associated with the item (searches backward)

Specified by:
getLastIndex in interface StaticStringListInterface
Parameters:
item - item to search for
startIndex - index where search should start
Returns:
int the zero-based index corresponding to the list item of -1 if the item is not found
Throws:
java.lang.IndexOutOfBoundsException - if the start index is not in the range [0..count()-1]

getString

public java.lang.String getString(int index)
Fetch a string at a index.

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

count

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

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

getItems

public java.util.Enumeration getItems()
Get an enumeration of this collection's contents.

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

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 this string collection.
Returns:
true if this string collection contains the object, false if not.
See Also:
StaticCollectionInterface.contains(java.lang.Object)



Copyright © 2009 SAS Institute Inc. All Rights Reserved.