001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the
010 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
011 * express or implied. See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package com.google.common.collect;
016
017import com.google.common.annotations.Beta;
018import com.google.common.annotations.GwtIncompatible;
019import com.google.errorprone.annotations.DoNotMock;
020import java.util.NoSuchElementException;
021import java.util.Set;
022import javax.annotation.CheckForNull;
023
024/**
025 * A set comprising zero or more {@linkplain Range#isEmpty nonempty}, {@linkplain
026 * Range#isConnected(Range) disconnected} ranges of type {@code C}.
027 *
028 * <p>Implementations that choose to support the {@link #add(Range)} operation are required to
029 * ignore empty ranges and coalesce connected ranges. For example:
030 *
031 * <pre>{@code
032 * RangeSet<Integer> rangeSet = TreeRangeSet.create();
033 * rangeSet.add(Range.closed(1, 10)); // {[1, 10]}
034 * rangeSet.add(Range.closedOpen(11, 15)); // disconnected range; {[1, 10], [11, 15)}
035 * rangeSet.add(Range.closedOpen(15, 20)); // connected range; {[1, 10], [11, 20)}
036 * rangeSet.add(Range.openClosed(0, 0)); // empty range; {[1, 10], [11, 20)}
037 * rangeSet.remove(Range.open(5, 10)); // splits [1, 10]; {[1, 5], [10, 10], [11, 20)}
038 * }</pre>
039 *
040 * <p>Note that the behavior of {@link Range#isEmpty()} and {@link Range#isConnected(Range)} may not
041 * be as expected on discrete ranges. See the Javadoc of those methods for details.
042 *
043 * <p>For a {@link Set} whose contents are specified by a {@link Range}, see {@link ContiguousSet}.
044 *
045 * <p>See the Guava User Guide article on <a href=
046 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#rangeset"> RangeSets</a>.
047 *
048 * @author Kevin Bourrillion
049 * @author Louis Wasserman
050 * @since 14.0
051 */
052@Beta
053@DoNotMock("Use ImmutableRangeSet or TreeRangeSet")
054@GwtIncompatible
055@ElementTypesAreNonnullByDefault
056public interface RangeSet<C extends Comparable> {
057  // TODO(lowasser): consider adding default implementations of some of these methods
058
059  // Query methods
060
061  /** Determines whether any of this range set's member ranges contains {@code value}. */
062  boolean contains(C value);
063
064  /**
065   * Returns the unique range from this range set that {@linkplain Range#contains contains} {@code
066   * value}, or {@code null} if this range set does not contain {@code value}.
067   */
068  @CheckForNull
069  Range<C> rangeContaining(C value);
070
071  /**
072   * Returns {@code true} if there exists a non-empty range enclosed by both a member range in this
073   * range set and the specified range. This is equivalent to calling {@code
074   * subRangeSet(otherRange)} and testing whether the resulting range set is non-empty.
075   *
076   * @since 20.0
077   */
078  boolean intersects(Range<C> otherRange);
079
080  /**
081   * Returns {@code true} if there exists a member range in this range set which {@linkplain
082   * Range#encloses encloses} the specified range.
083   */
084  boolean encloses(Range<C> otherRange);
085
086  /**
087   * Returns {@code true} if for each member range in {@code other} there exists a member range in
088   * this range set which {@linkplain Range#encloses encloses} it. It follows that {@code
089   * this.contains(value)} whenever {@code other.contains(value)}. Returns {@code true} if {@code
090   * other} is empty.
091   *
092   * <p>This is equivalent to checking if this range set {@link #encloses} each of the ranges in
093   * {@code other}.
094   */
095  boolean enclosesAll(RangeSet<C> other);
096
097  /**
098   * Returns {@code true} if for each range in {@code other} there exists a member range in this
099   * range set which {@linkplain Range#encloses encloses} it. Returns {@code true} if {@code other}
100   * is empty.
101   *
102   * <p>This is equivalent to checking if this range set {@link #encloses} each range in {@code
103   * other}.
104   *
105   * @since 21.0
106   */
107  default boolean enclosesAll(Iterable<Range<C>> other) {
108    for (Range<C> range : other) {
109      if (!encloses(range)) {
110        return false;
111      }
112    }
113    return true;
114  }
115
116  /** Returns {@code true} if this range set contains no ranges. */
117  boolean isEmpty();
118
119  /**
120   * Returns the minimal range which {@linkplain Range#encloses(Range) encloses} all ranges in this
121   * range set.
122   *
123   * @throws NoSuchElementException if this range set is {@linkplain #isEmpty() empty}
124   */
125  Range<C> span();
126
127  // Views
128
129  /**
130   * Returns a view of the {@linkplain Range#isConnected disconnected} ranges that make up this
131   * range set. The returned set may be empty. The iterators returned by its {@link
132   * Iterable#iterator} method return the ranges in increasing order of lower bound (equivalently,
133   * of upper bound).
134   */
135  Set<Range<C>> asRanges();
136
137  /**
138   * Returns a descending view of the {@linkplain Range#isConnected disconnected} ranges that make
139   * up this range set. The returned set may be empty. The iterators returned by its {@link
140   * Iterable#iterator} method return the ranges in decreasing order of lower bound (equivalently,
141   * of upper bound).
142   *
143   * @since 19.0
144   */
145  Set<Range<C>> asDescendingSetOfRanges();
146
147  /**
148   * Returns a view of the complement of this {@code RangeSet}.
149   *
150   * <p>The returned view supports the {@link #add} operation if this {@code RangeSet} supports
151   * {@link #remove}, and vice versa.
152   */
153  RangeSet<C> complement();
154
155  /**
156   * Returns a view of the intersection of this {@code RangeSet} with the specified range.
157   *
158   * <p>The returned view supports all optional operations supported by this {@code RangeSet}, with
159   * the caveat that an {@link IllegalArgumentException} is thrown on an attempt to {@linkplain
160   * #add(Range) add} any range not {@linkplain Range#encloses(Range) enclosed} by {@code view}.
161   */
162  RangeSet<C> subRangeSet(Range<C> view);
163
164  // Modification
165
166  /**
167   * Adds the specified range to this {@code RangeSet} (optional operation). That is, for equal
168   * range sets a and b, the result of {@code a.add(range)} is that {@code a} will be the minimal
169   * range set for which both {@code a.enclosesAll(b)} and {@code a.encloses(range)}.
170   *
171   * <p>Note that {@code range} will be {@linkplain Range#span(Range) coalesced} with any ranges in
172   * the range set that are {@linkplain Range#isConnected(Range) connected} with it. Moreover, if
173   * {@code range} is empty, this is a no-op.
174   *
175   * @throws UnsupportedOperationException if this range set does not support the {@code add}
176   *     operation
177   */
178  void add(Range<C> range);
179
180  /**
181   * Removes the specified range from this {@code RangeSet} (optional operation). After this
182   * operation, if {@code range.contains(c)}, {@code this.contains(c)} will return {@code false}.
183   *
184   * <p>If {@code range} is empty, this is a no-op.
185   *
186   * @throws UnsupportedOperationException if this range set does not support the {@code remove}
187   *     operation
188   */
189  void remove(Range<C> range);
190
191  /**
192   * Removes all ranges from this {@code RangeSet} (optional operation). After this operation,
193   * {@code this.contains(c)} will return false for all {@code c}.
194   *
195   * <p>This is equivalent to {@code remove(Range.all())}.
196   *
197   * @throws UnsupportedOperationException if this range set does not support the {@code clear}
198   *     operation
199   */
200  void clear();
201
202  /**
203   * Adds all of the ranges from the specified range set to this range set (optional operation).
204   * After this operation, this range set is the minimal range set that {@linkplain
205   * #enclosesAll(RangeSet) encloses} both the original range set and {@code other}.
206   *
207   * <p>This is equivalent to calling {@link #add} on each of the ranges in {@code other} in turn.
208   *
209   * @throws UnsupportedOperationException if this range set does not support the {@code addAll}
210   *     operation
211   */
212  void addAll(RangeSet<C> other);
213
214  /**
215   * Adds all of the specified ranges to this range set (optional operation). After this operation,
216   * this range set is the minimal range set that {@linkplain #enclosesAll(RangeSet) encloses} both
217   * the original range set and each range in {@code other}.
218   *
219   * <p>This is equivalent to calling {@link #add} on each of the ranges in {@code other} in turn.
220   *
221   * @throws UnsupportedOperationException if this range set does not support the {@code addAll}
222   *     operation
223   * @since 21.0
224   */
225  default void addAll(Iterable<Range<C>> ranges) {
226    for (Range<C> range : ranges) {
227      add(range);
228    }
229  }
230
231  /**
232   * Removes all of the ranges from the specified range set from this range set (optional
233   * operation). After this operation, if {@code other.contains(c)}, {@code this.contains(c)} will
234   * return {@code false}.
235   *
236   * <p>This is equivalent to calling {@link #remove} on each of the ranges in {@code other} in
237   * turn.
238   *
239   * @throws UnsupportedOperationException if this range set does not support the {@code removeAll}
240   *     operation
241   */
242  void removeAll(RangeSet<C> other);
243
244  /**
245   * Removes all of the specified ranges from this range set (optional operation).
246   *
247   * <p>This is equivalent to calling {@link #remove} on each of the ranges in {@code other} in
248   * turn.
249   *
250   * @throws UnsupportedOperationException if this range set does not support the {@code removeAll}
251   *     operation
252   * @since 21.0
253   */
254  default void removeAll(Iterable<Range<C>> ranges) {
255    for (Range<C> range : ranges) {
256      remove(range);
257    }
258  }
259
260  // Object methods
261
262  /**
263   * Returns {@code true} if {@code obj} is another {@code RangeSet} that contains the same ranges
264   * according to {@link Range#equals(Object)}.
265   */
266  @Override
267  boolean equals(@CheckForNull Object obj);
268
269  /** Returns {@code asRanges().hashCode()}. */
270  @Override
271  int hashCode();
272
273  /**
274   * Returns a readable string representation of this range set. For example, if this {@code
275   * RangeSet} consisted of {@code Range.closed(1, 3)} and {@code Range.greaterThan(4)}, this might
276   * return {@code " [1..3](4..+∞)}"}.
277   */
278  @Override
279  String toString();
280}