001/* 002 * Copyright (C) 2011 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the 010 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 011 * express or implied. See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015package com.google.common.collect; 016 017import com.google.common.annotations.GwtCompatible; 018import java.util.Comparator; 019import java.util.Iterator; 020import java.util.NavigableSet; 021import javax.annotation.CheckForNull; 022import org.checkerframework.checker.nullness.qual.Nullable; 023 024/** 025 * A sorted multiset which forwards all its method calls to another sorted multiset. Subclasses 026 * should override one or more methods to modify the behavior of the backing multiset as desired per 027 * the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 028 * 029 * <p><b>Warning:</b> The methods of {@code ForwardingSortedMultiset} forward 030 * <b>indiscriminately</b> to the methods of the delegate. For example, overriding {@link 031 * #add(Object, int)} alone <b>will not</b> change the behavior of {@link #add(Object)}, which can 032 * lead to unexpected behavior. In this case, you should override {@code add(Object)} as well, 033 * either providing your own implementation, or delegating to the provided {@code standardAdd} 034 * method. 035 * 036 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 037 * default} methods. Instead, it inherits their default implementations. When those implementations 038 * invoke methods, they invoke methods on the {@code ForwardingSortedMultiset}. 039 * 040 * <p>The {@code standard} methods and any collection views they return are not guaranteed to be 041 * thread-safe, even when all of the methods that they depend on are thread-safe. 042 * 043 * @author Louis Wasserman 044 * @since 15.0 045 */ 046@GwtCompatible(emulated = true) 047public abstract class ForwardingSortedMultiset<E extends @Nullable Object> 048 extends ForwardingMultiset<E> implements SortedMultiset<E> { 049 /** Constructor for use by subclasses. */ 050 protected ForwardingSortedMultiset() {} 051 052 @Override 053 protected abstract SortedMultiset<E> delegate(); 054 055 @Override 056 public NavigableSet<E> elementSet() { 057 return delegate().elementSet(); 058 } 059 060 /** 061 * A sensible implementation of {@link SortedMultiset#elementSet} in terms of the following 062 * methods: {@link SortedMultiset#clear}, {@link SortedMultiset#comparator}, {@link 063 * SortedMultiset#contains}, {@link SortedMultiset#containsAll}, {@link SortedMultiset#count}, 064 * {@link SortedMultiset#firstEntry} {@link SortedMultiset#headMultiset}, {@link 065 * SortedMultiset#isEmpty}, {@link SortedMultiset#lastEntry}, {@link SortedMultiset#subMultiset}, 066 * {@link SortedMultiset#tailMultiset}, the {@code size()} and {@code iterator()} methods of 067 * {@link SortedMultiset#entrySet}, and {@link SortedMultiset#remove(Object, int)}. In many 068 * situations, you may wish to override {@link SortedMultiset#elementSet} to forward to this 069 * implementation or a subclass thereof. 070 * 071 * @since 15.0 072 */ 073 protected class StandardElementSet extends SortedMultisets.NavigableElementSet<E> { 074 /** Constructor for use by subclasses. */ 075 public StandardElementSet() { 076 super(ForwardingSortedMultiset.this); 077 } 078 } 079 080 @Override 081 public Comparator<? super E> comparator() { 082 return delegate().comparator(); 083 } 084 085 @Override 086 public SortedMultiset<E> descendingMultiset() { 087 return delegate().descendingMultiset(); 088 } 089 090 /** 091 * A skeleton implementation of a descending multiset view. Normally, {@link 092 * #descendingMultiset()} will not reflect any changes you make to the behavior of methods such as 093 * {@link #add(Object)} or {@link #pollFirstEntry}. This skeleton implementation correctly 094 * delegates each of its operations to the appropriate methods of this {@code 095 * ForwardingSortedMultiset}. 096 * 097 * <p>In many cases, you may wish to override {@link #descendingMultiset()} to return an instance 098 * of a subclass of {@code StandardDescendingMultiset}. 099 * 100 * @since 15.0 101 */ 102 protected abstract class StandardDescendingMultiset extends DescendingMultiset<E> { 103 /** Constructor for use by subclasses. */ 104 public StandardDescendingMultiset() {} 105 106 @Override 107 SortedMultiset<E> forwardMultiset() { 108 return ForwardingSortedMultiset.this; 109 } 110 } 111 112 @Override 113 @CheckForNull 114 public Entry<E> firstEntry() { 115 return delegate().firstEntry(); 116 } 117 118 /** 119 * A sensible definition of {@link #firstEntry()} in terms of {@code entrySet().iterator()}. 120 * 121 * <p>If you override {@link #entrySet()}, you may wish to override {@link #firstEntry()} to 122 * forward to this implementation. 123 */ 124 @CheckForNull 125 protected Entry<E> standardFirstEntry() { 126 Iterator<Entry<E>> entryIterator = entrySet().iterator(); 127 if (!entryIterator.hasNext()) { 128 return null; 129 } 130 Entry<E> entry = entryIterator.next(); 131 return Multisets.immutableEntry(entry.getElement(), entry.getCount()); 132 } 133 134 @Override 135 @CheckForNull 136 public Entry<E> lastEntry() { 137 return delegate().lastEntry(); 138 } 139 140 /** 141 * A sensible definition of {@link #lastEntry()} in terms of {@code 142 * descendingMultiset().entrySet().iterator()}. 143 * 144 * <p>If you override {@link #descendingMultiset} or {@link #entrySet()}, you may wish to override 145 * {@link #firstEntry()} to forward to this implementation. 146 */ 147 @CheckForNull 148 protected Entry<E> standardLastEntry() { 149 Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator(); 150 if (!entryIterator.hasNext()) { 151 return null; 152 } 153 Entry<E> entry = entryIterator.next(); 154 return Multisets.immutableEntry(entry.getElement(), entry.getCount()); 155 } 156 157 @Override 158 @CheckForNull 159 public Entry<E> pollFirstEntry() { 160 return delegate().pollFirstEntry(); 161 } 162 163 /** 164 * A sensible definition of {@link #pollFirstEntry()} in terms of {@code entrySet().iterator()}. 165 * 166 * <p>If you override {@link #entrySet()}, you may wish to override {@link #pollFirstEntry()} to 167 * forward to this implementation. 168 */ 169 @CheckForNull 170 protected Entry<E> standardPollFirstEntry() { 171 Iterator<Entry<E>> entryIterator = entrySet().iterator(); 172 if (!entryIterator.hasNext()) { 173 return null; 174 } 175 Entry<E> entry = entryIterator.next(); 176 entry = Multisets.immutableEntry(entry.getElement(), entry.getCount()); 177 entryIterator.remove(); 178 return entry; 179 } 180 181 @Override 182 @CheckForNull 183 public Entry<E> pollLastEntry() { 184 return delegate().pollLastEntry(); 185 } 186 187 /** 188 * A sensible definition of {@link #pollLastEntry()} in terms of {@code 189 * descendingMultiset().entrySet().iterator()}. 190 * 191 * <p>If you override {@link #descendingMultiset()} or {@link #entrySet()}, you may wish to 192 * override {@link #pollLastEntry()} to forward to this implementation. 193 */ 194 @CheckForNull 195 protected Entry<E> standardPollLastEntry() { 196 Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator(); 197 if (!entryIterator.hasNext()) { 198 return null; 199 } 200 Entry<E> entry = entryIterator.next(); 201 entry = Multisets.immutableEntry(entry.getElement(), entry.getCount()); 202 entryIterator.remove(); 203 return entry; 204 } 205 206 @Override 207 public SortedMultiset<E> headMultiset(@ParametricNullness E upperBound, BoundType boundType) { 208 return delegate().headMultiset(upperBound, boundType); 209 } 210 211 @Override 212 public SortedMultiset<E> subMultiset( 213 @ParametricNullness E lowerBound, 214 BoundType lowerBoundType, 215 @ParametricNullness E upperBound, 216 BoundType upperBoundType) { 217 return delegate().subMultiset(lowerBound, lowerBoundType, upperBound, upperBoundType); 218 } 219 220 /** 221 * A sensible definition of {@link #subMultiset(Object, BoundType, Object, BoundType)} in terms of 222 * {@link #headMultiset(Object, BoundType) headMultiset} and {@link #tailMultiset(Object, 223 * BoundType) tailMultiset}. 224 * 225 * <p>If you override either of these methods, you may wish to override {@link 226 * #subMultiset(Object, BoundType, Object, BoundType)} to forward to this implementation. 227 */ 228 protected SortedMultiset<E> standardSubMultiset( 229 @ParametricNullness E lowerBound, 230 BoundType lowerBoundType, 231 @ParametricNullness E upperBound, 232 BoundType upperBoundType) { 233 return tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, upperBoundType); 234 } 235 236 @Override 237 public SortedMultiset<E> tailMultiset(@ParametricNullness E lowerBound, BoundType boundType) { 238 return delegate().tailMultiset(lowerBound, boundType); 239 } 240}