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

Class Method, % Line, %
ForwardingSortedMultiset 0% (0/16) 0% (0/36)
ForwardingSortedMultiset$StandardDescendingMultiset 0% (0/2) 0% (0/2)
ForwardingSortedMultiset$StandardElementSet 0% (0/1) 0% (0/2)
Total 0% (0/19) 0% (0/40)


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