001/*
002 * Copyright (C) 2010 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.testing;
018
019import com.google.common.annotations.GwtIncompatible;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import java.io.Serializable;
022import java.util.Collection;
023import java.util.Comparator;
024import java.util.Iterator;
025import java.util.NavigableSet;
026import java.util.SortedSet;
027import java.util.TreeSet;
028import org.jspecify.annotations.Nullable;
029
030/**
031 * A wrapper around {@code TreeSet} that aggressively checks to see if elements are mutually
032 * comparable. This implementation passes the navigable set test suites.
033 *
034 * @author Louis Wasserman
035 */
036@GwtIncompatible
037public final class SafeTreeSet<E> implements Serializable, NavigableSet<E> {
038  @SuppressWarnings("unchecked")
039  private static final Comparator<Object> NATURAL_ORDER =
040      (o1, o2) -> ((Comparable<Object>) o1).compareTo(o2);
041
042  private final NavigableSet<E> delegate;
043
044  public SafeTreeSet() {
045    this(new TreeSet<E>());
046  }
047
048  public SafeTreeSet(Collection<? extends E> collection) {
049    this(new TreeSet<E>(collection));
050  }
051
052  public SafeTreeSet(Comparator<? super E> comparator) {
053    this(new TreeSet<E>(comparator));
054  }
055
056  public SafeTreeSet(SortedSet<E> set) {
057    this(new TreeSet<E>(set));
058  }
059
060  private SafeTreeSet(NavigableSet<E> delegate) {
061    this.delegate = delegate;
062    for (E e : this) {
063      checkValid(e);
064    }
065  }
066
067  @Override
068  public boolean add(E element) {
069    return delegate.add(checkValid(element));
070  }
071
072  @Override
073  public boolean addAll(Collection<? extends E> collection) {
074    for (E e : collection) {
075      checkValid(e);
076    }
077    return delegate.addAll(collection);
078  }
079
080  @Override
081  public @Nullable E ceiling(E e) {
082    return delegate.ceiling(checkValid(e));
083  }
084
085  @Override
086  public void clear() {
087    delegate.clear();
088  }
089
090  @Override
091  public Comparator<? super E> comparator() {
092    Comparator<? super E> comparator = delegate.comparator();
093    if (comparator != null) {
094      return comparator;
095    }
096    return NATURAL_ORDER;
097  }
098
099  @Override
100  public boolean contains(Object object) {
101    return delegate.contains(checkValid(object));
102  }
103
104  @Override
105  public boolean containsAll(Collection<?> c) {
106    return delegate.containsAll(c);
107  }
108
109  @Override
110  public Iterator<E> descendingIterator() {
111    return delegate.descendingIterator();
112  }
113
114  @Override
115  public NavigableSet<E> descendingSet() {
116    return new SafeTreeSet<>(delegate.descendingSet());
117  }
118
119  @Override
120  public E first() {
121    return delegate.first();
122  }
123
124  @Override
125  public @Nullable E floor(E e) {
126    return delegate.floor(checkValid(e));
127  }
128
129  @Override
130  public SortedSet<E> headSet(E toElement) {
131    return headSet(toElement, false);
132  }
133
134  @Override
135  public NavigableSet<E> headSet(E toElement, boolean inclusive) {
136    return new SafeTreeSet<>(delegate.headSet(checkValid(toElement), inclusive));
137  }
138
139  @Override
140  public @Nullable E higher(E e) {
141    return delegate.higher(checkValid(e));
142  }
143
144  @Override
145  public boolean isEmpty() {
146    return delegate.isEmpty();
147  }
148
149  @Override
150  public Iterator<E> iterator() {
151    return delegate.iterator();
152  }
153
154  @Override
155  public E last() {
156    return delegate.last();
157  }
158
159  @Override
160  public @Nullable E lower(E e) {
161    return delegate.lower(checkValid(e));
162  }
163
164  @Override
165  public @Nullable E pollFirst() {
166    return delegate.pollFirst();
167  }
168
169  @Override
170  public @Nullable E pollLast() {
171    return delegate.pollLast();
172  }
173
174  @Override
175  public boolean remove(Object object) {
176    return delegate.remove(checkValid(object));
177  }
178
179  @Override
180  public boolean removeAll(Collection<?> c) {
181    return delegate.removeAll(c);
182  }
183
184  @Override
185  public boolean retainAll(Collection<?> c) {
186    return delegate.retainAll(c);
187  }
188
189  @Override
190  public int size() {
191    return delegate.size();
192  }
193
194  @Override
195  public NavigableSet<E> subSet(
196      E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
197    return new SafeTreeSet<>(
198        delegate.subSet(
199            checkValid(fromElement), fromInclusive, checkValid(toElement), toInclusive));
200  }
201
202  @Override
203  public SortedSet<E> subSet(E fromElement, E toElement) {
204    return subSet(fromElement, true, toElement, false);
205  }
206
207  @Override
208  public SortedSet<E> tailSet(E fromElement) {
209    return tailSet(fromElement, true);
210  }
211
212  @Override
213  public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
214    return new SafeTreeSet<>(delegate.tailSet(checkValid(fromElement), inclusive));
215  }
216
217  @Override
218  public Object[] toArray() {
219    return delegate.toArray();
220  }
221
222  @Override
223  public <T> T[] toArray(T[] a) {
224    return delegate.toArray(a);
225  }
226
227  @CanIgnoreReturnValue
228  private <T> T checkValid(T t) {
229    // a ClassCastException is what's supposed to happen!
230    @SuppressWarnings("unchecked")
231    E e = (E) t;
232    int unused = comparator().compare(e, e);
233    return t;
234  }
235
236  @Override
237  public boolean equals(@Nullable Object obj) {
238    return delegate.equals(obj);
239  }
240
241  @Override
242  public int hashCode() {
243    return delegate.hashCode();
244  }
245
246  @Override
247  public String toString() {
248    return delegate.toString();
249  }
250
251  private static final long serialVersionUID = 0L;
252}