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