net.walend.lyophil.collection
Class AbstractFreezerHashSet

java.lang.Object
  |
  +--net.walend.lyophil.collection.AbstractFreezerHashSet
All Implemented Interfaces:
Collection, CollectionOfFreezables, HasState, Set, SetOfFreezables
Direct Known Subclasses:
FreezerHashSet, MutableFreezerHashSet

public abstract class AbstractFreezerHashSet
extends java.lang.Object
implements SetOfFreezables

This class implements the net.walend.collection.SetOfFreezables interface, but instead of storing whole Freezables in the set, it stores a Set of identifiers. When it needs the actual Freezables, it thaws them from the Freezer. This impelentation trades memory for time to access.

Since:
200010727
Author:
David Walend

Nested Class Summary
private  class AbstractFreezerHashSet.IteratesFreezables
           
 
Field Summary
private  MutableSet ids
           
(package private) static long serialVersionUID
           
 
Fields inherited from interface net.walend.lyophil.collection.SetOfFreezables
EMPTY
 
Constructor Summary
protected AbstractFreezerHashSet()
          Constructs a new, empty set; the backing MutableHashMap instance has default capacity and load factor, which is 0.75, and uses the DefaultIdentitor.
protected AbstractFreezerHashSet(Collection c)
          Constructs a new set containing the elements in the specified collection.
protected AbstractFreezerHashSet(java.util.Collection c)
          Constructs a new set containing the elements in the specified java.util.Collection.
protected AbstractFreezerHashSet(Identitor identitor)
          Constructs a new, empty set; the backing MutableHashMap instance has default capacity and load factor, which is 0.75.
protected AbstractFreezerHashSet(Identitor identitor, int initialCapacity)
          Constructs a new, empty set; the backing MutableHashMap instance has the specified initial capacity and default load factor, which is 0.75.
protected AbstractFreezerHashSet(Identitor identitor, int initialCapacity, float loadFactor)
          Constructs a new, empty set; the backing MutableHashMap instance has the specified initial capacity and the specified load factor.
protected AbstractFreezerHashSet(int initialCapacity)
          Constructs a new, empty set; the backing MutableHashMap instance has the specified initial capacity and default load factor, which is 0.75.
protected AbstractFreezerHashSet(int initialCapacity, float loadFactor)
          Constructs a new, empty set; the backing MutableHashMap instance has the specified initial capacity and the specified load factor.
 
Method Summary
protected  boolean add(java.lang.Object o)
          Adds the specified element to this set if it is not already present.
protected  boolean addAll(Collection c)
          Adds all the Objects in c to this MutableCollection by using the add() method.
protected  boolean addAllIdentifiedBy(Collection c)
          Adds all the Freezables identified by c to this MutableCollection using the addIdentifier method.
protected  boolean addIdentifiedBy(java.io.Serializable id)
          Adds a Freezable to the MutableCollection.
private  void checkFreezable(java.lang.Object o)
           
protected  void clear()
          Removes all of the elements from this set.
 boolean contains(java.lang.Object o)
          Returns true if this set contains the specified element.
 boolean containsAll(Collection c)
          Returns true if this collection contains all of the Objects in the specified collection.
 boolean containsAllIdentifiedBy(Collection collectionOfFreezables)
          Returns true if this collection contains the specified Freezables.
 boolean containsIdentifiedBy(java.io.Serializable id)
          Returns true if this collection contains the specified Freezable.
 FreezableIterator freezableIterator()
          Returns an iterator over the Freezables in this collection.
protected  Freezable getFreezableForID(java.io.Serializable id)
           
protected  Freezer getFreezer()
           
 Collection getIdentifiers()
          Returns a Collection of all the identifiers in this collection.
 Identitor getIdentitor()
          Returns the Identitor for this Collection.
 java.util.Collection getJavaCollection()
          Return a java.util.Collection of these Objects.
 java.util.Set getJavaSet()
          Return a java.util.Collection of these Objects.
 java.lang.Class getPrincipleInterface()
          Returns the class's principle interface for state comparisons.
 java.util.Iterator identifierIterator()
          Returns an iterator over the identifiers in this collection.
 boolean isEmpty()
          Returns true if this set contains no elements.
 java.util.Iterator iterator()
          Returns an iterator over the elements in this set.
