Class IntervalStatistics<I extends Interval,K>

java.lang.Object
com.amalgamasimulation.core.statistics.IntervalStatistics<I,K>
Type Parameters:
I - the type of intervals to be collected, extending Interval
K - the type of keys used to group intervals, frequently the class of the slot, i.e. Class.
All Implemented Interfaces:
Consumer<I>

public class IntervalStatistics<I extends Interval,K> extends Object implements Consumer<I>
A class for collecting and analyzing statistics over intervals.

This class is used to process, categorize, and manage intervals dynamically, with a common use case being the management of simulation statistics slots. The intervals can be grouped by keys, typically, but not mandatory, by the class of the slot. For example, intervals can be categorized by the type of slot (e.g., loading slots, drilling slots) for more detailed analysis.

The class implements Consumer, allowing intervals to be fed into it dynamically during the simulation process.

Author:
Andrey Malykhanov
  • Constructor Details

    • IntervalStatistics

      public IntervalStatistics(Function<I,K> keyExtractor, Consumer<I> intervalFinalizer)
      Constructs an instance of IntervalStatistics with the given key extractor and interval finalizer. The instance created can contain effectively infinite ( Integer.MAX_VALUE / 2 number of intervals.
      Parameters:
      keyExtractor - function to extract the key from an interval
      intervalFinalizer - consumer to finalize an interval
    • IntervalStatistics

      public IntervalStatistics(Function<I,K> keyExtractor, Consumer<I> intervalFinalizer, int maxIntervalsToStore)
      Constructs an instance of IntervalStatistics with the given key extractor, interval finalizer, and maximum number of intervals to store.
      Parameters:
      keyExtractor - function to extract the key from an interval
      intervalFinalizer - consumer to finalize an interval
      maxIntervalsToStore - guaranteed maximum number of intervals to store. These intervals can be retrieved by intervals() method. All earlier intervals can be erased to save memory
  • Method Details

    • setCheckIntervals

      public IntervalStatistics<I,K> setCheckIntervals(boolean checkIntervals)
      Enables or disables checking of the newly added intervals if they intersect with any previously added intervals and throws an exception in such case.

      Note that using this checking can significantly slow down performance and is therefore only recommended for debugging and testing purposes.

      Parameters:
      checkIntervals - true to enable checks, false to disable
      Returns:
      the current instance of IntervalStatistics
    • accept

      public void accept(I interval)
      Accepts a new interval, finalizing and processing the current interval if it exists.
      Specified by:
      accept in interface Consumer<I extends Interval>
      Parameters:
      interval - the new interval to process
    • totalLengthByKeys

      public Accumulator<K> totalLengthByKeys()
      Returns the total length of intervals by keys.
      Returns:
      total length of intervals by keys
    • intervalSetsByKeys

      public List<Pair<K,IntervalSet>> intervalSetsByKeys()
      Returns a list of pairs containing keys (e.g., interval classes) and their corresponding IntervalSet instances.
      Returns:
      a list of key-interval set pairs
    • keys

      public List<K> keys()
      Returns a list of all keys of the intervals ever added to this IntervalStatistics instance.
      Returns:
      a list of keys
    • intervals

      public List<I> intervals()
      Returns a list of all stored intervals, including the current interval if applicable.

      Returned list will contain references to the original instances of intervals (i.e., statistics slots themselves).

      Number of intervals in the returned list is limited by maxIntervalsToStore parameter passed to IntervalStatistics(Function, Consumer, int) constructor.

      Returns:
      a list of intervals
    • totalIntervalSet

      public IntervalSet totalIntervalSet()
      Returns the IntervalSet which is a union of all ever added intervals.
      Returns:
      the union of all ever added intervals
    • intervalSet

      public IntervalSet intervalSet(K key)
      Returns the IntervalSet for a specific key, including the current interval if it exists and its key equals to the specified key.
      Parameters:
      key - the key for which to retrieve the interval set
      Returns:
      the interval set for the key
    • currentInterval

      public I currentInterval()
      Returns the current interval, or null if there is no current interval.
      Returns:
      the current interval, or null