001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the
010 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
011 * express or implied. See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package com.google.common.collect;
016
017import com.google.common.annotations.Beta;
018import com.google.common.annotations.GwtCompatible;
019import java.util.Comparator;
020import java.util.Iterator;
021import java.util.NavigableSet;
022import javax.annotation.CheckForNull;
023import org.checkerframework.checker.nullness.qual.Nullable;
024
025/**
026 * A sorted multiset which forwards all its method calls to another sorted multiset. Subclasses
027 * should override one or more methods to modify the behavior of the backing multiset as desired per
028 * the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
029 *
030 * <p><b>Warning:</b> The methods of {@code ForwardingSortedMultiset} forward
031 * <b>indiscriminately</b> to the methods of the delegate. For example, overriding {@link
032 * #add(Object, int)} alone <b>will not</b> change the behavior of {@link #add(Object)}, which can
033 * lead to unexpected behavior. In this case, you should override {@code add(Object)} as well,
034 * either providing your own implementation, or delegating to the provided {@code standardAdd}
035 * method.
036 *
037 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
038 * default} methods. Instead, it inherits their default implementations. When those implementations
039 * invoke methods, they invoke methods on the {@code ForwardingSortedMultiset}.
040 *
041 * <p>The {@code standard} methods and any collection views they return are not guaranteed to be
042 * thread-safe, even when all of the methods that they depend on are thread-safe.
043 *
044 * @author Louis Wasserman
045 * @since 15.0
046 */
047@Beta
048@GwtCompatible(emulated = true)
049@ElementTypesAreNonnullByDefault
050public abstract class ForwardingSortedMultiset<E extends @Nullable Object>
051    extends ForwardingMultiset<E> implements SortedMultiset<E> {
052  /** Constructor for use by subclasses. */
053  protected ForwardingSortedMultiset() {}
054
055  @Override
056  protected abstract SortedMultiset<E> delegate();
057
058  @Override
059  public NavigableSet<E> elementSet() {
060    return delegate().elementSet();
061  }
062
063  /**
064   * A sensible implementation of {@link SortedMultiset#elementSet} in terms of the following
065   * methods: {@link SortedMultiset#clear}, {@link SortedMultiset#comparator}, {@link
066   * SortedMultiset#contains}, {@link SortedMultiset#containsAll}, {@link SortedMultiset#count},
067   * {@link SortedMultiset#firstEntry} {@link SortedMultiset#headMultiset}, {@link
068   * SortedMultiset#isEmpty}, {@link SortedMultiset#lastEntry}, {@link SortedMultiset#subMultiset},
069   * {@link SortedMultiset#tailMultiset}, the {@code size()} and {@code iterator()} methods of
070   * {@link SortedMultiset#entrySet}, and {@link SortedMultiset#remove(Object, int)}. In many
071   * situations, you may wish to override {@link SortedMultiset#elementSet} to forward to this
072   * implementation or a subclass thereof.
073   *
074   * @since 15.0
075   */
076  protected class StandardElementSet extends SortedMultisets.NavigableElementSet<E> {
077    /** Constructor for use by subclasses. */
078    public StandardElementSet() {
079      super(ForwardingSortedMultiset.this);
080    }
081  }
082
083  @Override
084  public Comparator<? super E> comparator() {
085    return delegate().comparator();
086  }
087
088  @Override
089  public SortedMultiset<E> descendingMultiset() {
090    return delegate().descendingMultiset();
091  }
092
093  /**
094   * A skeleton implementation of a descending multiset view. Normally, {@link
095   * #descendingMultiset()} will not reflect any changes you make to the behavior of methods such as
096   * {@link #add(Object)} or {@link #pollFirstEntry}. This skeleton implementation correctly
097   * delegates each of its operations to the appropriate methods of this {@code
098   * ForwardingSortedMultiset}.
099   *
100   * <p>In many cases, you may wish to override {@link #descendingMultiset()} to return an instance
101   * of a subclass of {@code StandardDescendingMultiset}.
102   *
103   * @since 15.0
104   */
105  protected abstract class StandardDescendingMultiset extends DescendingMultiset<E> {
106    /** Constructor for use by subclasses. */
107    public StandardDescendingMultiset() {}
108
109    @Override
110    SortedMultiset<E> forwardMultiset() {
111      return ForwardingSortedMultiset.this;
112    }
113  }
114
115  @Override
116  @CheckForNull
117  public Entry<E> firstEntry() {
118    return delegate().firstEntry();
119  }
120
121  /**
122   * A sensible definition of {@link #firstEntry()} in terms of {@code entrySet().iterator()}.
123   *
124   * <p>If you override {@link #entrySet()}, you may wish to override {@link #firstEntry()} to
125   * forward to this implementation.
126   */
127  @CheckForNull
128  protected Entry<E> standardFirstEntry() {
129    Iterator<Entry<E>> entryIterator = entrySet().iterator();
130    if (!entryIterator.hasNext()) {
131      return null;
132    }
133    Entry<E> entry = entryIterator.next();
134    return Multisets.immutableEntry(entry.getElement(), entry.getCount());
135  }
136
137  @Override
138  @CheckForNull
139  public Entry<E> lastEntry() {
140    return delegate().lastEntry();
141  }
142
143  /**
144   * A sensible definition of {@link #lastEntry()} in terms of {@code
145   * descendingMultiset().entrySet().iterator()}.
146   *
147   * <p>If you override {@link #descendingMultiset} or {@link #entrySet()}, you may wish to override
148   * {@link #firstEntry()} to forward to this implementation.
149   */
150  @CheckForNull
151  protected Entry<E> standardLastEntry() {
152    Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator();
153    if (!entryIterator.hasNext()) {
154      return null;
155    }
156    Entry<E> entry = entryIterator.next();
157    return Multisets.immutableEntry(entry.getElement(), entry.getCount());
158  }
159
160  @Override
161  @CheckForNull
162  public Entry<E> pollFirstEntry() {
163    return delegate().pollFirstEntry();
164  }
165
166  /**
167   * A sensible definition of {@link #pollFirstEntry()} in terms of {@code entrySet().iterator()}.
168   *
169   * <p>If you override {@link #entrySet()}, you may wish to override {@link #pollFirstEntry()} to
170   * forward to this implementation.
171   */
172  @CheckForNull
173  protected Entry<E> standardPollFirstEntry() {
174    Iterator<Entry<E>> entryIterator = entrySet().iterator();
175    if (!entryIterator.hasNext()) {
176      return null;
177    }
178    Entry<E> entry = entryIterator.next();
179    entry = Multisets.immutableEntry(entry.getElement(), entry.getCount());
180    entryIterator.remove();
181    return entry;
182  }
183
184  @Override
185  @CheckForNull
186  public Entry<E> pollLastEntry() {
187    return delegate().pollLastEntry();
188  }
189
190  /**
191   * A sensible definition of {@link #pollLastEntry()} in terms of {@code
192   * descendingMultiset().entrySet().iterator()}.
193   *
194   * <p>If you override {@link #descendingMultiset()} or {@link #entrySet()}, you may wish to
195   * override {@link #pollLastEntry()} to forward to this implementation.
196   */
197  @CheckForNull
198  protected Entry<E> standardPollLastEntry() {
199    Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator();
200    if (!entryIterator.hasNext()) {
201      return null;
202    }
203    Entry<E> entry = entryIterator.next();
204    entry = Multisets.immutableEntry(entry.getElement(), entry.getCount());
205    entryIterator.remove();
206    return entry;
207  }
208
209  @Override
210  public SortedMultiset<E> headMultiset(@ParametricNullness E upperBound, BoundType boundType) {
211    return delegate().headMultiset(upperBound, boundType);
212  }
213
214  @Override
215  public SortedMultiset<E> subMultiset(
216      @ParametricNullness E lowerBound,
217      BoundType lowerBoundType,
218      @ParametricNullness E upperBound,
219      BoundType upperBoundType) {
220    return delegate().subMultiset(lowerBound, lowerBoundType, upperBound, upperBoundType);
221  }
222
223  /**
224   * A sensible definition of {@link #subMultiset(Object, BoundType, Object, BoundType)} in terms of
225   * {@link #headMultiset(Object, BoundType) headMultiset} and {@link #tailMultiset(Object,
226   * BoundType) tailMultiset}.
227   *
228   * <p>If you override either of these methods, you may wish to override {@link
229   * #subMultiset(Object, BoundType, Object, BoundType)} to forward to this implementation.
230   */
231  protected SortedMultiset<E> standardSubMultiset(
232      @ParametricNullness E lowerBound,
233      BoundType lowerBoundType,
234      @ParametricNullness E upperBound,
235      BoundType upperBoundType) {
236    return tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, upperBoundType);
237  }
238
239  @Override
240  public SortedMultiset<E> tailMultiset(@ParametricNullness E lowerBound, BoundType boundType) {
241    return delegate().tailMultiset(lowerBound, boundType);
242  }
243}