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.
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.
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.
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.
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.
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.
java.lang.ClassCastException - In some implementations,
the objects that are stored may be restricted to a particular
type (such as String). (a RuntimeException)