Package com.gengoai.collection.counter
Class BaseMultiCounter<K,V>
- java.lang.Object
-
- com.gengoai.collection.counter.BaseMultiCounter<K,V>
-
- Type Parameters:
K
- the first key VV
- the second V
- All Implemented Interfaces:
MultiCounter<K,V>
,Serializable
- Direct Known Subclasses:
ConcurrentHashMapMultiCounter
,HashMapMultiCounter
public abstract class BaseMultiCounter<K,V> extends Object implements MultiCounter<K,V>, Serializable
Implementation of a MultiCounter using a HashMaps.- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
BaseMultiCounter(Map<K,Counter<V>> backingMap)
Instantiates a new Base multi counter.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description MultiCounter<K,V>
adjustValues(DoubleUnaryOperator function)
Constructs a new multi-counter made up of counts that are adjusted using the supplied function.MultiCounter<K,V>
adjustValuesSelf(DoubleUnaryOperator function)
Adjust the values in-place using the supplied functionvoid
clear()
Clears the counterboolean
contains(K item)
Determines if (item,*) is in the counter, i.e.boolean
contains(K item1, V item2)
Determines if the pair (item1,item2) is in the counter.protected abstract Counter<V>
createCounter()
Creates a new counter.Set<Tuple3<K,V,Double>>
entries()
A set of triplies entries (key1,key2,double) making up the counterMultiCounter<K,V>
filterByFirstKey(Predicate<K> predicate)
Creates a new multi-counter containing only those entries whose first key evaluate true for the given predicateMultiCounter<K,V>
filterBySecondKey(Predicate<V> predicate)
Creates a new multi-counter containing only those entries whose second key evaluate true for the given predicateMultiCounter<K,V>
filterByValue(DoublePredicate predicate)
Creates a new multi-counter containing only those entries whose value evaluate true for the given predicateSet<K>
firstKeys()
Retrieves a set of the first keys in the counterCounter<V>
get(K firstKey)
Gets a counter of second keys associated with the first key.boolean
isEmpty()
Determines if the counter is empty or notList<Map.Entry<K,V>>
itemsByCount(boolean ascending)
Returns the items as a sorted list by their counts.Set<Map.Entry<K,V>>
keyPairs()
Retrieves the set of key pairs making up the counts in the counterMultiCounter<K,V>
merge(MultiCounter<K,V> other)
Merges the counts in one counter with this one.protected abstract MultiCounter<K,V>
newInstance()
New instance.Counter<V>
remove(K item)
Removes an item and its associated secondary keys from the counterdouble
remove(K item1, V item2)
Removes a key pair from the counter.MultiCounter<K,V>
set(K item, Counter<V> counter)
Sets the secondary keys and counts associated with a first level kekMultiCounter<K,V>
set(K item1, V item2, double amount)
Sets the value of the given key pair to the given amountint
size()
The total number of items in the counterString
toString()
Collection<Double>
values()
The values associated with the items in the counter-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.gengoai.collection.counter.MultiCounter
average, decrement, decrement, decrementAll, decrementAll, divideByKeySum, divideBySum, get, increment, increment, incrementAll, incrementAll, magnitude, maximumCount, minimumCount, removeAll, secondKeys, setIfAbsent, standardDeviation, sum, toJson, transpose, writeCsv
-
-
-
-
Method Detail
-
adjustValues
public MultiCounter<K,V> adjustValues(DoubleUnaryOperator function)
Description copied from interface:MultiCounter
Constructs a new multi-counter made up of counts that are adjusted using the supplied function.- Specified by:
adjustValues
in interfaceMultiCounter<K,V>
- Parameters:
function
- The function to use to adjust the counts- Returns:
- The new counter with adjusted counts.
-
adjustValuesSelf
public MultiCounter<K,V> adjustValuesSelf(DoubleUnaryOperator function)
Description copied from interface:MultiCounter
Adjust the values in-place using the supplied function- Specified by:
adjustValuesSelf
in interfaceMultiCounter<K,V>
- Parameters:
function
- The function to use to adjust the counts- Returns:
- this counter
-
clear
public void clear()
Description copied from interface:MultiCounter
Clears the counter- Specified by:
clear
in interfaceMultiCounter<K,V>
-
contains
public boolean contains(K item)
Description copied from interface:MultiCounter
Determines if (item,*) is in the counter, i.e. if the item is a first key in the counter- Specified by:
contains
in interfaceMultiCounter<K,V>
- Parameters:
item
- item relating to a first key to check- Returns:
- True if item is in the counter, false otherwise
-
contains
public boolean contains(K item1, V item2)
Description copied from interface:MultiCounter
Determines if the pair (item1,item2) is in the counter.- Specified by:
contains
in interfaceMultiCounter<K,V>
- Parameters:
item1
- the item 1item2
- the item 2- Returns:
- the boolean
-
createCounter
protected abstract Counter<V> createCounter()
Creates a new counter.- Returns:
- the counter
-
entries
public Set<Tuple3<K,V,Double>> entries()
Description copied from interface:MultiCounter
A set of triplies entries (key1,key2,double) making up the counter- Specified by:
entries
in interfaceMultiCounter<K,V>
- Returns:
- the set of entries
-
filterByFirstKey
public MultiCounter<K,V> filterByFirstKey(Predicate<K> predicate)
Description copied from interface:MultiCounter
Creates a new multi-counter containing only those entries whose first key evaluate true for the given predicate- Specified by:
filterByFirstKey
in interfaceMultiCounter<K,V>
- Parameters:
predicate
- the predicate to use to filter the first keys- Returns:
- A new counter containing only those entries whose first key evaluate true for the given predicate
-
filterBySecondKey
public MultiCounter<K,V> filterBySecondKey(Predicate<V> predicate)
Description copied from interface:MultiCounter
Creates a new multi-counter containing only those entries whose second key evaluate true for the given predicate- Specified by:
filterBySecondKey
in interfaceMultiCounter<K,V>
- Parameters:
predicate
- the predicate to use to filter the second keys- Returns:
- A new counter containing only those entries whose second key evaluate true for the given predicate
-
filterByValue
public MultiCounter<K,V> filterByValue(DoublePredicate predicate)
Description copied from interface:MultiCounter
Creates a new multi-counter containing only those entries whose value evaluate true for the given predicate- Specified by:
filterByValue
in interfaceMultiCounter<K,V>
- Parameters:
predicate
- the predicate to use to filter the values- Returns:
- A new counter containing only those entries whose value evaluate true for the given predicate
-
firstKeys
public Set<K> firstKeys()
Description copied from interface:MultiCounter
Retrieves a set of the first keys in the counter- Specified by:
firstKeys
in interfaceMultiCounter<K,V>
- Returns:
- The items making up the first level keys in the counter
-
get
public Counter<V> get(K firstKey)
Description copied from interface:MultiCounter
Gets a counter of second keys associated with the first key.- Specified by:
get
in interfaceMultiCounter<K,V>
- Parameters:
firstKey
- the first key whose counter we want- Returns:
- A counter of second key - double values associated with the first key
-
isEmpty
public boolean isEmpty()
Description copied from interface:MultiCounter
Determines if the counter is empty or not- Specified by:
isEmpty
in interfaceMultiCounter<K,V>
- Returns:
- True if the counter is empty
-
itemsByCount
public List<Map.Entry<K,V>> itemsByCount(boolean ascending)
Description copied from interface:MultiCounter
Returns the items as a sorted list by their counts.- Specified by:
itemsByCount
in interfaceMultiCounter<K,V>
- Parameters:
ascending
- True if the counts are sorted in ascending order, False if in descending order.- Returns:
- The sorted list of items.
-
keyPairs
public Set<Map.Entry<K,V>> keyPairs()
Description copied from interface:MultiCounter
Retrieves the set of key pairs making up the counts in the counter- Specified by:
keyPairs
in interfaceMultiCounter<K,V>
- Returns:
- A set of key pairs that make up the items in the counter
-
merge
public MultiCounter<K,V> merge(MultiCounter<K,V> other)
Description copied from interface:MultiCounter
Merges the counts in one counter with this one.- Specified by:
merge
in interfaceMultiCounter<K,V>
- Parameters:
other
- The other counter to merge.- Returns:
- the multi counter
-
newInstance
protected abstract MultiCounter<K,V> newInstance()
New instance.- Returns:
- the multi counter
-
remove
public Counter<V> remove(K item)
Description copied from interface:MultiCounter
Removes an item and its associated secondary keys from the counter- Specified by:
remove
in interfaceMultiCounter<K,V>
- Parameters:
item
- The first level key to remove- Returns:
- the counter associated with the removed item
-
remove
public double remove(K item1, V item2)
Description copied from interface:MultiCounter
Removes a key pair from the counter.- Specified by:
remove
in interfaceMultiCounter<K,V>
- Parameters:
item1
- the first keyitem2
- the second key- Returns:
- the count of the key pair
-
set
public MultiCounter<K,V> set(K item1, V item2, double amount)
Description copied from interface:MultiCounter
Sets the value of the given key pair to the given amount- Specified by:
set
in interfaceMultiCounter<K,V>
- Parameters:
item1
- the first keyitem2
- the second keyamount
- the amount to set the key pair to- Returns:
- This MultiCounter (for fluent design)
-
set
public MultiCounter<K,V> set(K item, Counter<V> counter)
Description copied from interface:MultiCounter
Sets the secondary keys and counts associated with a first level kek- Specified by:
set
in interfaceMultiCounter<K,V>
- Parameters:
item
- the first keycounter
- the counter of secondary keys and counts- Returns:
- This MultiCounter (for fluent design)
-
size
public int size()
Description copied from interface:MultiCounter
The total number of items in the counter- Specified by:
size
in interfaceMultiCounter<K,V>
- Returns:
- The number of items in the counter
-
values
public Collection<Double> values()
Description copied from interface:MultiCounter
The values associated with the items in the counter- Specified by:
values
in interfaceMultiCounter<K,V>
- Returns:
- The values of the items in the counter.
-
-