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