Interface RangeSet<C extends Comparable>

  • All Known Implementing Classes:
    ImmutableRangeSet, TreeRangeSet

    @Beta
    @DoNotMock("Use ImmutableRangeSet or TreeRangeSet")
    @GwtIncompatible
    public interface RangeSet<C extends Comparable>
    A set comprising zero or more nonempty, disconnected ranges of type C.

    Implementations that choose to support the add(Range) operation are required to ignore empty ranges and coalesce connected ranges. For example:

    
     RangeSet<Integer> rangeSet = TreeRangeSet.create();
     rangeSet.add(Range.closed(1, 10)); // {[1, 10]}
     rangeSet.add(Range.closedOpen(11, 15)); // disconnected range; {[1, 10], [11, 15)}
     rangeSet.add(Range.closedOpen(15, 20)); // connected range; {[1, 10], [11, 20)}
     rangeSet.add(Range.openClosed(0, 0)); // empty range; {[1, 10], [11, 20)}
     rangeSet.remove(Range.open(5, 10)); // splits [1, 10]; {[1, 5], [10, 10], [11, 20)}
     

    Note that the behavior of Range.isEmpty() and Range.isConnected(Range) may not be as expected on discrete ranges. See the Javadoc of those methods for details.

    For a Set whose contents are specified by a Range, see ContiguousSet.

    See the Guava User Guide article on RangeSets.

    Since:
    14.0
    Author:
    Kevin Bourrillion, Louis Wasserman
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void add(Range<C> range)
      Adds the specified range to this RangeSet (optional operation).
      void addAll(RangeSet<C> other)
      Adds all of the ranges from the specified range set to this range set (optional operation).
      default void addAll(Iterable<Range<C>> ranges)
      Adds all of the specified ranges to this range set (optional operation).
      Set<Range<C>> asDescendingSetOfRanges()
      Returns a descending view of the disconnected ranges that make up this range set.
      Set<Range<C>> asRanges()
      Returns a view of the disconnected ranges that make up this range set.
      void clear()
      Removes all ranges from this RangeSet (optional operation).
      RangeSet<C> complement()
      Returns a view of the complement of this RangeSet.
      boolean contains(C value)
      Determines whether any of this range set's member ranges contains value.
      boolean encloses(Range<C> otherRange)
      Returns true if there exists a member range in this range set which encloses the specified range.
      boolean enclosesAll(RangeSet<C> other)
      Returns true if for each member range in other there exists a member range in this range set which encloses it.
      default boolean enclosesAll(Iterable<Range<C>> other)
      Returns true if for each range in other there exists a member range in this range set which encloses it.
      boolean equals(@Nullable Object obj)
      Returns true if obj is another RangeSet that contains the same ranges according to Range.equals(Object).
      int hashCode()
      Returns asRanges().hashCode().
      boolean intersects(Range<C> otherRange)
      Returns true if there exists a non-empty range enclosed by both a member range in this range set and the specified range.
      boolean isEmpty()
      Returns true if this range set contains no ranges.
      Range<C> rangeContaining(C value)
      Returns the unique range from this range set that contains value, or null if this range set does not contain value.
      void remove(Range<C> range)
      Removes the specified range from this RangeSet (optional operation).
      void removeAll(RangeSet<C> other)
      Removes all of the ranges from the specified range set from this range set (optional operation).
      default void removeAll(Iterable<Range<C>> ranges)
      Removes all of the specified ranges from this range set (optional operation).
      Range<C> span()
      Returns the minimal range which encloses all ranges in this range set.
      RangeSet<C> subRangeSet(Range<C> view)
      Returns a view of the intersection of this RangeSet with the specified range.
      String toString()
      Returns a readable string representation of this range set.
    • Method Detail

      • contains

        boolean contains(C value)
        Determines whether any of this range set's member ranges contains value.
      • rangeContaining

        Range<CrangeContaining(C value)
        Returns the unique range from this range set that contains value, or null if this range set does not contain value.
      • intersects

        boolean intersects(Range<C> otherRange)
        Returns true if there exists a non-empty range enclosed by both a member range in this range set and the specified range. This is equivalent to calling subRangeSet(otherRange) and testing whether the resulting range set is non-empty.
        Since:
        20.0
      • encloses

        boolean encloses(Range<C> otherRange)
        Returns true if there exists a member range in this range set which encloses the specified range.
      • enclosesAll

        boolean enclosesAll(RangeSet<C> other)
        Returns true if for each member range in other there exists a member range in this range set which encloses it. It follows that this.contains(value) whenever other.contains(value). Returns true if other is empty.

        This is equivalent to checking if this range set encloses(com.google.common.collect.Range<C>) each of the ranges in other.

      • isEmpty

        boolean isEmpty()
        Returns true if this range set contains no ranges.
      • asRanges

        Set<Range<C>> asRanges()
        Returns a view of the disconnected ranges that make up this range set. The returned set may be empty. The iterators returned by its Iterable.iterator() method return the ranges in increasing order of lower bound (equivalently, of upper bound).
      • asDescendingSetOfRanges

        Set<Range<C>> asDescendingSetOfRanges()
        Returns a descending view of the disconnected ranges that make up this range set. The returned set may be empty. The iterators returned by its Iterable.iterator() method return the ranges in decreasing order of lower bound (equivalently, of upper bound).
        Since:
        19.0
      • subRangeSet

        RangeSet<CsubRangeSet(Range<C> view)
        Returns a view of the intersection of this RangeSet with the specified range.

        The returned view supports all optional operations supported by this RangeSet, with the caveat that an IllegalArgumentException is thrown on an attempt to add any range not enclosed by view.

      • add

        void add(Range<C> range)
        Adds the specified range to this RangeSet (optional operation). That is, for equal range sets a and b, the result of a.add(range) is that a will be the minimal range set for which both a.enclosesAll(b) and a.encloses(range).

        Note that range will be coalesced with any ranges in the range set that are connected with it. Moreover, if range is empty, this is a no-op.

        Throws:
        UnsupportedOperationException - if this range set does not support the add operation
      • remove

        void remove(Range<C> range)
        Removes the specified range from this RangeSet (optional operation). After this operation, if range.contains(c), this.contains(c) will return false.

        If range is empty, this is a no-op.

        Throws:
        UnsupportedOperationException - if this range set does not support the remove operation
      • clear

        void clear()
        Removes all ranges from this RangeSet (optional operation). After this operation, this.contains(c) will return false for all c.

        This is equivalent to remove(Range.all()).

        Throws:
        UnsupportedOperationException - if this range set does not support the clear operation
      • toString

        String toString()
        Returns a readable string representation of this range set. For example, if this RangeSet consisted of Range.closed(1, 3) and Range.greaterThan(4), this might return " [1..3](4..+∞)"}.
        Overrides:
        toString in class Object
        Returns:
        a string representation of the object.