Class IntervalStatistics<I extends Interval,K>
- Type Parameters:
I- the type of intervals to be collected, extendingIntervalK- the type of keys used to group intervals, frequently the class of the slot, i.e.Class.
- All Implemented Interfaces:
Consumer<I>
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 Summary
ConstructorsConstructorDescriptionConstructs an instance ofIntervalStatisticswith the given key extractor and interval finalizer.IntervalStatistics(Function<I, K> keyExtractor, Consumer<I> intervalFinalizer, int maxIntervalsToStore) Constructs an instance ofIntervalStatisticswith the given key extractor, interval finalizer, and maximum number of intervals to store. -
Method Summary
Modifier and TypeMethodDescriptionvoidAccepts a new interval, finalizing and processing the current interval if it exists.Returns the current interval, ornullif there is no current interval.Returns a list of all stored intervals, including the current interval if applicable.intervalSet(K key) Returns theIntervalSetfor a specific key, including the current interval if it exists and its key equals to the specified key.List<Pair<K, IntervalSet>> Returns a list of pairs containing keys (e.g., interval classes) and their correspondingIntervalSetinstances.keys()Returns a list of all keys of the intervals ever added to thisIntervalStatisticsinstance.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.Returns theIntervalSetwhich is a union of all ever added intervals.Returns the total length of intervals by keys.
-
Constructor Details
-
IntervalStatistics
Constructs an instance ofIntervalStatisticswith the given key extractor and interval finalizer. The instance created can contain effectively infinite (Integer.MAX_VALUE / 2number of intervals.- Parameters:
keyExtractor- function to extract the key from an intervalintervalFinalizer- consumer to finalize an interval
-
IntervalStatistics
public IntervalStatistics(Function<I, K> keyExtractor, Consumer<I> intervalFinalizer, int maxIntervalsToStore) Constructs an instance ofIntervalStatisticswith the given key extractor, interval finalizer, and maximum number of intervals to store.- Parameters:
keyExtractor- function to extract the key from an intervalintervalFinalizer- consumer to finalize an intervalmaxIntervalsToStore- guaranteed maximum number of intervals to store. These intervals can be retrieved byintervals()method. All earlier intervals can be erased to save memory
-
-
Method Details
-
setCheckIntervals
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-trueto enable checks,falseto disable- Returns:
- the current instance of
IntervalStatistics
-
accept
Accepts a new interval, finalizing and processing the current interval if it exists. -
totalLengthByKeys
Returns the total length of intervals by keys.- Returns:
- total length of intervals by keys
-
intervalSetsByKeys
Returns a list of pairs containing keys (e.g., interval classes) and their correspondingIntervalSetinstances.- Returns:
- a list of key-interval set pairs
-
keys
Returns a list of all keys of the intervals ever added to thisIntervalStatisticsinstance.- Returns:
- a list of keys
-
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
Returns theIntervalSetwhich is a union of all ever added intervals.- Returns:
- the union of all ever added intervals
-
intervalSet
Returns theIntervalSetfor 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
Returns the current interval, ornullif there is no current interval.- Returns:
- the current interval, or
null
-