Class TimeRange

  • All Implemented Interfaces:
    java.lang.Comparable<TimeRange>

    public class TimeRange
    extends java.lang.Object
    implements java.lang.Comparable<TimeRange>
    interval [min,max] of long data type

    Reference: http://www.java2s.com/Code/Java/Collections-Data-Structure/Anumericalinterval.htm

    • Constructor Detail

      • TimeRange

        public TimeRange​(long min,
                         long max)
        Initialize a closed interval [min,max].
        Parameters:
        min - the left endpoint of the closed interval
        max - the right endpoint of the closed interval
    • Method Detail

      • compareTo

        public int compareTo​(TimeRange r)
        Specified by:
        compareTo in interface java.lang.Comparable<TimeRange>
      • setMin

        public void setMin​(long min)
      • setMax

        public void setMax​(long max)
      • contains

        public boolean contains​(TimeRange r)
        Check whether this TimeRange contains r.
        Returns:
        true if the given range lies in this range, inclusively
      • contains

        public boolean contains​(long min,
                                long max)
      • contains

        public boolean contains​(long time)
      • set

        public void set​(long min,
                        long max)
        Set a closed interval [min,max].
        Parameters:
        min - the left endpoint of the closed interval
        max - the right endpoint of the closed interval
      • getMin

        public long getMin()
        Get the lower range bundary.
        Returns:
        The lower range boundary.
      • getMax

        public long getMax()
        Get the upper range boundary.
        Returns:
        The upper range boundary.
      • intersects

        public boolean intersects​(TimeRange r)
        Here are some examples.

        [1,3] does not intersect with (4,5].

        [1,3) does not intersect with (3,5].

        [1,3] does not intersect with [5,6].

        [1,3] intersects with [2,5].

        [1,3] intersects with (3,5].

        [1,3) intersects with (2,5].

        Note: this method treats [1,3] and [4,5] as two "intersected" interval even if they are not truly intersected.

        Parameters:
        r - the given time range
        Returns:
        true if the current time range "intersects" with the given time range r
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • overlaps

        public boolean overlaps​(TimeRange rhs)
        Check if two TimeRanges overlap
        Parameters:
        rhs - the given time range
        Returns:
        true if the current time range overlaps with the given time range rhs
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • setLeftClose

        public void setLeftClose​(boolean leftClose)
      • setRightClose

        public void setRightClose​(boolean rightClose)
      • getLeftClose

        public boolean getLeftClose()
      • getRightClose

        public boolean getRightClose()
      • sortAndMerge

        public static java.util.List<TimeRange> sortAndMerge​(java.util.List<TimeRange> unionCandidates)
        Return the union of the given time ranges.
        Parameters:
        unionCandidates - time ranges to be merged
        Returns:
        the union of time ranges
      • merge

        public void merge​(TimeRange rhs)
      • getRemains

        public java.util.List<TimeRange> getRemains​(java.util.List<TimeRange> timeRangesPrev)
        Get the remaining time ranges in the current ranges but not in timeRangesPrev.

        NOTE the primitive timeRange is always a closed interval [min,max] and only in this function are leftClose and rightClose changed.

        Parameters:
        timeRangesPrev - time ranges union in ascending order of the start time
        Returns:
        the remaining time ranges