org.jjoost.collections.maps.base
Class ScalarHashMap<K,V,N extends HashNode<N> & java.util.Map.Entry<K,V>>

java.lang.Object
  extended by org.jjoost.collections.maps.base.AbstractHashMap<K,V,N>
      extended by org.jjoost.collections.maps.base.ScalarHashMap<K,V,N>
All Implemented Interfaces:
java.io.Serializable, AnyMap<K,V>, AnyReadMap<K,V>, Map<K,V>, Function<K,V>
Direct Known Subclasses:
LockFreeHashMap, LockFreeLinkedHashMap, SerialHashMap, SerialLinkedHashMap

public class ScalarHashMap<K,V,N extends HashNode<N> & java.util.Map.Entry<K,V>>
extends AbstractHashMap<K,V,N>
implements Map<K,V>

See Also:
Serialized Form

Method Summary
 boolean add(K key, V val)
          Attempt to add the pair to the map, returning false if it could not be added, and true otherwise.
 V apply(K k)
          Returns the result of applying the function to the parameter
 Map<K,V> copy()
          Returns a copy of the map.
 V ensureAndGet(K key, Factory<? extends V> putIfNotPresent)
          Equivalent to putIfAbsent(key, putIfNotPresent), except that instead of returning the value previously associated with the key, returns the value associated with the key as the method is exiting; i.e.
 V ensureAndGet(K key, Function<? super K,? extends V> putIfNotPresent)
          Equivalent to putIfAbsent(key, putIfNotPresent.create(key)), except that
putIfNotPresent.create() is only executed if there is no key associated with the value.
 Set<java.util.Map.Entry<K,V>> entries()
          Returns a set representing all the key->value pairs in this map.
 V get(K key)
          A convenience method, equivalent to first(key)
 Set<K> keys()
          Returns a set representing all the keys in the domain of this map.
 boolean permitsDuplicateKeys()
          Returns true if a key can map to more than one value
 V put(K key, V val)
          Ensures that the provided key binds to the provided value, removing and returning the value currently associated with the key, or null if none.
 V putIfAbsent(K key, Function<? super K,? extends V> putIfNotPresent)
          Equivalent to putIfAbsent(key, putIfNotPresent.create()), except that putIfNotPresent.create() is only executed if there is no key associated with the value.
 V putIfAbsent(K key, V val)
          Attempts to bind the provided key to the provided value.
 int size()
          A convenience method, equivalent to both totalCount() and uniqueKeyCount()
 int uniqueKeyCount()
          Return an integer representing the number of unique keys in the domain of the map
 UnitarySet<V> values(K key)
          Returns a set representing the values associated with the provided key in this map.
 
Methods inherited from class org.jjoost.collections.maps.base.AbstractHashMap
clear, clearAndReturn, contains, contains, count, count, entries, first, inverse, isEmpty, list, remove, remove, removeAndReturn, removeAndReturn, removeAndReturnFirst, shrink, totalCount, values
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jjoost.collections.AnyMap
clear, clearAndReturn, inverse, remove, remove, removeAndReturn, removeAndReturn, removeAndReturnFirst, shrink, values
 
Methods inherited from interface org.jjoost.collections.AnyReadMap
contains, contains, count, count, entries, first, isEmpty, list, totalCount
 

Method Detail

entries

public Set<java.util.Map.Entry<K,V>> entries()
Description copied from interface: AnyReadMap
Returns a set representing all the key->value pairs in this map. In a ListMap this will be a MultiSet. This set should always reflect changes to the map, and changes to the set should be reflected in the map also. Otherwise, this set should behave exactly as a regular set does.

Specified by:
entries in interface AnyMap<K,V>
Specified by:
entries in interface AnyReadMap<K,V>
Specified by:
entries in interface Map<K,V>
Returns:
the entry set< k>

keys

public Set<K> keys()
Description copied from interface: AnyReadMap
Returns a set representing all the keys in the domain of this map. In a MultiMap or ListMap this will be a MultiSet. This set should always reflect changes to the map, and changes to the set should be reflected in the map also. Otherwise, this set should behave exactly as a regular set does. The key equality used by this map can be obtained from this set.

Depending on the implementation of map, this key set may contain every occurrence of equal keys provided to the map, or may contain a subset thereof with repetitions to bring the number of occurrences in line with the number provided.

Note that the put() methods on this set will always fail, because no value can be provided to update the map with.

Specified by:
keys in interface AnyMap<K,V>
Specified by:
keys in interface AnyReadMap<K,V>
Specified by:
keys in interface Map<K,V>
Returns:
the key set< k>

add

public boolean add(K key,
                   V val)
