org.apache.commons.collections.bidimap
Class DualTreeBidiMap<K,V>

java.lang.Object
  extended by org.apache.commons.collections.bidimap.AbstractDualBidiMap<K,V>
      extended by org.apache.commons.collections.bidimap.DualTreeBidiMap<K,V>
All Implemented Interfaces:
Serializable, Map<K,V>, SortedMap<K,V>, BidiMap<K,V>, IterableMap<K,V>, OrderedBidiMap<K,V>, OrderedMap<K,V>, SortedBidiMap<K,V>

public class DualTreeBidiMap<K,V>
extends AbstractDualBidiMap<K,V>
implements SortedBidiMap<K,V>, Serializable

Implementation of BidiMap that uses two TreeMap instances.

The setValue() method on iterators will succeed only if the new value being set is not already in the bidimap.

When considering whether to use this class, the TreeBidiMap class should also be considered. It implements the interface using a dedicated design, and does not store each object twice, which can save on memory use.

NOTE: From Commons Collections 3.1, all subclasses will use TreeMap and the flawed createMap method is ignored.

Since:
Commons Collections 3.0
Version:
$Id: DualTreeBidiMap.java,v 1.1.1.1 2005/05/23 04:33:41 pents90 Exp $
Author:
Matthew Hawthorne, Matt Hall, John Watkinson, Stephen Colebourne
See Also:
Serialized Form

Nested Class Summary
protected static class DualTreeBidiMap.BidiOrderedMapIterator<K,V>
          Inner class MapIterator.
protected static class DualTreeBidiMap.ViewMap<K,V>
          Internal sorted map view.
 
Nested classes/interfaces inherited from class org.apache.commons.collections.bidimap.AbstractDualBidiMap
AbstractDualBidiMap.BidiMapIterator<K,V>, AbstractDualBidiMap.EntrySet<K,V>, AbstractDualBidiMap.EntrySetIterator<K,V>, AbstractDualBidiMap.KeySet<K,V>, AbstractDualBidiMap.KeySetIterator<K,V>, AbstractDualBidiMap.MapEntry<K,V>, AbstractDualBidiMap.Values<K,V>, AbstractDualBidiMap.ValuesIterator<K,V>, AbstractDualBidiMap.View<K,V,E>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  Comparator<? super K> comparator
          The comparator to use
 
Fields inherited from class org.apache.commons.collections.bidimap.AbstractDualBidiMap
entrySet, forwardMap, inverseBidiMap, inverseMap, keySet, values
 
Constructor Summary
  DualTreeBidiMap()
          Creates an empty DualTreeBidiMap
  DualTreeBidiMap(Comparator<? super K> comparator, Comparator<? super V> inverseComparator)
          Constructs a DualTreeBidiMap using the specified Comparators.
  DualTreeBidiMap(Map<? extends K,? extends V> map)
          Constructs a DualTreeBidiMap and copies the mappings from specified Map.
protected DualTreeBidiMap(Map<K,V> normalMap, Map<V,K> reverseMap, BidiMap<V,K> inverseBidiMap)
          Constructs a DualTreeBidiMap that decorates the specified maps.
 
Method Summary
 Comparator<? super K> comparator()
           
protected
<K,V> BidiMap<K,V>
createBidiMap(Map<K,V> normalMap, Map<V,K> reverseMap, BidiMap<V,K> inverseMap)
          Creates a new instance of this object.
static
<E> DualTreeBidiMap<E,E>
createTwoWayBidiMap(Comparator<? super E> comparator)
           
 K firstKey()
          Gets the first key currently in this map.
 SortedMap<K,V> headMap(K toKey)
           
 OrderedBidiMap<V,K> inverseOrderedBidiMap()
          Gets a view of this map where the keys and values are reversed.
 SortedBidiMap<V,K> inverseSortedBidiMap()
          Gets a view of this map where the keys and values are reversed.
 K lastKey()
          Gets the last key currently in this map.
 K nextKey(K key)
          Gets the next key after the one specified.
 OrderedMapIterator<K,V> orderedMapIterator()
          Obtains an ordered map iterator.
 K previousKey(K key)
          Gets the previous key before the one specified.
 SortedMap<K,V> subMap(K fromKey, K toKey)
           
 SortedMap<K,V> tailMap(K fromKey)
           
 
Methods inherited from class org.apache.commons.collections.bidimap.AbstractDualBidiMap
clear, containsKey, containsValue, createEntrySetIterator, createKeySetIterator, createMap, createValuesIterator, entrySet, equals, get, getKey, hashCode, inverseBidiMap, isEmpty, keySet, mapIterator, put, putAll, remove, removeValue, size, toString, values
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.commons.collections.SortedBidiMap
inverseBidiMap
 
Methods inherited from interface org.apache.commons.collections.BidiMap
getKey, mapIterator, put, removeValue
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, putAll, remove, size, values
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, putAll, remove, size, values
 

