Class IntervalCollection<T>

java.lang.Object
java.util.AbstractCollection<ChronoInterval<T>>
net.time4j.range.IntervalCollection<T>
Type Parameters:
T - generic type characterizing the associated time axis
All Implemented Interfaces:
Serializable, Iterable<ChronoInterval<T>>, Collection<ChronoInterval<T>>

public abstract class IntervalCollection<T> extends AbstractCollection<ChronoInterval<T>> implements Serializable

Represents a sorted list of arbitrary possibly overlapping intervals (no series) whose boundaries can be changed in many ways.

Any instance can first be achieved by calling one of the static onXYZAxis()-methods and then be filled with any count of typed intervals via plus(...)-methods. All intervals are stored with closed start if they have finite start. Empty intervals are never stored.

Since:
2.0
Author:
Meno Hochschild
See Also:
DateInterval.comparator(), ClockInterval.comparator(), TimestampInterval.comparator(), MomentInterval.comparator()
  • Method Details

    • onDateAxis

      public static IntervalCollection<PlainDate> onDateAxis()

      Yields an empty instance on the date axis.

      Returns:
      empty IntervalCollection for date intervals
      Since:
      2.0
    • onClockAxis

      public static IntervalCollection<PlainTime> onClockAxis()

      Yields an empty instance on the walltime axis.

      Returns:
      empty IntervalCollection for clock intervals
      Since:
      2.0
    • onTimestampAxis

      public static IntervalCollection<PlainTimestamp> onTimestampAxis()

      Yields an empty instance on the timestamp axis.

      Returns:
      empty IntervalCollection for timestamp intervals
      Since:
      2.0
    • onMomentAxis

      public static IntervalCollection<Moment> onMomentAxis()

      Yields an empty instance on the UTC-axis.

      Returns:
      empty IntervalCollection for moment intervals
      Since:
      2.0
    • onTraditionalTimeLine

      public static IntervalCollection<Date> onTraditionalTimeLine()

      Yields an empty instance for intervals with the component type java.util.Date.

      Returns:
      empty IntervalCollection for old java.util.Date-intervals
      Since:
      3.25/4.21
    • onInstantTimeLine

      public static IntervalCollection<Instant> onInstantTimeLine()

      Yields an empty instance for intervals with the component type java.time.Instant.

      Returns:
      empty IntervalCollection for java.time.Instant-intervals
      Since:
      4.21
    • on

      public static <T> IntervalCollection<T> on(TimeLine<T> timeLine)

      Yields an empty instance for intervals on given timeline.

      Type Parameters:
      T - generic type of time points
      Parameters:
      timeLine - the associated timeline
      Returns:
      empty generic IntervalCollection
      Since:
      5.0
      See Also:
      TimeAxis, CalendarFamily.getTimeLine(String), CalendarFamily.getTimeLine(net.time4j.engine.VariantSource), CalendarYear.timeline(), CalendarQuarter.timeline(), CalendarMonth.timeline(), CalendarWeek.timeline()
    • getIntervals

      public List<ChronoInterval<T>> getIntervals()

      Returns all appended intervals.

      Note that all contained finite intervals have each a closed start.

      Returns:
      unmodifiable list of intervals sorted by start and then by length
      Since:
      2.0
    • iterator

      public Iterator<ChronoInterval<T>> iterator()

      Obtains an interval iterator.

      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in class AbstractCollection<ChronoInterval<T>>
      Returns:
      Iterator
      Since:
      3.35/4.30
    • size

      public int size()

      Obtains the count of stored intervals.

      Specified by:
      size in interface Collection<T>
      Specified by:
      size in class AbstractCollection<ChronoInterval<T>>
      Returns:
      int
      Since:
      3.35/4.30
    • isEmpty

      public boolean isEmpty()

      Gives an answer if this instance contains no intervals.

      Specified by:
      isEmpty in interface Collection<T>
      Overrides:
      isEmpty in class AbstractCollection<ChronoInterval<T>>
      Returns:
      true if there are no intervals else false
      Since:
      2.0
    • isDisjunct

      public boolean isDisjunct()

      Queries if there is no intersection of intervals.

      Returns:
      true if there is no intersection else false
      Since:
      3.24/4.20
    • encloses

      public boolean encloses(T temporal)

      Queries if any interval of this collection contains given temporal.

      Parameters:
      temporal - time point to be queried
      Returns:
      true if given time point belongs to any interval of this collection else false
      Since:
      3.35/4.30
    • contains

      public boolean contains(ChronoInterval<T> interval)

      Queries if given interval is stored in this collection.

      Parameters:
      interval - the interval to be checked
      Returns:
      boolean
      Since:
      3.35/4.30
    • getMinimum

      public T getMinimum()

      Returns the overall minimum of this interval collection.

      The minimum is always inclusive, if finite.

      Returns:
      lower limit of this instance or null if infinite
      Throws:
      NoSuchElementException - if there are no intervals
      Since:
      2.0
      See Also:
      isEmpty()
    • getMaximum

      public T getMaximum()

      Returns the overall maximum of this interval collection.

      The maximum is always inclusive, if finite.

      Returns:
      upper limit of this instance or null if infinite
      Throws:
      NoSuchElementException - if there are no intervals
      Since:
      2.0
      See Also:
      isEmpty()
    • getRange

      public ChronoInterval<T> getRange()

      Yields the full min-max-range of this instance.

      Returns:
      minimum range interval spanning over all enclosed intervals
      Since:
      3.7/4.5
    • plus

      public IntervalCollection<T> plus(ChronoInterval<T> interval)

      Adds the given interval to this interval collection.

      An empty interval will be ignored.

      Parameters:
      interval - the new interval to be added
      Returns:
      new IntervalCollection-instance containing a sum of the own intervals and the given one while this instance remains unaffected
      Throws:
      IllegalArgumentException - if given interval is finite and has open start which cannot be adjusted to one with closed start
      Since:
      2.0
    • plus

      public IntervalCollection<T> plus(Collection<? extends ChronoInterval<T>> intervals)

      Adds the given intervals to this interval collection.

      Empty intervals will be ignored.

      Parameters:
      intervals - the new intervals to be added
      Returns:
      new IntervalCollection-instance containing a sum of the own intervals and the given one while this instance remains unaffected
      Throws:
      IllegalArgumentException - if given list contains a finite interval with open start which cannot be adjusted to one with closed start
      Since:
      3.35/4.30
    • plus

      public IntervalCollection<T> plus(IntervalCollection<T> other)

      Equivalent to plus(other.getIntervals()).

      Parameters:
      other - another interval collection whose intervals are to be added to this instance
      Returns:
      new interval collection containing the intervals of this instance and the argument
      Throws:
      IllegalArgumentException - if given collection contains a finite interval with open start which cannot be adjusted to one with closed start
      Since:
      3.7/4.5
    • minus

      public IntervalCollection<T> minus(ChronoInterval<T> interval)

      Subtracts all timepoints of given interval from this interval collection.

      Parameters:
      interval - other interval to be subtracted from this
      Returns:
      new interval collection containing all timepoints of this instance excluding those of given interval
      Throws:
      IllegalArgumentException - if given interval is finite and has open start which cannot be adjusted to one with closed start
      Since:
      2.2
    • minus

      public IntervalCollection<T> minus(Collection<? extends ChronoInterval<T>> intervals)

      Subtracts all timepoints of given intervals from this interval collection.

      Parameters:
      intervals - collection of intervals to be subtracted
      Returns:
      new interval collection containing all timepoints of this instance excluding those of given intervals
      Throws:
      IllegalArgumentException - if given list contains a finite interval with open start which cannot be adjusted to one with closed start
      Since:
      3.35/4.30
    • minus

      public IntervalCollection<T> minus(IntervalCollection<T> other)

      Equivalent to minus(other.getIntervals()).

      Parameters:
      other - another interval collection whose intervals are to be subtracted from this instance
      Returns:
      new interval collection containing all timepoints of this instance excluding those of argument
      Throws:
      IllegalArgumentException - if given collection contains a finite interval with open start which cannot be adjusted to one with closed start
      Since:
      3.7/4.5
    • withTimeWindow

      public IntervalCollection<T> withTimeWindow(ChronoInterval<T> timeWindow)

      Determines a filtered version of this interval collection within given range.

      Parameters:
      timeWindow - time window filter
      Returns:
      new interval collection containing only timepoints within given range
      Throws:
      IllegalArgumentException - if given window is finite and has open start which cannot be adjusted to one with closed start
      Since:
      2.1
    • withComplement

      public IntervalCollection<T> withComplement(ChronoInterval<T> timeWindow)

      Determines the complement of this interval collection within given range.

      Parameters:
      timeWindow - time window filter
      Returns:
      new interval collection containing all timepoints within given range which do not belong to this instance
      Throws:
      IllegalArgumentException - if given window is finite and has open start which cannot be adjusted to one with closed start
      Since:
      2.1
    • withGaps

      public IntervalCollection<T> withGaps()

      Searches for all gaps with time points which are not covered by any interval of this instance.

      withGaps

      Returns:
      new interval collection containing the inner gaps between the own intervals while this instance remains unaffected
      Since:
      2.0
    • withBlocks

      public IntervalCollection<T> withBlocks()

      Combines all intervals to disjunct blocks which neither overlap nor meet each other.

      Any overlapping or abutting intervals will be merged to one block. If the interval boundaries are still to be kept then consider withSplits() instead.

      withBlocks

      Returns:
      new interval collection containing disjunct merged blocks while this instance remains unaffected
      Since:
      2.0
    • withSplits

      public IntervalCollection<T> withSplits()

      Combines all intervals to disjunct blocks which never overlap but still might meet each other.

      Similar to withBlocks() but all boundaries will be temporally conserved.

      withSplits

      Returns:
      new interval collection containing disjunct splitted sections while this instance remains unaffected
      Since:
      3.24/4.20
    • withIntersection

      public IntervalCollection<T> withIntersection()

      Determines the intersection of all contained intervals.

      Note: This instance remains unaffected as specified for immutable classes.

      withIntersection

      Returns:
      new interval collection containing the intersection interval, maybe empty (if there is no intersection)
      Since:
      2.0
    • union

      public IntervalCollection<T> union(IntervalCollection<T> other)

      Equivalent to plus(other).withBlocks().

      Note: Before version 3.7/4.5 the behaviour was just giving an unmerged collection.

      Parameters:
      other - another interval collection whose intervals are to be added to this instance
      Returns:
      new merged interval collection with disjunct blocks
      Since:
      2.0
    • intersect

      public IntervalCollection<T> intersect(IntervalCollection<T> other)

      Determines the intersection.

      Parameters:
      other - another interval collection
      Returns:
      new interval collection with disjunct blocks containing all time points in both interval collections
      Since:
      3.8/4.5
    • xor

      public IntervalCollection<T> xor(IntervalCollection<T> other)

      Determines the difference which holds all time points either in this xor the other collection.

      Parameters:
      other - another interval collection
      Returns:
      new interval collection with disjunct blocks containing all time points which are in only one of both interval collections
      Since:
      3.8/4.5
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface Collection<T>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<T>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()

      For debugging purposes.

      Overrides:
      toString in class AbstractCollection<ChronoInterval<T>>
      Returns:
      String