org.javimmutable.collections.inorder
Class JImmutableInsertOrderMap<K,V>

java.lang.Object
  extended by org.javimmutable.collections.common.AbstractJImmutableMap<K,V>
      extended by org.javimmutable.collections.inorder.JImmutableInsertOrderMap<K,V>
Type Parameters:
K -
V -
All Implemented Interfaces:
Iterable<JImmutableMap.Entry<K,V>>, Cursorable<JImmutableMap.Entry<K,V>>, Insertable<JImmutableMap.Entry<K,V>>, JImmutableMap<K,V>, Mapped<K,V>

@Immutable
public class JImmutableInsertOrderMap<K,V>
extends AbstractJImmutableMap<K,V>

JImmutableMap implementation that allows iteration over members in the order in which they were inserted into the map. Maintains two parallel data structures, one for sorting and the other for storing entries. Gets are approximately as fast as hash map gets but updates are significantly slower. Iteration is comparable to sorted map iteration.

Use a hash or tree map whenever possible but this class performs well enough for most cases where insertion order is important to an algorithm.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.javimmutable.collections.JImmutableMap
JImmutableMap.Entry<K,V>
 
Field Summary
static JImmutableInsertOrderMap EMPTY
           
 
Method Summary
 JImmutableInsertOrderMap<K,V> assign(K key, V value)
          Sets the value associated with a specific key.
 Cursor<JImmutableMap.Entry<K,V>> cursor()
           
 JImmutableInsertOrderMap<K,V> delete(K key)
          Deletes the entry for the specified key (if any).
 JImmutableInsertOrderMap<K,V> deleteAll()
           
 Holder<V> find(K key)
          Search for a value within the map and return a Holder indicating if the value was found and, if it was found, the value itself.
 Holder<JImmutableMap.Entry<K,V>> findEntry(K key)
          Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself.
 V getValueOr(K key, V defaultValue)
          Return the value associated with key or defaultValue if no value is associated.
static
<K,V> JImmutableInsertOrderMap<K,V>
of()
           
 int size()
          Return the number of entries in the map.
 
Methods inherited from class org.javimmutable.collections.common.AbstractJImmutableMap
equals, get, getMap, hashCode, insert, isEmpty, iterator, keysCursor, toString, valuesCursor
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY

public static final JImmutableInsertOrderMap EMPTY
Method Detail

of

public static <K,V> JImmutableInsertOrderMap<K,V> of()

getValueOr

public V getValueOr(K key,
                    V defaultValue)
Description copied from interface: Mapped
Return the value associated with key or defaultValue if no value is associated. Note that if defaultValue is an acceptable value to the container then this method will be ambiguous and find() should be used instead.

Parameters:
key - identifies the value to retrieve
defaultValue - value to return if no entry exists for key
Returns:
value associated with key or defaultValue if no value is associated

find

@Nonnull
public Holder<V> find(@Nonnull
                              K key)
Description copied from interface: JImmutableMap
Search for a value within the map and return a Holder indicating if the value was found and, if it was found, the value itself. Holder allows null values to be returned unambiguously.

Parameters:
key - non-null key to search for
Returns:
empty Holder if not found, otherwise filled Holder with value

findEntry

@Nonnull
public Holder<JImmutableMap.Entry<K,V>> findEntry(@Nonnull
                                                          K key)
Description copied from interface: JImmutableMap
Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself.

Parameters:
key - non-null key to search for
Returns:
empty Holder if not found, otherwise filled Holder with Entry

assign

@Nonnull
public JImmutableInsertOrderMap<K,V> assign(@Nonnull
                                                    K key,
                                                    V value)
Description copied from interface: JImmutableMap
Sets the value associated with a specific key. Key must be non-null but value can be null. If the key already has a value in the map the old value is discarded and the new value is stored in its place. Returns a new PersistentMap reflecting any changes. The original map is always left unchanged.

Parameters:
key - non-null key
value - possibly null value
Returns:
new map reflecting the change

delete

@Nonnull
public JImmutableInsertOrderMap<K,V> delete(@Nonnull
                                                    K key)
Description copied from interface: JImmutableMap
Deletes the entry for the specified key (if any). Returns a new map if the value was deleted or the current map if the key was not contained in the map.

Parameters:
key - non-null key
Returns:
same or different map depending on whether key was removed

size

public int size()
Description copied from interface: JImmutableMap
Return the number of entries in the map.

Returns:

deleteAll

@Nonnull
public JImmutableInsertOrderMap<K,V> deleteAll()
Returns:
an equivalent collection with no values

cursor

@Nonnull
public Cursor<JImmutableMap.Entry<K,V>> cursor()


Copyright © 2014 Burton Computer Corporation. All rights reserved.