Package com.globalmentor.model
Class Count
- java.lang.Object
-
- java.lang.Number
-
- com.globalmentor.java.AbstractLong<Count>
-
- com.globalmentor.model.Count
-
- All Implemented Interfaces:
CloneSupported
,java.io.Serializable
,java.lang.Cloneable
,java.lang.Comparable<Count>
public class Count extends AbstractLong<Count> implements CloneSupported
Convenience class for keeping track of a count. This is useful, for example, as a value object in a map.This class is not thread safe.
- Author:
- Garret Wilson
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Count.CounterMapEntryComparator<K>
Comparator that compares map entries of a counter map based upon the count of each entry.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object
clone()
This version is guaranteed not to throwCloneNotSupportedException
.protected long
decrement()
Decrements the counter and returns the new value.static <K> long
decrementCounterMapCount(java.util.Map<K,Count> map, K key)
Decrements the occurrence count of the given key using the given map.long
getCount()
static <K> long
getCount(java.util.Map<K,Count> map, K key)
Returns the current count of the given key in the given map.protected long
increment()
Increments the counter and returns the new value.static <K> long
incrementCounterMapCount(java.util.Map<K,Count> map, K key)
Increments the occurrence count of the given key using the given map.long
longValue()
This version delegates togetCount()
.java.lang.String
toString()
This implementation outputs the current count.-
Methods inherited from class com.globalmentor.java.AbstractLong
compareTo, doubleValue, equals, floatValue, hashCode, intValue
-
-
-
-
Method Detail
-
getCount
public long getCount()
- Returns:
- The current count.
-
longValue
public long longValue()
This version delegates togetCount()
.- Specified by:
longValue
in classjava.lang.Number
-
increment
protected long increment()
Increments the counter and returns the new value.- Returns:
- The new, incremented count value.
-
decrement
protected long decrement()
Decrements the counter and returns the new value.- Returns:
- The new, decremented count value.
-
clone
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
Description copied from interface:CloneSupported
This version is guaranteed not to throwCloneNotSupportedException
.- Specified by:
clone
in interfaceCloneSupported
- Overrides:
clone
in classjava.lang.Object
- Throws:
java.lang.CloneNotSupportedException
-
toString
public java.lang.String toString()
This implementation outputs the current count.- Overrides:
toString
in classjava.lang.Object
- See Also:
getCount()
-
incrementCounterMapCount
public static <K> long incrementCounterMapCount(java.util.Map<K,Count> map, K key)
Increments the occurrence count of the given key using the given map. In other words, the associated count for the key will be increased by one. If the key does not yet exist in the map, it will be added with a count of1
.This is a convenience method for keeping track of the count of some key in a map.
This implementation does not allow
null
counter values.- Type Parameters:
K
- The type of the key used on the map.- Parameters:
map
- The map containing the counts.key
- The key being counted.- Returns:
- The new count of the key in the map.
-
decrementCounterMapCount
public static <K> long decrementCounterMapCount(java.util.Map<K,Count> map, K key)
Decrements the occurrence count of the given key using the given map. In other words, the associated count for the key will be decreased by one. If the counter gets to zero, it will be removed from the map.This is a convenience method for keeping track of the count of some key in a map.
This implementation does not allow
null
counter values. This implementation does not support counts less than zero.- Type Parameters:
K
- The type of the key used on the map.- Parameters:
map
- The map containing the counts.key
- The key being counted.- Returns:
- The new count of the key in the map.
- Throws:
java.lang.IllegalStateException
- if the key does not exist in the map (i.e. the count is zero).
-
getCount
public static <K> long getCount(java.util.Map<K,Count> map, K key)
Returns the current count of the given key in the given map. If the key does not exist in the map, the count is considered zero.This is a convenience method for keeping track of the count of some key in a map.
This implementation does not allow
null
counter values.- Type Parameters:
K
- The type of the key used on the map.- Parameters:
map
- The map containing the counts.key
- The key being counted.- Returns:
- The count value of the counter of the key in the map.
-
-