View Javadoc
1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.Beta;
20  import com.google.common.annotations.GwtCompatible;
21  
22  import java.util.Comparator;
23  import java.util.Iterator;
24  import java.util.NoSuchElementException;
25  import java.util.SortedSet;
26  
27  import javax.annotation.Nullable;
28  
29  /**
30   * A sorted set which forwards all its method calls to another sorted set.
31   * Subclasses should override one or more methods to modify the behavior of the
32   * backing sorted set as desired per the <a
33   * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
34   *
35   * <p><i>Warning:</i> The methods of {@code ForwardingSortedSet} forward
36   * <i>indiscriminately</i> to the methods of the delegate. For example,
37   * overriding {@link #add} alone <i>will not</i> change the behavior of {@link
38   * #addAll}, which can lead to unexpected behavior. In this case, you should
39   * override {@code addAll} as well, either providing your own implementation, or
40   * delegating to the provided {@code standardAddAll} method.
41   *
42   * <p>Each of the {@code standard} methods, where appropriate, uses the set's
43   * comparator (or the natural ordering of the elements, if there is no
44   * comparator) to test element equality. As a result, if the comparator is not
45   * consistent with equals, some of the standard implementations may violate the
46   * {@code Set} contract.
47   *
48   * <p>The {@code standard} methods and the collection views they return are not
49   * guaranteed to be thread-safe, even when all of the methods that they depend
50   * on are thread-safe.
51   *
52   * @author Mike Bostock
53   * @author Louis Wasserman
54   * @since 2.0 (imported from Google Collections Library)
55   */
56  @GwtCompatible
57  public abstract class ForwardingSortedSet<E> extends ForwardingSet<E>
58      implements SortedSet<E> {
59  
60    /** Constructor for use by subclasses. */
61    protected ForwardingSortedSet() {}
62  
63    @Override protected abstract SortedSet<E> delegate();
64  
65    @Override
66    public Comparator<? super E> comparator() {
67      return delegate().comparator();
68    }
69  
70    @Override
71    public E first() {
72      return delegate().first();
73    }
74  
75    @Override
76    public SortedSet<E> headSet(E toElement) {
77      return delegate().headSet(toElement);
78    }
79  
80    @Override
81    public E last() {
82      return delegate().last();
83    }
84  
85    @Override
86    public SortedSet<E> subSet(E fromElement, E toElement) {
87      return delegate().subSet(fromElement, toElement);
88    }
89  
90    @Override
91    public SortedSet<E> tailSet(E fromElement) {
92      return delegate().tailSet(fromElement);
93    }
94  
95    // unsafe, but worst case is a CCE is thrown, which callers will be expecting
96    @SuppressWarnings("unchecked")
97    private int unsafeCompare(Object o1, Object o2) {
98      Comparator<? super E> comparator = comparator();
99      return (comparator == null)
100         ? ((Comparable<Object>) o1).compareTo(o2)
101         : ((Comparator<Object>) comparator).compare(o1, o2);
102   }
103 
104   /**
105    * A sensible definition of {@link #contains} in terms of the {@code first()}
106    * method of {@link #tailSet}. If you override {@link #tailSet}, you may wish
107    * to override {@link #contains} to forward to this implementation.
108    *
109    * @since 7.0
110    */
111   @Override @Beta protected boolean standardContains(@Nullable Object object) {
112     try {
113       // any ClassCastExceptions are caught
114       @SuppressWarnings("unchecked")
115       SortedSet<Object> self = (SortedSet<Object>) this;
116       Object ceiling = self.tailSet(object).first();
117       return unsafeCompare(ceiling, object) == 0;
118     } catch (ClassCastException e) {
119       return false;
120     } catch (NoSuchElementException e) {
121       return false;
122     } catch (NullPointerException e) {
123       return false;
124     }
125   }
126 
127   /**
128    * A sensible definition of {@link #remove} in terms of the {@code iterator()}
129    * method of {@link #tailSet}. If you override {@link #tailSet}, you may wish
130    * to override {@link #remove} to forward to this implementation.
131    *
132    * @since 7.0
133    */
134   @Override @Beta protected boolean standardRemove(@Nullable Object object) {
135     try {
136       // any ClassCastExceptions are caught
137       @SuppressWarnings("unchecked")
138       SortedSet<Object> self = (SortedSet<Object>) this;
139       Iterator<Object> iterator = self.tailSet(object).iterator();
140       if (iterator.hasNext()) {
141         Object ceiling = iterator.next();
142         if (unsafeCompare(ceiling, object) == 0) {
143           iterator.remove();
144           return true;
145         }
146       }
147     } catch (ClassCastException e) {
148       return false;
149     } catch (NullPointerException e) {
150       return false;
151     }
152     return false;
153   }
154 
155   /**
156    * A sensible default implementation of {@link #subSet(Object, Object)} in
157    * terms of {@link #headSet(Object)} and {@link #tailSet(Object)}. In some
158    * situations, you may wish to override {@link #subSet(Object, Object)} to
159    * forward to this implementation.
160    *
161    * @since 7.0
162    */
163   @Beta protected SortedSet<E> standardSubSet(E fromElement, E toElement) {
164     return tailSet(fromElement).headSet(toElement);
165   }
166 }