Field Detail

comparator

protected final Comparator<? super K> comparator
The comparator to use

Constructor Detail

DualTreeBidiMap

public DualTreeBidiMap()
Creates an empty DualTreeBidiMap


DualTreeBidiMap

public DualTreeBidiMap(Map<? extends K,? extends V> map)
Constructs a DualTreeBidiMap and copies the mappings from specified Map.

Parameters:
map - the map whose mappings are to be placed in this map

DualTreeBidiMap

public DualTreeBidiMap(Comparator<? super K> comparator,
                       Comparator<? super V> inverseComparator)
Constructs a DualTreeBidiMap using the specified Comparators.

Parameters:
comparator - the Comparator

DualTreeBidiMap

protected DualTreeBidiMap(Map<K,V> normalMap,
                          Map<V,K> reverseMap,
                          BidiMap<V,K> inverseBidiMap)
Constructs a DualTreeBidiMap that decorates the specified maps.

Parameters:
normalMap - the normal direction map
reverseMap - the reverse direction map
inverseBidiMap - the inverse BidiMap
Method Detail

createTwoWayBidiMap

public static <E> DualTreeBidiMap<E,E> createTwoWayBidiMap(Comparator<? super E> comparator)

createBidiMap

protected <K,V> BidiMap<K,V> createBidiMap(Map<K,V> normalMap,
                                           Map<V,K> reverseMap,
                                           BidiMap<V,K> inverseMap)
Creates a new instance of this object.

Specified by:
createBidiMap in class AbstractDualBidiMap<K,V>
Parameters:
normalMap - the normal direction map
reverseMap - the reverse direction map
inverseMap - the inverse BidiMap
Returns:
new bidi map

comparator

public Comparator<? super K> comparator()
Specified by:
comparator in interface SortedMap<K,V>

firstKey

public K firstKey()
Description copied from interface: OrderedMap
Gets the first key currently in this map.

Specified by:
firstKey in interface SortedMap<K,V>
Specified by:
firstKey in interface OrderedMap<K,V>
Returns:
the first key currently in this map

lastKey

public K lastKey()
Description copied from interface: OrderedMap
Gets the last key currently in this map.

Specified by:
lastKey in interface SortedMap<K,V>
Specified by:
lastKey in interface OrderedMap<K,V>
Returns:
the last key currently in this map

nextKey

public K nextKey(K key)
Description copied from interface: OrderedMap
Gets the next key after the one specified.

Specified by:
nextKey in interface OrderedMap<K,V>
Parameters:
key - the key to search for next from
Returns:
the next key, null if no match or at end

previousKey

public K previousKey(K key)
Description copied from interface: OrderedMap
Gets the previous key before the one specified.

Specified by:
previousKey in interface OrderedMap<K,V>
Parameters:
key - the key to search for previous from
Returns:
the previous key, null if no match or at start

orderedMapIterator

public OrderedMapIterator<K,V> orderedMapIterator()
Obtains an ordered map iterator.

This implementation copies the elements to an ArrayList in order to provide the forward/backward behaviour.

Specified by:
orderedMapIterator in interface OrderedMap<K,V>
Returns:
a new ordered map iterator

inverseSortedBidiMap

public SortedBidiMap<V,K> inverseSortedBidiMap()
Description copied from interface: SortedBidiMap
Gets a view of this map where the keys and values are reversed.

Changes to one map will be visible in the other and vice versa. This enables both directions of the map to be accessed as a SortedMap.

Implementations should seek to avoid creating a new object every time this method is called. See AbstractMap.values() etc. Calling this method on the inverse map should return the original.

The inverse map returned by inverseBidiMap() should be the same object as returned by this method.

Specified by:
inverseSortedBidiMap in interface SortedBidiMap<K,V>
Returns:
an inverted bidirectional map

inverseOrderedBidiMap

public OrderedBidiMap<V,K> inverseOrderedBidiMap()
Description copied from interface: OrderedBidiMap
Gets a view of this map where the keys and values are reversed.

Changes to one map will be visible in the other and vice versa. This enables both directions of the map to be accessed equally.

Implementations should seek to avoid creating a new object every time this method is called. See AbstractMap.values() etc. Calling this method on the inverse map should return the original.

Specified by:
inverseOrderedBidiMap in interface OrderedBidiMap<K,V>
Returns:
an inverted bidirectional map

headMap

public SortedMap<K,V> headMap(K toKey)
Specified by:
headMap in interface SortedMap<K,V>

tailMap

public SortedMap<K,V> tailMap(K fromKey)
Specified by:
tailMap in interface SortedMap<K,V>

subMap

public SortedMap<K,V> subMap(K fromKey,
                             K toKey)
Specified by:
subMap in interface SortedMap<K,V>


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.