protected  boolean remove(java.lang.Object o)
          Removes the given element from this set if it is present.
protected  boolean removeAll(Collection c)
          Removes all the Objects in c from this MutableCollection by using the remove() method.
protected  boolean removeAllIdentifiedBy(Collection c)
          Removes all the Freezables identified by c from this MutableCollection using the removeIdentifiedBy method.
protected  boolean removeIdentifiedBy(java.io.Serializable id)
          Removes a Freezable from the MutableCollection, based on that Freezable's identifier.
protected  boolean retainAll(Collection c)
          Retains only the Objects in this collection that are contained in c by using the contains() and remove() methods.
protected  boolean retainAllIdentifiedBy(Collection c)
          Removes all but the Freezables identified by c from this MutableCollection using the removeIdentifiedBy method.
 boolean sameContentsAs(Collection c)
          Returns true if this Collection's contents are equal to c's.
 boolean sameStateAs(HasState victem)
          If two HasStates have the same internal state, return true.
 int size()
          Returns the number of elements in this set (its cardinality).
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ids

private MutableSet ids

serialVersionUID

static final long serialVersionUID
Constructor Detail

AbstractFreezerHashSet

protected AbstractFreezerHashSet()
Constructs a new, empty set; the backing MutableHashMap instance has default capacity and load factor, which is 0.75, and uses the DefaultIdentitor.


AbstractFreezerHashSet

protected AbstractFreezerHashSet(Collection c)
Constructs a new set containing the elements in the specified collection. The capacity of the backing MutableHashMap instance is twice the size of the specified collection or eleven (whichever is greater), the default load factor (which is 0.75) is used. It uses the same Identitor as c.

Parameters:
c - the collection whose elements are to be placed into this set.

AbstractFreezerHashSet

protected AbstractFreezerHashSet(java.util.Collection c)
Constructs a new set containing the elements in the specified java.util.Collection. The capacity of the backing MutableHashMap instance is twice the size of the specified collection or eleven (whichever is greater), and the default load factor (which is 0.75) is used. It uses the DefaultIdentitor.

Parameters:
c - the collection whose elements are to be placed into this set.

AbstractFreezerHashSet

protected AbstractFreezerHashSet(int initialCapacity,
                                 float loadFactor)
Constructs a new, empty set; the backing MutableHashMap instance has the specified initial capacity and the specified load factor. It uses the DefaultIdentitor.

Parameters:
initialCapacity - the initial capacity of the hash map.
loadFactor - the load factor of the hash map.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is less than zero, or if the load factor is nonpositive.

AbstractFreezerHashSet

protected AbstractFreezerHashSet(int initialCapacity)
Constructs a new, empty set; the backing MutableHashMap instance has the specified initial capacity and default load factor, which is 0.75. It uses the DefaultIdentitor.

Parameters:
initialCapacity - the initial capacity of the hash table.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is less than zero.

AbstractFreezerHashSet

protected AbstractFreezerHashSet(Identitor identitor)
Constructs a new, empty set; the backing MutableHashMap instance has default capacity and load factor, which is 0.75. It uses the specified identitor.


AbstractFreezerHashSet

protected AbstractFreezerHashSet(Identitor identitor,
                                 int initialCapacity,
                                 float loadFactor)
Constructs a new, empty set; the backing MutableHashMap instance has the specified initial capacity and the specified load factor. It uses the specified identitor.

Parameters:
identitor -
initialCapacity - the initial capacity of the hash map.
loadFactor - the load factor of the hash map.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is less than zero, or if the load factor is nonpositive.

AbstractFreezerHashSet

