Class 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.
    • Constructor Summary

      Constructors 
      Constructor Description
      Count()
      Default constructor with a count of zero.
      Count​(long count)
      Count constructor.
    • 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 throw CloneNotSupportedException.
      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 to getCount().
      java.lang.String toString()
      This implementation outputs the current count.
      • Methods inherited from class java.lang.Number

        byteValue, shortValue
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Count

        public Count()
        Default constructor with a count of zero.
      • Count

        public Count​(long count)
        Count constructor.
        Parameters:
        count - The current count.
    • Method Detail

      • getCount

        public long getCount()
        Returns:
        The current count.
      • longValue

        public long longValue()
        This version delegates to getCount().
        Specified by:
        longValue in class java.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 throw CloneNotSupportedException.
        Specified by:
        clone in interface CloneSupported
        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException
      • toString

        public java.lang.String toString()
        This implementation outputs the current count.
        Overrides:
        toString in class java.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 of 1.

        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.