001/*
002 * Copyright (C) 2010 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 License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.collect;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;
019import static java.util.Objects.requireNonNull;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.annotations.J2ktIncompatible;
024import com.google.errorprone.annotations.DoNotCall;
025import java.util.Collections;
026import java.util.NoSuchElementException;
027import java.util.Set;
028
029/**
030 * A sorted set of contiguous values in a given {@link DiscreteDomain}. Example:
031 *
032 * <pre>{@code
033 * ContiguousSet.create(Range.closed(5, 42), DiscreteDomain.integers())
034 * }</pre>
035 *
036 * <p>Note that because bounded ranges over {@code int} and {@code long} values are so common, this
037 * particular example can be written as just:
038 *
039 * <pre>{@code
040 * ContiguousSet.closed(5, 42)
041 * }</pre>
042 *
043 * <p><b>Warning:</b> Be extremely careful what you do with conceptually large instances (such as
044 * {@code ContiguousSet.create(Range.greaterThan(0), DiscreteDomain.integers()}). Certain operations
045 * on such a set can be performed efficiently, but others (such as {@link Set#hashCode} or {@link
046 * Collections#frequency}) can cause major performance problems.
047 *
048 * @author Gregory Kick
049 * @since 10.0
050 */
051@GwtCompatible(emulated = true)
052@SuppressWarnings("rawtypes") // allow ungenerified Comparable types
053public abstract class ContiguousSet<C extends Comparable> extends ImmutableSortedSet<C> {
054  /**
055   * Returns a {@code ContiguousSet} containing the same values in the given domain {@linkplain
056   * Range#contains contained} by the range.
057   *
058   * @throws IllegalArgumentException if neither range nor the domain has a lower bound, or if
059   *     neither has an upper bound
060   * @since 13.0
061   */
062  public static <C extends Comparable> ContiguousSet<C> create(
063      Range<C> range, DiscreteDomain<C> domain) {
064    checkNotNull(range);
065    checkNotNull(domain);
066    Range<C> effectiveRange = range;
067    try {
068      if (!range.hasLowerBound()) {
069        effectiveRange = effectiveRange.intersection(Range.atLeast(domain.minValue()));
070      }
071      if (!range.hasUpperBound()) {
072        effectiveRange = effectiveRange.intersection(Range.atMost(domain.maxValue()));
073      }
074    } catch (NoSuchElementException e) {
075      throw new IllegalArgumentException(e);
076    }
077
078    boolean empty;
079    if (effectiveRange.isEmpty()) {
080      empty = true;
081    } else {
082      /*
083       * requireNonNull is safe because the effectiveRange operations above would have thrown or
084       * effectiveRange.isEmpty() would have returned true.
085       */
086      C afterLower = requireNonNull(range.lowerBound.leastValueAbove(domain));
087      C beforeUpper = requireNonNull(range.upperBound.greatestValueBelow(domain));
088      // Per class spec, we are allowed to throw CCE if necessary
089      empty = Range.compareOrThrow(afterLower, beforeUpper) > 0;
090    }
091
092    return empty
093        ? new EmptyContiguousSet<C>(domain)
094        : new RegularContiguousSet<C>(effectiveRange, domain);
095  }
096
097  /**
098   * Returns a nonempty contiguous set containing all {@code int} values from {@code lower}
099   * (inclusive) to {@code upper} (inclusive). (These are the same values contained in {@code
100   * Range.closed(lower, upper)}.)
101   *
102   * @throws IllegalArgumentException if {@code lower} is greater than {@code upper}
103   * @since 23.0
104   */
105  public static ContiguousSet<Integer> closed(int lower, int upper) {
106    return create(Range.closed(lower, upper), DiscreteDomain.integers());
107  }
108
109  /**
110   * Returns a nonempty contiguous set containing all {@code long} values from {@code lower}
111   * (inclusive) to {@code upper} (inclusive). (These are the same values contained in {@code
112   * Range.closed(lower, upper)}.)
113   *
114   * @throws IllegalArgumentException if {@code lower} is greater than {@code upper}
115   * @since 23.0
116   */
117  public static ContiguousSet<Long> closed(long lower, long upper) {
118    return create(Range.closed(lower, upper), DiscreteDomain.longs());
119  }
120
121  /**
122   * Returns a contiguous set containing all {@code int} values from {@code lower} (inclusive) to
123   * {@code upper} (exclusive). If the endpoints are equal, an empty set is returned. (These are the
124   * same values contained in {@code Range.closedOpen(lower, upper)}.)
125   *
126   * @throws IllegalArgumentException if {@code lower} is greater than {@code upper}
127   * @since 23.0
128   */
129  public static ContiguousSet<Integer> closedOpen(int lower, int upper) {
130    return create(Range.closedOpen(lower, upper), DiscreteDomain.integers());
131  }
132
133  /**
134   * Returns a contiguous set containing all {@code long} values from {@code lower} (inclusive) to
135   * {@code upper} (exclusive). If the endpoints are equal, an empty set is returned. (These are the
136   * same values contained in {@code Range.closedOpen(lower, upper)}.)
137   *
138   * @throws IllegalArgumentException if {@code lower} is greater than {@code upper}
139   * @since 23.0
140   */
141  public static ContiguousSet<Long> closedOpen(long lower, long upper) {
142    return create(Range.closedOpen(lower, upper), DiscreteDomain.longs());
143  }
144
145  final DiscreteDomain<C> domain;
146
147  ContiguousSet(DiscreteDomain<C> domain) {
148    super(Ordering.natural());
149    this.domain = domain;
150  }
151
152  @Override
153  public ContiguousSet<C> headSet(C toElement) {
154    return headSetImpl(checkNotNull(toElement), false);
155  }
156
157  /**
158   * @since 12.0
159   */
160  @GwtIncompatible // NavigableSet
161  @Override
162  public ContiguousSet<C> headSet(C toElement, boolean inclusive) {
163    return headSetImpl(checkNotNull(toElement), inclusive);
164  }
165
166  @Override
167  public ContiguousSet<C> subSet(C fromElement, C toElement) {
168    checkNotNull(fromElement);
169    checkNotNull(toElement);
170    checkArgument(comparator().compare(fromElement, toElement) <= 0);
171    return subSetImpl(fromElement, true, toElement, false);
172  }
173
174  /**
175   * @since 12.0
176   */
177  @GwtIncompatible // NavigableSet
178  @Override
179  public ContiguousSet<C> subSet(
180      C fromElement, boolean fromInclusive, C toElement, boolean toInclusive) {
181    checkNotNull(fromElement);
182    checkNotNull(toElement);
183    checkArgument(comparator().compare(fromElement, toElement) <= 0);
184    return subSetImpl(fromElement, fromInclusive, toElement, toInclusive);
185  }
186
187  @Override
188  public ContiguousSet<C> tailSet(C fromElement) {
189    return tailSetImpl(checkNotNull(fromElement), true);
190  }
191
192  /**
193   * @since 12.0
194   */
195  @GwtIncompatible // NavigableSet
196  @Override
197  public ContiguousSet<C> tailSet(C fromElement, boolean inclusive) {
198    return tailSetImpl(checkNotNull(fromElement), inclusive);
199  }
200
201  /*
202   * These methods perform most headSet, subSet, and tailSet logic, besides parameter validation.
203   */
204  @SuppressWarnings("MissingOverride") // Supermethod does not exist under GWT.
205  abstract ContiguousSet<C> headSetImpl(C toElement, boolean inclusive);
206
207  @SuppressWarnings("MissingOverride") // Supermethod does not exist under GWT.
208  abstract ContiguousSet<C> subSetImpl(
209      C fromElement, boolean fromInclusive, C toElement, boolean toInclusive);
210
211  @SuppressWarnings("MissingOverride") // Supermethod does not exist under GWT.
212  abstract ContiguousSet<C> tailSetImpl(C fromElement, boolean inclusive);
213
214  /**
215   * Returns the set of values that are contained in both this set and the other.
216   *
217   * <p>This method should always be used instead of {@link Sets#intersection} for {@link
218   * ContiguousSet} instances.
219   */
220  public abstract ContiguousSet<C> intersection(ContiguousSet<C> other);
221
222  /**
223   * Returns a range, closed on both ends, whose endpoints are the minimum and maximum values
224   * contained in this set. This is equivalent to {@code range(CLOSED, CLOSED)}.
225   *
226   * @throws NoSuchElementException if this set is empty
227   */
228  public abstract Range<C> range();
229
230  /**
231   * Returns the minimal range with the given boundary types for which all values in this set are
232   * {@linkplain Range#contains(Comparable) contained} within the range.
233   *
234   * <p>Note that this method will return ranges with unbounded endpoints if {@link BoundType#OPEN}
235   * is requested for a domain minimum or maximum. For example, if {@code set} was created from the
236   * range {@code [1..Integer.MAX_VALUE]} then {@code set.range(CLOSED, OPEN)} must return {@code
237   * [1..∞)}.
238   *
239   * @throws NoSuchElementException if this set is empty
240   */
241  public abstract Range<C> range(BoundType lowerBoundType, BoundType upperBoundType);
242
243  @Override
244  @GwtIncompatible // NavigableSet
245  ImmutableSortedSet<C> createDescendingSet() {
246    return new DescendingImmutableSortedSet<>(this);
247  }
248
249  /** Returns a shorthand representation of the contents such as {@code "[1..100]"}. */
250  @Override
251  public String toString() {
252    return range().toString();
253  }
254
255  /**
256   * Not supported. {@code ContiguousSet} instances are constructed with {@link #create}. This
257   * method exists only to hide {@link ImmutableSet#builder} from consumers of {@code
258   * ContiguousSet}.
259   *
260   * @throws UnsupportedOperationException always
261   * @deprecated Use {@link #create}.
262   */
263  @Deprecated
264  @DoNotCall("Always throws UnsupportedOperationException")
265  public static <E> ImmutableSortedSet.Builder<E> builder() {
266    throw new UnsupportedOperationException();
267  }
268
269  // redeclare to help optimizers with b/310253115
270  @SuppressWarnings("RedundantOverride")
271  @J2ktIncompatible // serialization
272  @Override
273  @GwtIncompatible // serialization
274  Object writeReplace() {
275    return super.writeReplace();
276  }
277}