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
20 import java.util.Comparator;
21 import java.util.Iterator;
22 import java.util.NavigableSet;
23
24 /**
25 * A sorted multiset which forwards all its method calls to another sorted multiset. Subclasses
26 * should override one or more methods to modify the behavior of the backing multiset as desired
27 * per the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
28 *
29 * <p><b>Warning:</b> The methods of {@code ForwardingSortedMultiset} forward
30 * <b>indiscriminately</b> to the methods of the delegate. For example, overriding
31 * {@link #add(Object, int)} alone <b>will not</b> change the behavior of {@link #add(Object)},
32 * which can lead to unexpected behavior. In this case, you should override {@code add(Object)} as
33 * well, either providing your own implementation, or delegating to the provided {@code
34 * standardAdd} method.
35 *
36 * <p>The {@code standard} methods and any collection views they return are not guaranteed to be
37 * thread-safe, even when all of the methods that they depend on are thread-safe.
38 *
39 * @author Louis Wasserman
40 * @since 15.0
41 */
42 @Beta
43 @GwtCompatible(emulated = true)
44 public abstract class ForwardingSortedMultiset<E> extends ForwardingMultiset<E>
45 implements SortedMultiset<E> {
46 /** Constructor for use by subclasses. */
47 protected ForwardingSortedMultiset() {}
48
49 @Override
50 protected abstract SortedMultiset<E> delegate();
51
52 @Override
53 public NavigableSet<E> elementSet() {
54 return (NavigableSet<E>) super.elementSet();
55 }
56
57 /**
58 * A sensible implementation of {@link SortedMultiset#elementSet} in terms of the following
59 * methods: {@link SortedMultiset#clear}, {@link SortedMultiset#comparator}, {@link
60 * SortedMultiset#contains}, {@link SortedMultiset#containsAll}, {@link SortedMultiset#count},
61 * {@link SortedMultiset#firstEntry} {@link SortedMultiset#headMultiset}, {@link
62 * SortedMultiset#isEmpty}, {@link SortedMultiset#lastEntry}, {@link SortedMultiset#subMultiset},
63 * {@link SortedMultiset#tailMultiset}, the {@code size()} and {@code iterator()} methods of
64 * {@link SortedMultiset#entrySet}, and {@link SortedMultiset#remove(Object, int)}. In many
65 * situations, you may wish to override {@link SortedMultiset#elementSet} to forward to this
66 * implementation or a subclass thereof.
67 */
68 protected class StandardElementSet extends SortedMultisets.NavigableElementSet<E> {
69 /** Constructor for use by subclasses. */
70 public StandardElementSet() {
71 super(ForwardingSortedMultiset.this);
72 }
73 }
74
75 @Override
76 public Comparator<? super E> comparator() {
77 return delegate().comparator();
78 }
79
80 @Override
81 public SortedMultiset<E> descendingMultiset() {
82 return delegate().descendingMultiset();
83 }
84
85 /**
86 * A skeleton implementation of a descending multiset view. Normally,
87 * {@link #descendingMultiset()} will not reflect any changes you make to the behavior of methods
88 * such as {@link #add(Object)} or {@link #pollFirstEntry}. This skeleton implementation
89 * correctly delegates each of its operations to the appropriate methods of this {@code
90 * ForwardingSortedMultiset}.
91 *
92 * In many cases, you may wish to override {@link #descendingMultiset()} to return an instance of
93 * a subclass of {@code StandardDescendingMultiset}.
94 */
95 protected abstract class StandardDescendingMultiset
96 extends DescendingMultiset<E> {
97 /** Constructor for use by subclasses. */
98 public StandardDescendingMultiset() {}
99
100 @Override
101 SortedMultiset<E> forwardMultiset() {
102 return ForwardingSortedMultiset.this;
103 }
104 }
105
106 @Override
107 public Entry<E> firstEntry() {
108 return delegate().firstEntry();
109 }
110
111 /**
112 * A sensible definition of {@link #firstEntry()} in terms of {@code entrySet().iterator()}.
113 *
114 * If you override {@link #entrySet()}, you may wish to override {@link #firstEntry()} to forward
115 * to this implementation.
116 */
117 protected Entry<E> standardFirstEntry() {
118 Iterator<Entry<E>> entryIterator = entrySet().iterator();
119 if (!entryIterator.hasNext()) {
120 return null;
121 }
122 Entry<E> entry = entryIterator.next();
123 return Multisets.immutableEntry(entry.getElement(), entry.getCount());
124 }
125
126 @Override
127 public Entry<E> lastEntry() {
128 return delegate().lastEntry();
129 }
130
131 /**
132 * A sensible definition of {@link #lastEntry()} in terms of {@code
133 * descendingMultiset().entrySet().iterator()}.
134 *
135 * If you override {@link #descendingMultiset} or {@link #entrySet()}, you may wish to override
136 * {@link #firstEntry()} to forward to this implementation.
137 */
138 protected Entry<E> standardLastEntry() {
139 Iterator<Entry<E>> entryIterator = descendingMultiset()
140 .entrySet()
141 .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()
185 .entrySet()
186 .iterator();
187 if (!entryIterator.hasNext()) {
188 return null;
189 }
190 Entry<E> entry = entryIterator.next();
191 entry = Multisets.immutableEntry(entry.getElement(), entry.getCount());
192 entryIterator.remove();
193 return entry;
194 }
195
196 @Override
197 public SortedMultiset<E> headMultiset(E upperBound, BoundType boundType) {
198 return delegate().headMultiset(upperBound, boundType);
199 }
200
201 @Override
202 public SortedMultiset<E> subMultiset(
203 E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) {
204 return delegate().subMultiset(lowerBound, lowerBoundType, upperBound, upperBoundType);
205 }
206
207 /**
208 * A sensible definition of {@link #subMultiset(Object, BoundType, Object, BoundType)} in terms
209 * of {@link #headMultiset(Object, BoundType) headMultiset} and
210 * {@link #tailMultiset(Object, BoundType) tailMultiset}.
211 *
212 * If you override either of these methods, you may wish to override
213 * {@link #subMultiset(Object, BoundType, Object, BoundType)} to forward to this implementation.
214 */
215 protected SortedMultiset<E> standardSubMultiset(
216 E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) {
217 return tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, upperBoundType);
218 }
219
220 @Override
221 public SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType) {
222 return delegate().tailMultiset(lowerBound, boundType);
223 }
224
225 }