protected AbstractFreezerHashSet(Identitor identitor,
                                 int initialCapacity)
Constructs a new, empty set; the backing MutableHashMap instance has the specified initial capacity and default load factor, which is 0.75. It uses the specified identitor.

Parameters:
identitor -
initialCapacity - the initial capacity of the hash table.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is less than zero.
Method Detail

getIdentitor

public Identitor getIdentitor()
Returns the Identitor for this Collection. Note that Freezer collection's default behavior implies that this method should return a FreezableIdentitor, but this method will return the Identitor of the underlying Set of identites, and not this collection's Identitor.

Specified by:
getIdentitor in interface Collection

size

public int size()
Returns the number of elements in this set (its cardinality).

Specified by:
size in interface Collection
Returns:
the number of elements in this set (its cardinality).

isEmpty

public boolean isEmpty()
Returns true if this set contains no elements.

Specified by:
isEmpty in interface Collection
Returns:
true if this set contains no elements.

checkFreezable

private void checkFreezable(java.lang.Object o)

contains

public boolean contains(java.lang.Object o)
Returns true if this set contains the specified element.

Specified by:
contains in interface Collection
Parameters:
o - element whose presence in this set is to be tested.
Returns:
true if this set contains the specified element.

iterator

public java.util.Iterator iterator()
Returns an iterator over the elements in this set. The elements are returned in no particular order.

Specified by:
iterator in interface Collection
Returns:
an Iterator over the elements in this set.
See Also:
<{ConcurrentModificationException}>

containsAll

public boolean containsAll(Collection c)
Returns true if this collection contains all of the Objects in the specified collection. This method uses the contains() method.

Specified by:
containsAll in interface Collection

sameContentsAs

public boolean sameContentsAs(Collection c)
Returns true if this Collection's contents are equal to c's.

Specified by:
sameContentsAs in interface Collection

getJavaCollection

public java.util.Collection getJavaCollection()
Description copied from interface: Collection
Return a java.util.Collection of these Objects.

Specified by:
getJavaCollection in interface Collection

getJavaSet

public java.util.Set getJavaSet()
Description copied from interface: Set
Return a java.util.Collection of these Objects.

Specified by:
getJavaSet in interface Set

add

protected boolean add(java.lang.Object o)
Adds the specified element to this set if it is not already present.

Parameters:
o - element to be added to this set.
Returns:
true if the set did not already contain the specified element.

remove

protected boolean remove(java.lang.Object o)
Removes the given element from this set if it is present.

Parameters:
o - object to be removed from this set, if present.
Returns:
true if the set contained the specified element.

addAll

protected boolean addAll(Collection c)
Adds all the Objects in c to this MutableCollection by using the add() method. Returns true if this MutableCollection changes.

Throws:
java.lang.UnsupportedOperationException - if this collection does not support the addAll method.
java.lang.ClassCastException - if the class of an element of the specified collection prevents it from being added to this collection.
java.lang.IllegalArgumentException - some aspect of an element of the specified collection prevents it from being added to this collection.

removeAll

protected boolean removeAll(Collection c)
Removes all the Objects in c from this MutableCollection by using the remove() method. Returns true if this Colleciton changes.

Throws:
java.lang.UnsupportedOperationException - if the removeAll method is not supported by this collection.

retainAll

protected boolean retainAll(Collection c)
Retains only the Objects in this collection that are contained in c by using the contains() and remove() methods. Returns true if this Colleciton changes.

Throws:
java.lang.UnsupportedOperationException - if the retainAll method is not supported by this collection.

clear

protected void clear()
Removes all of the elements from this set.


getPrincipleInterface

public java.lang.Class getPrincipleInterface()
Description copied from interface: HasState
Returns the class's principle interface for state comparisons. If two objects have different principle interfaces, they never have the same state.

Specified by:
getPrincipleInterface in interface HasState

sameStateAs

public boolean sameStateAs(HasState victem)
Description copied from interface: HasState
If two HasStates have the same internal state, return true.

