001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import static com.google.common.collect.ForwardingSortedMap.unsafeCompare;
020
021import com.google.common.annotations.Beta;
022import com.google.common.annotations.GwtCompatible;
023import java.util.Comparator;
024import java.util.Iterator;
025import java.util.NoSuchElementException;
026import java.util.SortedSet;
027import javax.annotation.CheckForNull;
028import org.checkerframework.checker.nullness.qual.Nullable;
029
030/**
031 * A sorted set which forwards all its method calls to another sorted set. Subclasses should
032 * override one or more methods to modify the behavior of the backing sorted set as desired per the
033 * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
034 *
035 * <p><b>Warning:</b> The methods of {@code ForwardingSortedSet} forward <i>indiscriminately</i> to
036 * the methods of the delegate. For example, overriding {@link #add} alone <i>will not</i> change
037 * the behavior of {@link #addAll}, which can lead to unexpected behavior. In this case, you should
038 * override {@code addAll} as well, either providing your own implementation, or delegating to the
039 * provided {@code standardAddAll} method.
040 *
041 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
042 * default} methods. Instead, it inherits their default implementations. When those implementations
043 * invoke methods, they invoke methods on the {@code ForwardingSortedSet}.
044 *
045 * <p>Each of the {@code standard} methods, where appropriate, uses the set's comparator (or the
046 * natural ordering of the elements, if there is no comparator) to test element equality. As a
047 * result, if the comparator is not consistent with equals, some of the standard implementations may
048 * violate the {@code Set} contract.
049 *
050 * <p>The {@code standard} methods and the collection views they return are not guaranteed to be
051 * thread-safe, even when all of the methods that they depend on are thread-safe.
052 *
053 * @author Mike Bostock
054 * @author Louis Wasserman
055 * @since 2.0
056 */
057@GwtCompatible
058@ElementTypesAreNonnullByDefault
059public abstract class ForwardingSortedSet<E extends @Nullable Object> extends ForwardingSet<E>
060    implements SortedSet<E> {
061
062  /** Constructor for use by subclasses. */
063  protected ForwardingSortedSet() {}
064
065  @Override
066  protected abstract SortedSet<E> delegate();
067
068  @Override
069  @CheckForNull
070  public Comparator<? super E> comparator() {
071    return delegate().comparator();
072  }
073
074  @Override
075  @ParametricNullness
076  public E first() {
077    return delegate().first();
078  }
079
080  @Override
081  public SortedSet<E> headSet(@ParametricNullness E toElement) {
082    return delegate().headSet(toElement);
083  }
084
085  @Override
086  @ParametricNullness
087  public E last() {
088    return delegate().last();
089  }
090
091  @Override
092  public SortedSet<E> subSet(@ParametricNullness E fromElement, @ParametricNullness E toElement) {
093    return delegate().subSet(fromElement, toElement);
094  }
095
096  @Override
097  public SortedSet<E> tailSet(@ParametricNullness E fromElement) {
098    return delegate().tailSet(fromElement);
099  }
100
101  /**
102   * A sensible definition of {@link #contains} in terms of the {@code first()} method of {@link
103   * #tailSet}. If you override {@link #tailSet}, you may wish to override {@link #contains} to
104   * forward to this implementation.
105   *
106   * @since 7.0
107   */
108  @Override
109  @Beta
110  protected boolean standardContains(@CheckForNull Object object) {
111    try {
112      // any ClassCastExceptions and NullPointerExceptions are caught
113      @SuppressWarnings({"unchecked", "nullness"})
114      SortedSet<@Nullable Object> self = (SortedSet<@Nullable Object>) this;
115      Object ceiling = self.tailSet(object).first();
116      return unsafeCompare(comparator(), ceiling, object) == 0;
117    } catch (ClassCastException | NoSuchElementException | NullPointerException e) {
118      return false;
119    }
120  }
121
122  /**
123   * A sensible definition of {@link #remove} in terms of the {@code iterator()} method of {@link
124   * #tailSet}. If you override {@link #tailSet}, you may wish to override {@link #remove} to
125   * forward to this implementation.
126   *
127   * @since 7.0
128   */
129  @Override
130  @Beta
131  protected boolean standardRemove(@CheckForNull Object object) {
132    try {
133      // any ClassCastExceptions and NullPointerExceptions are caught
134      @SuppressWarnings({"unchecked", "nullness"})
135      SortedSet<@Nullable Object> self = (SortedSet<@Nullable Object>) this;
136      Iterator<?> iterator = self.tailSet(object).iterator();
137      if (iterator.hasNext()) {
138        Object ceiling = iterator.next();
139        if (unsafeCompare(comparator(), ceiling, object) == 0) {
140          iterator.remove();
141          return true;
142        }
143      }
144    } catch (ClassCastException | NullPointerException e) {
145      return false;
146    }
147    return false;
148  }
149
150  /**
151   * A sensible default implementation of {@link #subSet(Object, Object)} in terms of {@link
152   * #headSet(Object)} and {@link #tailSet(Object)}. In some situations, you may wish to override
153   * {@link #subSet(Object, Object)} to forward to this implementation.
154   *
155   * @since 7.0
156   */
157  @Beta
158  protected SortedSet<E> standardSubSet(
159      @ParametricNullness E fromElement, @ParametricNullness E toElement) {
160    return tailSet(fromElement).headSet(toElement);
161  }
162}