Class Interval<E extends Comparable<E>>

java.lang.Object
io.openml.gearbox.interval.Interval<E>
Type Parameters:
E - The class of the object as the endpoint of the interval. The class must implement a meaningful equals(Object) method

public class Interval<E extends Comparable<E>> extends Object
A representation of interval bounded by comparable class.

An interval has a left bound (inclusive) and right bound (exclusive)

  • Constructor Details

    • Interval

      public Interval(E start, E end)
      Constructs a new Interval with the specified bounds.
      Parameters:
      start - The left bound of this Interval, inclusive
      end - The right bound of this Interval, inclusive
      Throws:
      NullPointerException - if either start or end is null
  • Method Details

    • merge

      public static <E extends Comparable<E>> List<Interval<E>> merge(Collection<Interval<E>> intervals)
      Takes one or more lists of intervals, and combines them into a single, sorted list with the minimum number of intervals needed to capture exactly the same instants as the original intervals.

      If any sub-intervals of the input collection abut or overlap they will be replaced with a single, combined interval.

      Taking year range as examples:

      • ['2014/2017', '2015/2020'] will combine into ['2014/2020']
      • ['2015/2016', '2016/2017'] will combine into ['2015/2017]
      • ['2015/2016', '2013/2014'] will sort into ['2013/2014', '2015/2016']
      • ['2015/2015', '2015/2016', '2012/2013'] will sort and combine to ['2012/2013', '2015/2016']
      Type Parameters:
      E - The class of the object as the endpoint of the interval. The class must implement a meaningful equals(Object) method
      Parameters:
      intervals - The collection of intervals being collated
      Returns:
      a single list of sorted intervals simplified to the smallest number of intervals able to describe the duration
      Throws:
      NullPointerException - if intervals is null
    • union

      public static <E extends Comparable<E>> List<Interval<E>> union(Collection<Interval<E>> intervals)
      Returns the union of a collection of intervals.

      This method is logically the same with merge(Collection)

      Type Parameters:
      E - The class of the object as the endpoint of the interval. The class must implement a meaningful equals(Object) method
      Parameters:
      intervals - The collection of intervals being collated
      Returns:
      a single list of sorted intervals simplified to the smallest number of intervals able to describe the duration
      Throws:
      NullPointerException - if intervals is null
    • getStart

      public E getStart()
      Returns the left bound of this Interval.
      Returns:
      the interval's starting instant
    • getEnd

      public E getEnd()
      Returns the right bound of this Interval.
      Returns:
      the interval's ending instant
    • isBefore

      public boolean isBefore(Interval<E> that)
      Returns whether this interval is completely before (exclusive on end) another specified interval.

      "2011/2016" is before "2017/2018" and "2016/2018" but not before "2014/2016" or "2014/2018"

      Parameters:
      that - The provided interval
      Returns:
      true if this interval is completely before interval or false otherwise
      Throws:
      NullPointerException - if interval is null
    • contains

      public boolean contains(Interval<E> that)
      Returns whether this interval contains the specified interval.
      Parameters:
      that - The interval to compare to
      Returns:
      true if this interval contains that interval
    • overlaps

      public boolean overlaps(Interval<E> that)
      Returns two Intervals overlap.

      Intervals are both inclusive of the start and end instants. An interval overlaps another if it shares some common part of the datetime continuum.

      Parameters:
      that - The other Interval
      Returns:
      true if the two Intervals overlap
    • abuts

      public boolean abuts(Interval<E> that)
      Returns whether two Intervals abut.

      Intervals are both inclusive of the start and end instants. An interval abuts if it starts immediately after, or ends immediately before another interval without overlap.

      Parameters:
      that - The other Interval
      Returns:
      true if the two Intervals abut
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Overrides:
      toString in class Object