saker.util Documentation TaskDoc JavaDoc Packages
package saker.util
public final class ConcurrentPrependEntryAccumulator<K, Vimplements Iterable<Entry<K, V>>
Simplified container that can be concurrently used to add key-value entries to it.

This class can be used by multiple threads to add key-value entries to it. The implementation does not use any synchronization when inserting or retrieving entries from it. Instances of this class can be used when multiple producers need to put their results to a common container.

Good use-case for this class is when lot of threads are doing computations, and are putting their result to a common container.

Although this accumulator contains Map.Entry objects, this class doesn't work like a Map. Multiple entries can be recorded with the same key.

This class works the same way as a ConcurrentPrependAccumulator<Map.Entry<K, V>> would, but is implemented to allow users to not allocate a new Map.Entry instance for each key-value pair stored.

This class collects the elements in an prepending manner, meaning that iteration order is the reverse of the insertion order.

Iterators returned by this class are weakly consistent.

The method Map.Entry.setValue(V) on entries returned by this instance can be called to modify a recorded entry in the accumulator.

KThe key type.
VThe value type.
Constructors
public
Creates a new instance without any entries.
public
ConcurrentPrependEntryAccumulator(Map<extends K, ? extends V> map)
Creates a new instance and adds the entries from the argument map to it.
Methods
public void
add(K key, V value)
Adds an entry to this accumulator.
public void
addAll(Map<extends K, ? extends V> map)
Adds all entries in the argument map to this accumulator.
public void
addAllWithKey(K key, Iterable<extends V> values)
Adds all values in the argument iterable to this accumulator, each one of them mapped to the specified key.
public void
addAllWithValue(Iterable<extends K> keys, V value)
Adds all keys in the argument iterable to this accumulator, each one of them mapped to the specified value.
public void
Clears this accumulator.
public Iterable<Entry<K, V>>
Clears this accumulator and returns an iterable for the cleared entries.
public Iterator<Entry<K, V>>
Clears this accumulator and returns an iterator for the cleared entries.
public boolean
Checks if there are any entries in this accumulator.
public Iterator<Entry<K, V>>
Returns an iterator over elements of type T.
public Entry<K, V>
Gets the first entry in this accumulator.
public Entry<K, V>
Removes the first entry from this accumulator.
public String
Returns a string representation of the object.
Creates a new instance without any entries.
public ConcurrentPrependEntryAccumulator(Map<extends K, ? extends V> map) throws NullPointerException
Creates a new instance and adds the entries from the argument map to it.
mapThe initial map of entries.
NullPointerExceptionIf the argument is null.
public void add(K key, V value)
Adds an entry to this accumulator.
keyThe key of the entry.
valueThe value of the entry.
public void addAll(Map<extends K, ? extends V> map) throws NullPointerException
Adds all entries in the argument map to this accumulator.
mapThe map of entries.
NullPointerExceptionIf the map is null.
public void addAllWithKey(K key, Iterable<extends V> values) throws NullPointerException
Adds all values in the argument iterable to this accumulator, each one of them mapped to the specified key.

If the values iterable is empty, no entries are added.

keyThe key to map the values to.
valuesThe iterable of values.
NullPointerExceptionIf the values iterable is null.
public void addAllWithValue(Iterable<extends K> keys, V value) throws NullPointerException
Adds all keys in the argument iterable to this accumulator, each one of them mapped to the specified value.

If the keys iterable is empty, no entries are added.

keysThe iterable of keys.
valueThe value to map the keys to.
NullPointerExceptionIf the keys iterable is null.
public void clear()
Clears this accumulator.
Clears this accumulator and returns an iterable for the cleared entries.

The iterable will always iterate on the same entries, and modifications to this accumulator are not reflected on it.

The iterable for the cleared entries.
Clears this accumulator and returns an iterator for the cleared entries.
The iterator for the cleared entries.
public boolean isEmpty()
Checks if there are any entries in this accumulator.
true if the accumulator is empty.
public Iterator<Entry<K, V>> iterator()
Overridden from: Iterable
Returns an iterator over elements of type T.
an Iterator.
public Entry<K, V> peek()
Gets the first entry in this accumulator.

This method doesn't remove the entry itself, but just returns it.

The first netry, or null if the accumulator is empty.
public Entry<K, V> take()
Removes the first entry from this accumulator.
The removed entry, or null if the accumulator is empty.
public String toString()
Overridden from: Object
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 
a string representation of the object.