View Javadoc
1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the
10   * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11   * express or implied. See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package com.google.common.collect;
16  
17  import com.google.common.annotations.Beta;
18  import com.google.common.annotations.GwtCompatible;
19  import java.util.Comparator;
20  import java.util.Iterator;
21  import java.util.NavigableSet;
22  
23  /**
24   * A sorted multiset which forwards all its method calls to another sorted multiset. Subclasses
25   * should override one or more methods to modify the behavior of the backing multiset as desired
26   * per the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
27   *
28   * <p><b>Warning:</b> The methods of {@code ForwardingSortedMultiset} forward
29   * <b>indiscriminately</b> to the methods of the delegate. For example, overriding
30   * {@link #add(Object, int)} alone <b>will not</b> change the behavior of {@link #add(Object)},
31   * which can lead to unexpected behavior. In this case, you should override {@code add(Object)} as
32   * well, either providing your own implementation, or delegating to the provided {@code
33   * standardAdd} method.
34   *
35   * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
36   * default} methods. Instead, it inherits their default implementations. When those implementations
37   * invoke methods, they invoke methods on the {@code ForwardingSortedMultiset}.
38   *
39   * <p>The {@code standard} methods and any collection views they return are not guaranteed to be
40   * thread-safe, even when all of the methods that they depend on are thread-safe.
41   *
42   * @author Louis Wasserman
43   * @since 15.0
44   */
45  @Beta
46  @GwtCompatible(emulated = true)
47  public abstract class ForwardingSortedMultiset<E> extends ForwardingMultiset<E>
48      implements SortedMultiset<E> {
49    /** Constructor for use by subclasses. */
50    protected ForwardingSortedMultiset() {}
51  
52    @Override
53    protected abstract SortedMultiset<E> delegate();
54  
55    @Override
56    public NavigableSet<E> elementSet() {
57      return delegate().elementSet();
58    }
59  
60    /**
61     * A sensible implementation of {@link SortedMultiset#elementSet} in terms of the following
62     * methods: {@link SortedMultiset#clear}, {@link SortedMultiset#comparator}, {@link
63     * SortedMultiset#contains}, {@link SortedMultiset#containsAll}, {@link SortedMultiset#count},
64     * {@link SortedMultiset#firstEntry} {@link SortedMultiset#headMultiset}, {@link
65     * SortedMultiset#isEmpty}, {@link SortedMultiset#lastEntry}, {@link SortedMultiset#subMultiset},
66     * {@link SortedMultiset#tailMultiset}, the {@code size()} and {@code iterator()} methods of
67     * {@link SortedMultiset#entrySet}, and {@link SortedMultiset#remove(Object, int)}. In many
68     * situations, you may wish to override {@link SortedMultiset#elementSet} to forward to this
69     * implementation or a subclass thereof.
70     */
71    protected class StandardElementSet extends SortedMultisets.NavigableElementSet<E> {
72      /** Constructor for use by subclasses. */
73      public StandardElementSet() {
74        super(ForwardingSortedMultiset.this);
75      }
76    }
77  
78    @Override
79    public Comparator<? super E> comparator() {
80      return delegate().comparator();
81    }
82  
83    @Override
84    public SortedMultiset<E> descendingMultiset() {
85      return delegate().descendingMultiset();
86    }
87  
88    /**
89     * A skeleton implementation of a descending multiset view. Normally,
90     * {@link #descendingMultiset()} will not reflect any changes you make to the behavior of methods
91     * such as {@link #add(Object)} or {@link #pollFirstEntry}. This skeleton implementation
92     * correctly delegates each of its operations to the appropriate methods of this {@code
93     * ForwardingSortedMultiset}.
94     *
95     * In many cases, you may wish to override {@link #descendingMultiset()} to return an instance of
96     * a subclass of {@code StandardDescendingMultiset}.
97     */
98    protected abstract class StandardDescendingMultiset extends DescendingMultiset<E> {
99      /** Constructor for use by subclasses. */
100     public StandardDescendingMultiset() {}
101 
102     @Override
103     SortedMultiset<E> forwardMultiset() {
104       return ForwardingSortedMultiset.this;
105     }
106   }
107 
108   @Override
109   public Entry<E> firstEntry() {
110     return delegate().firstEntry();
111   }
112 
113   /**
114    * A sensible definition of {@link #firstEntry()} in terms of {@code entrySet().iterator()}.
115    *
116    * If you override {@link #entrySet()}, you may wish to override {@link #firstEntry()} to forward
117    * to this implementation.
118    */
119   protected Entry<E> standardFirstEntry() {
120     Iterator<Entry<E>> entryIterator = entrySet().iterator();
121     if (!entryIterator.hasNext()) {
122       return null;
123     }
124     Entry<E> entry = entryIterator.next();
125     return Multisets.immutableEntry(entry.getElement(), entry.getCount());
126   }
127 
128   @Override
129   public Entry<E> lastEntry() {
130     return delegate().lastEntry();
131   }
132 
133   /**
134    * A sensible definition of {@link #lastEntry()} in terms of {@code
135    * descendingMultiset().entrySet().iterator()}.
136    *
137    * If you override {@link #descendingMultiset} or {@link #entrySet()}, you may wish to override
138    * {@link #firstEntry()} to forward to this implementation.
139    */
140   protected Entry<E> standardLastEntry() {
141     Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator();
142     if (!entryIterator.hasNext()) {
143       return null;
144     }
145     Entry<E> entry = entryIterator.next();
146     return Multisets.immutableEntry(entry.getElement(), entry.getCount());
147   }
148 
149   @Override
150   public Entry<E> pollFirstEntry() {
151     return delegate().pollFirstEntry();
152   }
153 
154   /**
155    * A sensible definition of {@link #pollFirstEntry()} in terms of {@code entrySet().iterator()}.
156    *
157    * If you override {@link #entrySet()}, you may wish to override {@link #pollFirstEntry()} to
158    * forward to this implementation.
159    */
160   protected Entry<E> standardPollFirstEntry() {
161     Iterator<Entry<E>> entryIterator = entrySet().iterator();
162     if (!entryIterator.hasNext()) {
163       return null;
164     }
165     Entry<E> entry = entryIterator.next();
166     entry = Multisets.immutableEntry(entry.getElement(), entry.getCount());
167     entryIterator.remove();
168     return entry;
169   }
170 
171   @Override
172   public Entry<E> pollLastEntry() {
173     return delegate().pollLastEntry();
174   }
175 
176   /**
177    * A sensible definition of {@link #pollLastEntry()} in terms of {@code
178    * descendingMultiset().entrySet().iterator()}.
179    *
180    * If you override {@link #descendingMultiset()} or {@link #entrySet()}, you may wish to override
181    * {@link #pollLastEntry()} to forward to this implementation.
182    */
183   protected Entry<E> standardPollLastEntry() {
184     Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator();
185     if (!entryIterator.hasNext()) {
186       return null;
187     }
188     Entry<E> entry = entryIterator.next();
189     entry = Multisets.immutableEntry(entry.getElement(), entry.getCount());
190     entryIterator.remove();
191     return entry;
192   }
193 
194   @Override
195   public SortedMultiset<E> headMultiset(E upperBound, BoundType boundType) {
196     return delegate().headMultiset(upperBound, boundType);
197   }
198 
199   @Override
200   public SortedMultiset<E> subMultiset(
201       E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) {
202     return delegate().subMultiset(lowerBound, lowerBoundType, upperBound, upperBoundType);
203   }
204 
205   /**
206    * A sensible definition of {@link #subMultiset(Object, BoundType, Object, BoundType)} in terms
207    * of {@link #headMultiset(Object, BoundType) headMultiset} and
208    * {@link #tailMultiset(Object, BoundType) tailMultiset}.
209    *
210    * If you override either of these methods, you may wish to override
211    * {@link #subMultiset(Object, BoundType, Object, BoundType)} to forward to this implementation.
212    */
213   protected SortedMultiset<E> standardSubMultiset(
214       E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) {
215     return tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, upperBoundType);
216   }
217 
218   @Override
219   public SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType) {
220     return delegate().tailMultiset(lowerBound, boundType);
221   }
222 }