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