001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import com.google.common.annotations.GwtCompatible; 020import com.google.common.base.Objects; 021import com.google.errorprone.annotations.CanIgnoreReturnValue; 022import java.util.Collection; 023import java.util.Iterator; 024import java.util.Set; 025import org.checkerframework.checker.nullness.qual.Nullable; 026 027/** 028 * A multiset which forwards all its method calls to another multiset. Subclasses should override 029 * one or more methods to modify the behavior of the backing multiset as desired per the <a 030 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 031 * 032 * <p><b>Warning:</b> The methods of {@code ForwardingMultiset} forward <b>indiscriminately</b> to 033 * the methods of the delegate. For example, overriding {@link #add(Object, int)} alone <b>will 034 * not</b> change the behavior of {@link #add(Object)}, which can lead to unexpected behavior. In 035 * this case, you should override {@code add(Object)} as well, either providing your own 036 * implementation, or delegating to the provided {@code standardAdd} method. 037 * 038 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 039 * default} methods. Instead, it inherits their default implementations. When those implementations 040 * invoke methods, they invoke methods on the {@code ForwardingMultiset}. 041 * 042 * <p>The {@code standard} methods and any collection views they return are not guaranteed to be 043 * thread-safe, even when all of the methods that they depend on are thread-safe. 044 * 045 * @author Kevin Bourrillion 046 * @author Louis Wasserman 047 * @since 2.0 048 */ 049@GwtCompatible 050public abstract class ForwardingMultiset<E extends @Nullable Object> extends ForwardingCollection<E> 051 implements Multiset<E> { 052 053 /** Constructor for use by subclasses. */ 054 protected ForwardingMultiset() {} 055 056 @Override 057 protected abstract Multiset<E> delegate(); 058 059 @Override 060 public int count(@Nullable Object element) { 061 return delegate().count(element); 062 } 063 064 @CanIgnoreReturnValue 065 @Override 066 public int add(@ParametricNullness E element, int occurrences) { 067 return delegate().add(element, occurrences); 068 } 069 070 @CanIgnoreReturnValue 071 @Override 072 public int remove(@Nullable Object element, int occurrences) { 073 return delegate().remove(element, occurrences); 074 } 075 076 @Override 077 public Set<E> elementSet() { 078 return delegate().elementSet(); 079 } 080 081 @Override 082 public Set<Entry<E>> entrySet() { 083 return delegate().entrySet(); 084 } 085 086 @Override 087 public boolean equals(@Nullable Object object) { 088 return object == this || delegate().equals(object); 089 } 090 091 @Override 092 public int hashCode() { 093 return delegate().hashCode(); 094 } 095 096 @CanIgnoreReturnValue 097 @Override 098 public int setCount(@ParametricNullness E element, int count) { 099 return delegate().setCount(element, count); 100 } 101 102 @CanIgnoreReturnValue 103 @Override 104 public boolean setCount(@ParametricNullness E element, int oldCount, int newCount) { 105 return delegate().setCount(element, oldCount, newCount); 106 } 107 108 /** 109 * A sensible definition of {@link #contains} in terms of {@link #count}. If you override {@link 110 * #count}, you may wish to override {@link #contains} to forward to this implementation. 111 * 112 * @since 7.0 113 */ 114 @Override 115 protected boolean standardContains(@Nullable Object object) { 116 return count(object) > 0; 117 } 118 119 /** 120 * A sensible definition of {@link #clear} in terms of the {@code iterator} method of {@link 121 * #entrySet}. If you override {@link #entrySet}, you may wish to override {@link #clear} to 122 * forward to this implementation. 123 * 124 * @since 7.0 125 */ 126 @Override 127 protected void standardClear() { 128 Iterators.clear(entrySet().iterator()); 129 } 130 131 /** 132 * A sensible, albeit inefficient, definition of {@link #count} in terms of {@link #entrySet}. If 133 * you override {@link #entrySet}, you may wish to override {@link #count} to forward to this 134 * implementation. 135 * 136 * @since 7.0 137 */ 138 protected int standardCount(@Nullable Object object) { 139 for (Entry<?> entry : this.entrySet()) { 140 if (Objects.equal(entry.getElement(), object)) { 141 return entry.getCount(); 142 } 143 } 144 return 0; 145 } 146 147 /** 148 * A sensible definition of {@link #add(Object)} in terms of {@link #add(Object, int)}. If you 149 * override {@link #add(Object, int)}, you may wish to override {@link #add(Object)} to forward to 150 * this implementation. 151 * 152 * @since 7.0 153 */ 154 protected boolean standardAdd(@ParametricNullness E element) { 155 add(element, 1); 156 return true; 157 } 158 159 /** 160 * A sensible definition of {@link #addAll(Collection)} in terms of {@link #add(Object)} and 161 * {@link #add(Object, int)}. If you override either of these methods, you may wish to override 162 * {@link #addAll(Collection)} to forward to this implementation. 163 * 164 * @since 7.0 165 */ 166 @Override 167 protected boolean standardAddAll(Collection<? extends E> elementsToAdd) { 168 return Multisets.addAllImpl(this, elementsToAdd); 169 } 170 171 /** 172 * A sensible definition of {@link #remove(Object)} in terms of {@link #remove(Object, int)}. If 173 * you override {@link #remove(Object, int)}, you may wish to override {@link #remove(Object)} to 174 * forward to this implementation. 175 * 176 * @since 7.0 177 */ 178 @Override 179 protected boolean standardRemove(@Nullable Object element) { 180 return remove(element, 1) > 0; 181 } 182 183 /** 184 * A sensible definition of {@link #removeAll} in terms of the {@code removeAll} method of {@link 185 * #elementSet}. If you override {@link #elementSet}, you may wish to override {@link #removeAll} 186 * to forward to this implementation. 187 * 188 * @since 7.0 189 */ 190 @Override 191 protected boolean standardRemoveAll(Collection<?> elementsToRemove) { 192 return Multisets.removeAllImpl(this, elementsToRemove); 193 } 194 195 /** 196 * A sensible definition of {@link #retainAll} in terms of the {@code retainAll} method of {@link 197 * #elementSet}. If you override {@link #elementSet}, you may wish to override {@link #retainAll} 198 * to forward to this implementation. 199 * 200 * @since 7.0 201 */ 202 @Override 203 protected boolean standardRetainAll(Collection<?> elementsToRetain) { 204 return Multisets.retainAllImpl(this, elementsToRetain); 205 } 206 207 /** 208 * A sensible definition of {@link #setCount(Object, int)} in terms of {@link #count(Object)}, 209 * {@link #add(Object, int)}, and {@link #remove(Object, int)}. {@link #entrySet()}. If you 210 * override any of these methods, you may wish to override {@link #setCount(Object, int)} to 211 * forward to this implementation. 212 * 213 * @since 7.0 214 */ 215 protected int standardSetCount(@ParametricNullness E element, int count) { 216 return Multisets.setCountImpl(this, element, count); 217 } 218 219 /** 220 * A sensible definition of {@link #setCount(Object, int, int)} in terms of {@link #count(Object)} 221 * and {@link #setCount(Object, int)}. If you override either of these methods, you may wish to 222 * override {@link #setCount(Object, int, int)} to forward to this implementation. 223 * 224 * @since 7.0 225 */ 226 protected boolean standardSetCount(@ParametricNullness E element, int oldCount, int newCount) { 227 return Multisets.setCountImpl(this, element, oldCount, newCount); 228 } 229 230 /** 231 * A sensible implementation of {@link Multiset#elementSet} in terms of the following methods: 232 * {@link ForwardingMultiset#clear}, {@link ForwardingMultiset#contains}, {@link 233 * ForwardingMultiset#containsAll}, {@link ForwardingMultiset#count}, {@link 234 * ForwardingMultiset#isEmpty}, the {@link Set#size} and {@link Set#iterator} methods of {@link 235 * ForwardingMultiset#entrySet}, and {@link ForwardingMultiset#remove(Object, int)}. In many 236 * situations, you may wish to override {@link ForwardingMultiset#elementSet} to forward to this 237 * implementation or a subclass thereof. 238 * 239 * @since 10.0 240 */ 241 protected class StandardElementSet extends Multisets.ElementSet<E> { 242 /** Constructor for use by subclasses. */ 243 public StandardElementSet() {} 244 245 @Override 246 Multiset<E> multiset() { 247 return ForwardingMultiset.this; 248 } 249 250 @Override 251 public Iterator<E> iterator() { 252 return Multisets.elementIterator(multiset().entrySet().iterator()); 253 } 254 } 255 256 /** 257 * A sensible definition of {@link #iterator} in terms of {@link #entrySet} and {@link 258 * #remove(Object)}. If you override either of these methods, you may wish to override {@link 259 * #iterator} to forward to this implementation. 260 * 261 * @since 7.0 262 */ 263 protected Iterator<E> standardIterator() { 264 return Multisets.iteratorImpl(this); 265 } 266 267 /** 268 * A sensible, albeit inefficient, definition of {@link #size} in terms of {@link #entrySet}. If 269 * you override {@link #entrySet}, you may wish to override {@link #size} to forward to this 270 * implementation. 271 * 272 * @since 7.0 273 */ 274 protected int standardSize() { 275 return Multisets.linearTimeSizeImpl(this); 276 } 277 278 /** 279 * A sensible, albeit inefficient, definition of {@link #equals} in terms of {@code 280 * entrySet().size()} and {@link #count}. If you override either of these methods, you may wish to 281 * override {@link #equals} to forward to this implementation. 282 * 283 * @since 7.0 284 */ 285 protected boolean standardEquals(@Nullable Object object) { 286 return Multisets.equalsImpl(this, object); 287 } 288 289 /** 290 * A sensible definition of {@link #hashCode} as {@code entrySet().hashCode()} . If you override 291 * {@link #entrySet}, you may wish to override {@link #hashCode} to forward to this 292 * implementation. 293 * 294 * @since 7.0 295 */ 296 protected int standardHashCode() { 297 return entrySet().hashCode(); 298 } 299 300 /** 301 * A sensible definition of {@link #toString} as {@code entrySet().toString()} . If you override 302 * {@link #entrySet}, you may wish to override {@link #toString} to forward to this 303 * implementation. 304 * 305 * @since 7.0 306 */ 307 @Override 308 protected String standardToString() { 309 return entrySet().toString(); 310 } 311}