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