Package com.gengoai.collection.counter
Class BaseMapCounter<T>
- java.lang.Object
-
- com.gengoai.collection.counter.BaseMapCounter<T>
-
- Type Parameters:
T
- the component type of the counter
- All Implemented Interfaces:
Counter<T>
,Copyable<Counter<T>>
,Serializable
- Direct Known Subclasses:
ConcurrentHashMapCounter
,HashMapCounter
public abstract class BaseMapCounter<T> extends Object implements Counter<T>, Serializable
Abstract implementation of a counter backed by aMap
.- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
BaseMapCounter(Map<T,Double> backingMap)
Instantiates a new map counter.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Counter<T>
adjustValues(DoubleUnaryOperator function)
Constructs a new counter made up of counts that are adjusted using the supplied function.Counter<T>
adjustValuesSelf(DoubleUnaryOperator function)
Adjust the values in-place using the supplied functionMap<T,Double>
asMap()
Provides a map view of this counterCounter<T>
bottomN(int n)
Creates a new counter containing the N items with lowest valuesvoid
clear()
Clears the counterboolean
contains(T item)
Determines if the item is in the counterCounter<T>
copy()
Makes a copy of this object.Counter<T>
divideBySum()
Divides the values in the counter by the sum and sets the sum to 1.0Set<Map.Entry<T,Double>>
entries()
A set of object - double entries making up the counterCounter<T>
filterByKey(Predicate<? super T> predicate)
Creates a new counter containing only those items that evaluate true for the given predicateCounter<T>
filterByValue(DoublePredicate doublePredicate)
Creates a new counter containing only those items whose value evaluate true for the given predicatedouble
get(T item)
Returns the value for the given itemCounter<T>
increment(T item, double amount)
Increments the count of the item by a given amountboolean
isEmpty()
Determines if the counter is empty or notSet<T>
items()
The items in the counterList<T>
itemsByCount(boolean ascending)
Returns the items as a sorted list by their counts.<R> Counter<R>
mapKeys(Function<? super T,? extends R> function)
Creates a new counter by mapping the items of this counter using the supplied functionCounter<T>
merge(Counter<? extends T> other)
Merges the counts in one counter with this one.Counter<T>
merge(Map<? extends T,? extends Number> other)
Merges the counts in a map with this counter.protected abstract <R> Counter<R>
newInstance()
New instance counter.double
remove(T item)
Removes an item from the counterCounter<T>
removeAll(Iterable<T> items)
Removes all the given items from the counterCounter<T>
set(T item, double count)
Sets the value of an item in the counterint
size()
The total number of items in the counterdouble
sum()
The sum of values in the counterCounter<T>
topN(int n)
Creates a new counter containing the N items with highest valuesString
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.Counter
average, decrement, decrement, decrementAll, decrementAll, forEach, increment, incrementAll, incrementAll, magnitude, max, maximumCount, min, minimumCount, sample, sample, standardDeviation, writeCsv
-
-
-
-
Method Detail
-
adjustValues
public Counter<T> adjustValues(DoubleUnaryOperator function)
Description copied from interface:Counter
Constructs a new counter made up of counts that are adjusted using the supplied function.- Specified by:
adjustValues
in interfaceCounter<T>
- Parameters:
function
- The function to use to adjust the counts- Returns:
- The new counter with adjusted counts.
-
adjustValuesSelf
public Counter<T> adjustValuesSelf(DoubleUnaryOperator function)
Description copied from interface:Counter
Adjust the values in-place using the supplied function- Specified by:
adjustValuesSelf
in interfaceCounter<T>
- Parameters:
function
- The function to use to adjust the counts- Returns:
- this counter
-
asMap
public Map<T,Double> asMap()
Description copied from interface:Counter
Provides a map view of this counter
-
bottomN
public Counter<T> bottomN(int n)
Description copied from interface:Counter
Creates a new counter containing the N items with lowest values
-
clear
public void clear()
Description copied from interface:Counter
Clears the counter
-
contains
public boolean contains(T item)
Description copied from interface:Counter
Determines if the item is in the counter
-
copy
public Counter<T> copy()
Description copied from interface:Copyable
Makes a copy of this object.
-
divideBySum
public Counter<T> divideBySum()
Description copied from interface:Counter
Divides the values in the counter by the sum and sets the sum to 1.0- Specified by:
divideBySum
in interfaceCounter<T>
- Returns:
- the counter
-
entries
public Set<Map.Entry<T,Double>> entries()
Description copied from interface:Counter
A set of object - double entries making up the counter
-
filterByKey
public Counter<T> filterByKey(Predicate<? super T> predicate)
Description copied from interface:Counter
Creates a new counter containing only those items that evaluate true for the given predicate- Specified by:
filterByKey
in interfaceCounter<T>
- Parameters:
predicate
- the predicate to use to filter the keys- Returns:
- A new counter containing only those items that evaluate true for the given predicate
-
filterByValue
public Counter<T> filterByValue(DoublePredicate doublePredicate)
Description copied from interface:Counter
Creates a new counter containing only those items whose value evaluate true for the given predicate- Specified by:
filterByValue
in interfaceCounter<T>
- Parameters:
doublePredicate
- the predicate to use to filter the values- Returns:
- A new counter containing only those items whose value evaluate true for the given predicate
-
get
public double get(T item)
Description copied from interface:Counter
Returns the value for the given item
-
increment
public Counter<T> increment(T item, double amount)
Description copied from interface:Counter
Increments the count of the item by a given amount
-
isEmpty
public boolean isEmpty()
Description copied from interface:Counter
Determines if the counter is empty or not
-
itemsByCount
public List<T> itemsByCount(boolean ascending)
Description copied from interface:Counter
Returns the items as a sorted list by their counts.- Specified by:
itemsByCount
in interfaceCounter<T>
- Parameters:
ascending
- True if the counts are sorted in ascending order, False if in descending order.- Returns:
- The sorted list of items.
-
mapKeys
public <R> Counter<R> mapKeys(Function<? super T,? extends R> function)
Description copied from interface:Counter
Creates a new counter by mapping the items of this counter using the supplied function
-
merge
public Counter<T> merge(Counter<? extends T> other)
Description copied from interface:Counter
Merges the counts in one counter with this one.
-
merge
public Counter<T> merge(Map<? extends T,? extends Number> other)
Description copied from interface:Counter
Merges the counts in a map with this counter.
-
newInstance
protected abstract <R> Counter<R> newInstance()
New instance counter.- Type Parameters:
R
- the type parameter- Returns:
- the counter
-
remove
public double remove(T item)
Description copied from interface:Counter
Removes an item from the counter
-
removeAll
public Counter<T> removeAll(Iterable<T> items)
Description copied from interface:Counter
Removes all the given items from the counter
-
set
public Counter<T> set(T item, double count)
Description copied from interface:Counter
Sets the value of an item in the counter
-
size
public int size()
Description copied from interface:Counter
The total number of items in the counter
-
sum
public double sum()
Description copied from interface:Counter
The sum of values in the counter
-
topN
public Counter<T> topN(int n)
Description copied from interface:Counter
Creates a new counter containing the N items with highest values
-
-