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