Description copied from interface: AnyMap
Attempt to add the pair to the map, returning false if it could not be added, and true otherwise. In a ListMap the return value will always be true; however in a MultiMap it will be false, and hence the map remain unmodified, if a pair whose key and value are both equal to the ones provided (as determined by the map's definitions of equality) is already present; and in a Map it will be false if a pair whose key is equal to the one provided is already present. When the value of the pair is inequal to null, this is equivalent to put(key, value) == null

Specified by:
add in interface AnyMap<K,V>
Parameters:
key - key to insert
val - value to insert
Returns:
true if the map was modified, false otherwise

put

public V put(K key,
             V val)
Description copied from interface: Map
Ensures that the provided key binds to the provided value, removing and returning the value currently associated with the key, or null if none.

Specified by:
put in interface AnyMap<K,V>
Specified by:
put in interface Map<K,V>
Parameters:
key - the key
val - the val
Returns:
the value of any maplet removed as a result of this action

putIfAbsent

public V putIfAbsent(K key,
                     V val)
Description copied from interface: Map
Attempts to bind the provided key to the provided value. If the key does not occur in the map then the value will be associated with it and null returned. If the key occurs in the map and is bound to a different value then this existing value will be returned

Specified by:
putIfAbsent in interface AnyMap<K,V>
Specified by:
putIfAbsent in interface Map<K,V>
Parameters:
key - the key
val - the val
Returns:
the value already associated with the key in the map, or null if none

ensureAndGet

public V ensureAndGet(K key,
                      Factory<? extends V> putIfNotPresent)
Description copied from interface: Map
Equivalent to putIfAbsent(key, putIfNotPresent), except that instead of returning the value previously associated with the key, returns the value associated with the key as the method is exiting; i.e. if a new value is associated with the key as a result of this method, this new value will be returned, otherwise the existing value will be

Specified by:
ensureAndGet in interface Map<K,V>
Parameters:
key - the key
putIfNotPresent - the put if not present
Returns:
the value associated with the provided key post method

ensureAndGet

public V ensureAndGet(K key,
                      Function<? super K,? extends V> putIfNotPresent)
Description copied from interface: Map
Equivalent to putIfAbsent(key, putIfNotPresent.create(key)), except that
  1. putIfNotPresent.create() is only executed if there is no key associated with the value. In concurrent maps this is not a guarantee, but a best effort, as it is possible for another thread to set a value for the key after this has executed but before the record can be inserted
  2. instead of returning the value previously associated with the key, returns the value associated with the key as the method is exiting; i.e. if a new value is associated with the key as a result of this method, this new value will be returned, otherwise the existing value will be

Specified by:
ensureAndGet in interface Map<K,V>
Parameters:
key - the key
putIfNotPresent - put if not present
Returns:
the value associated with the key post method

values

public UnitarySet<V> values(K key)
Description copied from interface: AnyReadMap
Returns a set representing the values associated with the provided key in this map. This set should always reflect changes to the map, and changes to the set should be reflected in the map also. Otherwise, this set should behave exactly as a regular set does.

Note that in a regular (scalar) map the set returned will be a UnitarySet, which contains at most one value. put() operations on such a set will override any existing value regardless of if it is equal to the one already present.

Specified by:
values in interface AnyMap<K,V>
Specified by:
values in interface AnyReadMap<K,V>
Specified by:
values in interface Map<K,V>
Returns:
the set of values mapped to by provided key

get

public V get(K key)
Description copied from interface: Map
A convenience method, equivalent to first(key)

Specified by:
get in interface Map<K,V>
Parameters:
key - the key
Returns:
the value associated with the key, or null if none

apply

public V apply(K k)
Description copied from interface: Function
Returns the result of applying the function to the parameter

Specified by:
apply in interface Function<K,V>
Parameters:
k - something of type domain
Returns:
the result of the function (something of type range)

putIfAbsent

public V putIfAbsent(K key,
                     Function<? super K,? extends V> putIfNotPresent)
Description copied from interface: Map
Equivalent to putIfAbsent(key, putIfNotPresent.create()), except that putIfNotPresent.create() is only executed if there is no key associated with the value. In concurrent maps this is not a guarantee, but a best effort, as it is possible for another thread to set a value for the key after this has executed but before the record can be inserted.

Specified by:
putIfAbsent in interface Map<K,V>
Parameters:
key - the key
putIfNotPresent - the put if not present
Returns:
the value associated with the provided key pre method

permitsDuplicateKeys

public boolean permitsDuplicateKeys()
Description copied from interface: AnyReadMap
Returns true if a key can map to more than one value

Specified by:
permitsDuplicateKeys in interface AnyReadMap<K,V>
Returns:
true, if keys can map to more than one value

size

public int size()
Description copied from interface: Map
A convenience method, equivalent to both totalCount() and uniqueKeyCount()

Specified by:
size in interface Map<K,V>
Returns:
the int

uniqueKeyCount

public int uniqueKeyCount()
Description copied from interface: AnyReadMap
Return an integer representing the number of unique keys in the domain of the map

Specified by:
uniqueKeyCount in interface AnyReadMap<K,V>
Returns:
number of unique keys

copy

public Map<K,V> copy()
Description copied from interface: AnyMap
Returns a copy of the map. Note that this method may not necessarily return an object of the same class as the one it is called upon, but will return one indistinguishable from it with respect to all method calls.

Specified by:
copy in interface AnyMap<K,V>
Specified by:
copy in interface Map<K,V>
Returns:
the any map< k, v>