For objects with subobjects, Generally this method should only return true if the internal objects are equal. Implement a contentsHaveSameState() method to determine if the contents have the same state.

Specified by:
sameStateAs in interface HasState

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getFreezer

protected Freezer getFreezer()

getFreezableForID

protected Freezable getFreezableForID(java.io.Serializable id)

containsIdentifiedBy

public boolean containsIdentifiedBy(java.io.Serializable id)
Returns true if this collection contains the specified Freezable. This method uses Serializable's equals() method.

Specified by:
containsIdentifiedBy in interface CollectionOfFreezables

containsAllIdentifiedBy

public boolean containsAllIdentifiedBy(Collection collectionOfFreezables)
Returns true if this collection contains the specified Freezables. This method uses Serializable's equals() method.

Specified by:
containsAllIdentifiedBy in interface CollectionOfFreezables

freezableIterator

public FreezableIterator freezableIterator()
Returns an iterator over the Freezables in this collection.

Specified by:
freezableIterator in interface CollectionOfFreezables

identifierIterator

public java.util.Iterator identifierIterator()
Returns an iterator over the identifiers in this collection.

Specified by:
identifierIterator in interface CollectionOfFreezables

getIdentifiers

public Collection getIdentifiers()
Returns a Collection of all the identifiers in this collection.

Specified by:
getIdentifiers in interface CollectionOfFreezables

addIdentifiedBy

protected boolean addIdentifiedBy(java.io.Serializable id)
Adds a Freezable to the MutableCollection. Sets will only add it to the Set if it's not already there, based on the getIdentifier() and sameIdentifierAs() methods. Lists will tack it on the end of a list.

Returns:
true if the MutableCollection changes, false if not.
Throws:
java.lang.UnsupportedOperationException - add is not supported by this collection.
java.lang.ClassCastException - class of the specified element prevents it from being added to this collection.
java.lang.IllegalArgumentException - some aspect of this element prevents it from being added to this collection.
ZombieIdentifierException - if f is not in the Freezer and this implementation checks on add.

removeIdentifiedBy

protected boolean removeIdentifiedBy(java.io.Serializable id)
Removes a Freezable from the MutableCollection, based on that Freezable's identifier. This method uses the getIdentifier() and sameIdentifierAs() methods of the Freezables to find the right one.

The AbstractHashSet implementation has to scan the freezables in the Set to find the right one.

Returns:
true if the MutableCollection changes, false if not.
Throws:
java.lang.UnsupportedOperationException - remove is not supported by this collection.

addAllIdentifiedBy

protected boolean addAllIdentifiedBy(Collection c)
Adds all the Freezables identified by c to this MutableCollection using the addIdentifier method. Returns true if this MutableCollection changes.

Throws:
java.lang.UnsupportedOperationException - if this collection does not support the addAll method.
java.lang.ClassCastException - if the class of an element of the specified collection prevents it from being added to this collection.
java.lang.IllegalArgumentException - some aspect of an element of the specified collection prevents it from being added to this collection.

removeAllIdentifiedBy

protected boolean removeAllIdentifiedBy(Collection c)
Removes all the Freezables identified by c from this MutableCollection using the removeIdentifiedBy method. Returns true if this MutableCollection changes.

Throws:
java.lang.ClassCastException - if the class of an element of the specified collection prevents it from being added to this collection.
java.lang.IllegalArgumentException - some aspect of an element of the specified collection prevents it from being added to this collection.

retainAllIdentifiedBy

protected boolean retainAllIdentifiedBy(Collection c)
Removes all but the Freezables identified by c from this MutableCollection using the removeIdentifiedBy method. Returns true if this MutableCollection changes.

Throws:
java.lang.ClassCastException - if the class of an element of the specified collection prevents it from being added to this collection.
java.lang.IllegalArgumentException - some aspect of an element of the specified collection prevents it from being added to this collection.


Copyright (c) 2000, 2001, David Walend