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