org.geotools.util
Class DerivedSet<B,E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<E>
          extended by org.geotools.util.DerivedSet<B,E>
Type Parameters:
B - The type of elements in the backing set.
E - The type of elements in this set.
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>, CheckedCollection<E>

public abstract class DerivedSet<B,E>
extends java.util.AbstractSet<E>
implements CheckedCollection<E>, java.io.Serializable

A set whose values are derived from an other set. The values are derived only when requested, which make it possible to backup potentially large sets. Implementations need only to overrides baseToDerived(B) and derivedToBase(E) methods. This set do not supports null value, since null is used when no mapping from base to this exists. This class is serializable if the underlying base set is serializable too.

This class is not thread-safe. Synchronizations (if wanted) are user's reponsability.

Since:
2.0
Version:
$Id: DerivedSet.java 30792 2008-06-23 19:19:58Z desruisseaux $
Author:
Martin Desruisseaux (IRD)
See Also:
Serialized Form

Field Summary
protected  java.util.Set<B> base
          The base set whose values are derived from.
 
Constructor Summary
DerivedSet(java.util.Set<B> base)
          Deprecated. Use DerivedSet(Set,Class) instead.
DerivedSet(java.util.Set<B> base, java.lang.Class<E> derivedType)
          Creates a new derived set from the specified base set.
 
Method Summary
 boolean add(E element)
          Ensures that this set contains the specified element.
protected abstract  E baseToDerived(B element)
          Transforms a value in the base set to a value in this set.
 boolean contains(java.lang.Object element)
          Returns true if this set contains the specified element.
protected abstract  B derivedToBase(E element)
          Transforms a value in this set to a value in the base set.
 java.lang.Class<E> getElementType()
          Returns the derived element type.
 boolean isEmpty()
          Returns true if this set contains no elements.
 java.util.Iterator<E> iterator()
          Returns an iterator over the elements contained in this set.
 boolean remove(java.lang.Object element)
          Removes a single instance of the specified element from this set.
 int size()
          Returns the number of elements in this set.
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, clear, containsAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
addAll, clear, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
 
Methods inherited from interface java.util.Set
addAll, clear, containsAll, retainAll, toArray, toArray
 

Field Detail

base

protected final java.util.Set<B> base
The base set whose values are derived from.

See Also:
baseToDerived(B), derivedToBase(E)
Constructor Detail

DerivedSet

public DerivedSet(java.util.Set<B> base)
Deprecated. Use DerivedSet(Set,Class) instead.

Creates a new derived set from the specified base set.

Parameters:
base - The base set.

DerivedSet

public DerivedSet(java.util.Set<B> base,
                  java.lang.Class<E> derivedType)
Creates a new derived set from the specified base set.

Parameters:
base - The base set.
derivedType - The type of elements in this derived set.
Since:
2.5
Method Detail

getElementType

public java.lang.Class<E> getElementType()
Returns the derived element type.

Specified by:
getElementType in interface CheckedCollection<E>
Returns:
The element type.
Since:
2.5

baseToDerived

protected abstract E baseToDerived(B element)
Transforms a value in the base set to a value in this set. If there is no mapping in the derived set for the specified element, then this method returns null.

Parameters:
element - A value in the base set.
Returns:
The value that this view should contains instead of element, or null.

derivedToBase

protected abstract B derivedToBase(E element)
Transforms a value in this set to a value in the base set.

Parameters:
element - A value in this set.
Returns:
The value stored in the base set.

iterator

public java.util.Iterator<E> iterator()
Returns an iterator over the elements contained in this set. The iterator will invokes baseToDerived(B) for each element.

Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface java.util.Set<E>
Specified by:
iterator in class java.util.AbstractCollection<E>
Returns:
an iterator over the elements contained in this set.

size

public int size()
Returns the number of elements in this set. The default implementation counts the number of elements returned by the iterator.

Specified by:
size in interface java.util.Collection<E>
Specified by:
size in interface java.util.Set<E>
Specified by:
size in class java.util.AbstractCollection<E>
Returns:
the number of elements in this set.

isEmpty

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

Specified by:
isEmpty in interface java.util.Collection<E>
Specified by:
isEmpty in interface java.util.Set<E>
Overrides:
isEmpty in class java.util.AbstractCollection<E>
Returns:
true if this set contains no elements.

contains

public boolean contains(java.lang.Object element)
Returns true if this set contains the specified element. The default implementation invokes base.contains(derivedToBase(element)).

Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.Set<E>
Overrides:
contains in class java.util.AbstractCollection<E>
Parameters:
element - object to be checked for containment in this set.
Returns:
true if this set contains the specified element.

add

public boolean add(E element)
            throws java.lang.UnsupportedOperationException
Ensures that this set contains the specified element. The default implementation invokes base.add(derivedToBase(element)).

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.Set<E>
Overrides:
add in class java.util.AbstractCollection<E>
Parameters:
element - element whose presence in this set is to be ensured.
Returns:
true if the set changed as a result of the call.
Throws:
java.lang.UnsupportedOperationException - if the base set doesn't supports the add operation.

remove

public boolean remove(java.lang.Object element)
               throws java.lang.UnsupportedOperationException
Removes a single instance of the specified element from this set. The default implementation invokes base.remove(derivedToBase(element)).

Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.Set<E>
Overrides:
remove in class java.util.AbstractCollection<E>
Parameters:
element - element to be removed from this set, if present.
Returns:
true if the set contained the specified element.
Throws:
java.lang.UnsupportedOperationException - if the base set doesn't supports the remove operation.


Copyright © 1996-2010 Geotools. All Rights Reserved.