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