Coverage Summary for Class: AbstractMultiset (com.google.common.collect)

Class Method, % Line, %
AbstractMultiset 36.8% (7/19) 42.3% (11/26)
AbstractMultiset$ElementSet 0% (0/3) 0% (0/3)
AbstractMultiset$EntrySet 75% (3/4) 75% (3/4)
Total 38.5% (10/26) 42.4% (14/33)


1 /* 2  * Copyright (C) 2007 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.collect; 18  19 import static com.google.common.collect.Multisets.setCountImpl; 20  21 import com.google.common.annotations.GwtCompatible; 22 import com.google.errorprone.annotations.CanIgnoreReturnValue; 23 import com.google.errorprone.annotations.concurrent.LazyInit; 24 import com.google.j2objc.annotations.WeakOuter; 25 import java.util.AbstractCollection; 26 import java.util.Collection; 27 import java.util.Iterator; 28 import java.util.Set; 29 import javax.annotation.CheckForNull; 30 import org.checkerframework.checker.nullness.qual.Nullable; 31  32 /** 33  * This class provides a skeletal implementation of the {@link Multiset} interface. A new multiset 34  * implementation can be created easily by extending this class and implementing the {@link 35  * Multiset#entrySet()} method, plus optionally overriding {@link #add(Object, int)} and {@link 36  * #remove(Object, int)} to enable modifications to the multiset. 37  * 38  * <p>The {@link #count} and {@link #size} implementations all iterate across the set returned by 39  * {@link Multiset#entrySet()}, as do many methods acting on the set returned by {@link 40  * #elementSet()}. Override those methods for better performance. 41  * 42  * @author Kevin Bourrillion 43  * @author Louis Wasserman 44  */ 45 @GwtCompatible 46 @ElementTypesAreNonnullByDefault 47 abstract class AbstractMultiset<E extends @Nullable Object> extends AbstractCollection<E> 48  implements Multiset<E> { 49  // Query Operations 50  51  @Override 52  public boolean isEmpty() { 53  return entrySet().isEmpty(); 54  } 55  56  @Override 57  public boolean contains(@CheckForNull Object element) { 58  return count(element) > 0; 59  } 60  61  // Modification Operations 62  @CanIgnoreReturnValue 63  @Override 64  public final boolean add(@ParametricNullness E element) { 65  add(element, 1); 66  return true; 67  } 68  69  @CanIgnoreReturnValue 70  @Override 71  public int add(@ParametricNullness E element, int occurrences) { 72  throw new UnsupportedOperationException(); 73  } 74  75  @CanIgnoreReturnValue 76  @Override 77  public final boolean remove(@CheckForNull Object element) { 78  return remove(element, 1) > 0; 79  } 80  81  @CanIgnoreReturnValue 82  @Override 83  public int remove(@CheckForNull Object element, int occurrences) { 84  throw new UnsupportedOperationException(); 85  } 86  87  @CanIgnoreReturnValue 88  @Override 89  public int setCount(@ParametricNullness E element, int count) { 90  return setCountImpl(this, element, count); 91  } 92  93  @CanIgnoreReturnValue 94  @Override 95  public boolean setCount(@ParametricNullness E element, int oldCount, int newCount) { 96  return setCountImpl(this, element, oldCount, newCount); 97  } 98  99  // Bulk Operations 100  101  /** 102  * {@inheritDoc} 103  * 104  * <p>This implementation is highly efficient when {@code elementsToAdd} is itself a {@link 105  * Multiset}. 106  */ 107  @CanIgnoreReturnValue 108  @Override 109  public final boolean addAll(Collection<? extends E> elementsToAdd) { 110  return Multisets.addAllImpl(this, elementsToAdd); 111  } 112  113  @CanIgnoreReturnValue 114  @Override 115  public final boolean removeAll(Collection<?> elementsToRemove) { 116  return Multisets.removeAllImpl(this, elementsToRemove); 117  } 118  119  @CanIgnoreReturnValue 120  @Override 121  public final boolean retainAll(Collection<?> elementsToRetain) { 122  return Multisets.retainAllImpl(this, elementsToRetain); 123  } 124  125  @Override 126  public abstract void clear(); 127  128  // Views 129  130  @LazyInit @CheckForNull private transient Set<E> elementSet; 131  132  @Override 133  public Set<E> elementSet() { 134  Set<E> result = elementSet; 135  if (result == null) { 136  elementSet = result = createElementSet(); 137  } 138  return result; 139  } 140  141  /** 142  * Creates a new instance of this multiset's element set, which will be returned by {@link 143  * #elementSet()}. 144  */ 145  Set<E> createElementSet() { 146  return new ElementSet(); 147  } 148  149  @WeakOuter 150  class ElementSet extends Multisets.ElementSet<E> { 151  @Override 152  Multiset<E> multiset() { 153  return AbstractMultiset.this; 154  } 155  156  @Override 157  public Iterator<E> iterator() { 158  return elementIterator(); 159  } 160  } 161  162  abstract Iterator<E> elementIterator(); 163  164  @LazyInit @CheckForNull private transient Set<Entry<E>> entrySet; 165  166  @Override 167  public Set<Entry<E>> entrySet() { 168  Set<Entry<E>> result = entrySet; 169  if (result == null) { 170  entrySet = result = createEntrySet(); 171  } 172  return result; 173  } 174  175  @WeakOuter 176  class EntrySet extends Multisets.EntrySet<E> { 177  @Override 178  Multiset<E> multiset() { 179  return AbstractMultiset.this; 180  } 181  182  @Override 183  public Iterator<Entry<E>> iterator() { 184  return entryIterator(); 185  } 186  187  @Override 188  public int size() { 189  return distinctElements(); 190  } 191  } 192  193  Set<Entry<E>> createEntrySet() { 194  return new EntrySet(); 195  } 196  197  abstract Iterator<Entry<E>> entryIterator(); 198  199  abstract int distinctElements(); 200  201  // Object methods 202  203  /** 204  * {@inheritDoc} 205  * 206  * <p>This implementation returns {@code true} if {@code object} is a multiset of the same size 207  * and if, for each element, the two multisets have the same count. 208  */ 209  @Override 210  public final boolean equals(@CheckForNull Object object) { 211  return Multisets.equalsImpl(this, object); 212  } 213  214  /** 215  * {@inheritDoc} 216  * 217  * <p>This implementation returns the hash code of {@link Multiset#entrySet()}. 218  */ 219  @Override 220  public final int hashCode() { 221  return entrySet().hashCode(); 222  } 223  224  /** 225  * {@inheritDoc} 226  * 227  * <p>This implementation returns the result of invoking {@code toString} on {@link 228  * Multiset#entrySet()}. 229  */ 230  @Override 231  public final String toString() { 232  return entrySet().toString(); 233  } 234 }