java.lang.Object
de.uni_mannheim.informatik.dws.melt.matching_jena_matchers.util.Counter<T>
Type Parameters:
T - the datatype of the objects to count.

public class Counter<T> extends Object
A Counter is for counting arbitrary objects. The number per object is integer, thus the maximum frequency of an entity should not be higher than 2,147,483,647.
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • counts

      private Map<T,Counter<T>.MutableInt> counts
    • overallCount

      private long overallCount
    • mapComparator

      private Comparator<Map.Entry<T,? extends Comparable>> mapComparator
    • JSON_MAPPER

      private static final com.fasterxml.jackson.databind.ObjectMapper JSON_MAPPER
  • Constructor Details

    • Counter

      public Counter()
      Create a new counter object with a default initial capacity.
    • Counter

      public Counter(Comparator<T> elementComparator)
      Create a new counter object with a default initial capacity.
      Parameters:
      elementComparator - the comparator to use if the count is the same
    • Counter

      public Counter(Iterable<T> iterable)
      Create a new counter object from the given iterable.
      Parameters:
      iterable - any iterable of type T
    • Counter

      public Counter(Iterable<T> iterable, Comparator<T> elementComparator)
      Create a new counter object from the given iterable.
      Parameters:
      iterable - any iterable of type T
      elementComparator - the comparator to use if the count is the same
  • Method Details

    • addAll

      public void addAll(Iterable<T> iterable)
      Add a collection of elements to the counter.
      Parameters:
      iterable - the iterable to be added.
    • addAll

      public void addAll(Iterator<T> iterator)
      Add a collection of elements to the counter.
      Parameters:
      iterator - the iterator to be added.
    • addAll

      public void addAll(Counter<T> counter)
      Adds another counter to this counter. Only this counter is modified.
      Parameters:
      counter - the other counter to add to this object.
    • add

      public void add(T t)
      Add one element to the counter
      Parameters:
      t - the element to add
    • add

      public void add(T t, int howOften)
      Add this element to the counter multiple times.
      Parameters:
      t - the element to add
      howOften - how often this element should be added.
    • remove

      public boolean remove(T t)
      Removes one count of the given element from this counter (the element is still in the counter if it was added multiple times). It will remove the element only if it is present (optional operation). Returns true if this counter contained the specified element t (or equivalently, if this collection changed as a result of the call). In case you want to remove all counts of an element, then use removeAll(java.lang.Object).
      Parameters:
      t - the element to remove
      Returns:
      true if an element was removed
    • remove

      public boolean remove(T t, int howOften)
      Removes the specified number of occurances of the given element from this counter. It will remove the element only if it is present (optional operation). Returns true if this counter contained the specified element t (or equivalently, if this collection changed as a result of the call). If the element should be removed more times, than it is added, the corresponding element is removed from this counter. In case you want to remove all counts of an element, then use removeAll(java.lang.Object).
      Parameters:
      t - the element to remove
      howOften - how often this element should be removed.
      Returns:
      true if an element was removed
    • removeAll

      public boolean removeAll(T t)
      Removes the given element from this counter (even if it appears multiple times), if it is present (optional operation). Returns true if this counter contained the specified element t (or equivalently, if this collection changed as a result of the call).
      Parameters:
      t - the element to remove
      Returns:
      true if an element was removed
    • removeAll

      public boolean removeAll(Collection<T> elements)
      Removes the given element from this counter (even if it appears multiple times), if it is present (optional operation). Returns true if this counter contained the specified element t (or equivalently, if this collection changed as a result of the call).
      Parameters:
      elements - the element to remove
      Returns:
      true if the counter was changed/modified
    • substract

      public boolean substract(Counter<T> other)
      It will substract the other counter from this counter. This counter will be modified and the counts are reduced.
      Parameters:
      other - the other counter with values to be removed from this counter.
      Returns:
      true if this counter was changed/modified
    • getCount

      public int getCount(T t)
      Get the count for a specific element. If the element does not appear in this counter, 0 is returned.
      Parameters:
      t - the element for which the count should be returned
      Returns:
      how often this element occured.
    • getCount

      public long getCount()
      Get the number of how many elements which were added to this counter (this includes duplicates).
      Returns:
      how many elements are added to this list
    • getDistinctElements

      public Set<T> getDistinctElements()
      Returns the set of distinct elements.
      Returns:
      the set of distinct elements
    • getAmountOfDistinctElements

      public int getAmountOfDistinctElements()
      Returns the amount of distinct elements.
      Returns:
      amount of distinct elements
    • isEmpty

      public boolean isEmpty()
    • mostCommon

      public List<Map.Entry<T,Integer>> mostCommon(int n)
      Return a list of the n most common elements and their counts. Ordered from the most common to the least.
      Parameters:
      n - number of common elements to return
      Returns:
      list of entries
    • mostCommon

      public List<Map.Entry<T,Integer>> mostCommon()
      Return a list of the most common elements and their counts. Ordered from the most common to the least.
      Returns:
      list of entries
    • getSortedMap

      public LinkedHashMap<T,Integer> getSortedMap()
      Return a sorted map of the most common elements and their counts. Ordered from the most common to the least.
      Returns:
      sorted map
    • getSortedMap

      public LinkedHashMap<T,Integer> getSortedMap(int n)
      Return a sorted map of the most common elements (limit by n) and their counts. Ordered from the most common to the least.
      Parameters:
      n - the number of elements to put in the sorted map
      Returns:
      sorted map
    • mostCommonWithHighestCount

      public List<Map.Entry<T,Integer>> mostCommonWithHighestCount()
      Return a list of the most common elements with the highest count. Usually this is only one element but if there are multiple ones with the same count, then this function will return them all. E.g. (a, a, a, b, b, b, c) results in a list of ((a,3), (b, 3)).
      Returns:
      list of elements with their counts
    • mostCommonByPercentage

      public List<Map.Entry<T,Double>> mostCommonByPercentage(double percentage)
      Return a list of the elements with a higher or equal frequency/percentage than the given one together with their frequency.
      Parameters:
      percentage - between zero and one. 0.95 means 95 percent.
      Returns:
      list of entries with frequency
    • mostCommonElementsByPercentage

      public List<T> mostCommonElementsByPercentage(double percentage)
      Return a list of the elements with a higher or equal frequency/percentage than the given one.
      Parameters:
      percentage - between zero and one. 0.95 menas 95 percent.
      Returns:
      list of entries
    • mostCommonElementsByPercentageOrTopN

      public List<T> mostCommonElementsByPercentageOrTopN(double percentage, int topN)
      Return a list of elements which have a higher (or equal) frequency than the specified one but not more than the specified amount(parameter topN). If one of those two parameters is zero, then it will not be taken into account. Especially if both are zero, then all elements will be returned.
      Parameters:
      percentage - between zero and one. 0.95 means 95 percent.
      topN - the maximum number of common elements to return
      Returns:
      list of elements (element with highest count first)
    • mostCommonElements

      public List<T> mostCommonElements(int n)
      Return a list of the n most common elements. Ordered from the most common to the least.
      Parameters:
      n - number of common elements to return
      Returns:
      list of elements
    • mostCommonElements

      public List<T> mostCommonElements()
      Return a list ordered by their number of occurences. Ordered from the most common to the least.
      Returns:
      list of elements
    • mostCommonElement

      public T mostCommonElement()
      Return the most common element.
      Returns:
      most common element or null if counter is empty.
    • betweenFrequency

      public List<Map.Entry<T,Double>> betweenFrequency(double min, double max)
      Return a list of elements with their frequency. Only elements are returned where the frequency is between the given min and max arguments (greater or equal to min and less or equal to max). Ordered from the most common to the least.
      Parameters:
      min - the min frequency (inclusive)
      max - the max frequency (inclusive)
      Returns:
      list of elements with their frequency
    • betweenFrequencyReturningElements

      public Set<T> betweenFrequencyReturningElements(double min, double max)
      Return a set of elements where the frequency is between the given min and max arguments (greater or equal to min and less or equal to max). Ordered from the most common to the least.
      Parameters:
      min - the min frequency (inclusive)
      max - the max frequency (inclusive)
      Returns:
      set of elements
    • betweenFrequencyRelativeToTotal

      public List<Map.Entry<T,Double>> betweenFrequencyRelativeToTotal(double min, double max, long total)
      Return a list of elements with their frequency relative to the given total number. Only elements are returned where the frequency is between the given min and max arguments (greater or equal to min and less or equal to max). Ordered from the most common to the least.
      Parameters:
      min - the min frequency (inclusive)
      max - the max frequency (inclusive)
      total - the total number to compare with
      Returns:
      list of elements with their frequency
    • betweenFrequencyRelativeToTotalReturningElements

      public Set<T> betweenFrequencyRelativeToTotalReturningElements(double min, double max, long total)
      Return a set of elements where the frequency is between the given min and max arguments (greater or equal to min and less or equal to max). It calculates the frequency compared to the parameter total. Ordered from the most common to the least.
      Parameters:
      min - the min frequency (inclusive)
      max - the max frequency (inclusive)
      total - the total number to compare with
      Returns:
      set of elements
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toStringMultiline

      public String toStringMultiline(Collection<Map.Entry<T,Double>> entries)
      ToString method which returns the given entries well formatted in multiple lines.
      Parameters:
      entries - the values which should be nicely formatted
      Returns:
      a string which contains the entries information in multiple lines.
    • toStringMultiline

      public String toStringMultiline()
      ToString method which returns the counter well formatted in multiple lines to have a better overview.
      Returns:
      a string which contains the counter information in multiple lines.
    • toStringMultiline

      public String toStringMultiline(int n)
      ToString method which returns return values from functions like mostCommon(int) well formatted in multiple lines to have a better overview.
      Parameters:
      n - the number of elements to print as multiline
      Returns:
      a string which contains the counter information in multiple lines.
    • toJson

      public String toJson()
      Returns a json representation of this counter.
      Returns:
      a json representation
    • toJson

      public void toJson(File file)
      Write the counter to a file (content is json).
      Parameters:
      file - the file to use for writing the JSON content.
    • loadFromJsonString

      public static Counter<String> loadFromJsonString(String jsonRepresentation)
      Given a json representation of a counter, create a new counter. The comparator is not loaded/serialized.
      Parameters:
      jsonRepresentation - the json representation of a counter.
      Returns:
      the new counter
    • loadFromJsonFile

      public static Counter<String> loadFromJsonFile(File jsonFile)
      Load a counter instance from a json file. The comparator is not loaded/serialized.
      Parameters:
      jsonFile - the json file.
      Returns:
      the new instance
    • loadFromJsonParser

      private static Counter<String> loadFromJsonParser(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Throws:
      IOException
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object