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.Beta; 018import com.google.common.annotations.GwtCompatible; 019import java.util.Comparator; 020import java.util.Iterator; 021import java.util.NavigableSet; 022 023/** 024 * A sorted multiset which forwards all its method calls to another sorted multiset. Subclasses 025 * should override one or more methods to modify the behavior of the backing multiset as desired 026 * per the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 027 * 028 * <p><b>Warning:</b> The methods of {@code ForwardingSortedMultiset} forward 029 * <b>indiscriminately</b> to the methods of the delegate. For example, overriding 030 * {@link #add(Object, int)} alone <b>will not</b> change the behavior of {@link #add(Object)}, 031 * which can lead to unexpected behavior. In this case, you should override {@code add(Object)} as 032 * well, either providing your own implementation, or delegating to the provided {@code 033 * standardAdd} method. 034 * 035 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 036 * default} methods. Instead, it inherits their default implementations. When those implementations 037 * invoke methods, they invoke methods on the {@code ForwardingSortedMultiset}. 038 * 039 * <p>The {@code standard} methods and any collection views they return are not guaranteed to be 040 * thread-safe, even when all of the methods that they depend on are thread-safe. 041 * 042 * @author Louis Wasserman 043 * @since 15.0 044 */ 045@Beta 046@GwtCompatible(emulated = true) 047public abstract class ForwardingSortedMultiset<E> extends ForwardingMultiset<E> 048 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 protected class StandardElementSet extends SortedMultisets.NavigableElementSet<E> { 072 /** Constructor for use by subclasses. */ 073 public StandardElementSet() { 074 super(ForwardingSortedMultiset.this); 075 } 076 } 077 078 @Override 079 public Comparator<? super E> comparator() { 080 return delegate().comparator(); 081 } 082 083 @Override 084 public SortedMultiset<E> descendingMultiset() { 085 return delegate().descendingMultiset(); 086 } 087 088 /** 089 * A skeleton implementation of a descending multiset view. Normally, 090 * {@link #descendingMultiset()} will not reflect any changes you make to the behavior of methods 091 * such as {@link #add(Object)} or {@link #pollFirstEntry}. This skeleton implementation 092 * correctly delegates each of its operations to the appropriate methods of this {@code 093 * ForwardingSortedMultiset}. 094 * 095 * In many cases, you may wish to override {@link #descendingMultiset()} to return an instance of 096 * a subclass of {@code StandardDescendingMultiset}. 097 */ 098 protected abstract class StandardDescendingMultiset extends DescendingMultiset<E> { 099 /** Constructor for use by subclasses. */ 100 public StandardDescendingMultiset() {} 101 102 @Override 103 SortedMultiset<E> forwardMultiset() { 104 return ForwardingSortedMultiset.this; 105 } 106 } 107 108 @Override 109 public Entry<E> firstEntry() { 110 return delegate().firstEntry(); 111 } 112 113 /** 114 * A sensible definition of {@link #firstEntry()} in terms of {@code entrySet().iterator()}. 115 * 116 * If you override {@link #entrySet()}, you may wish to override {@link #firstEntry()} to forward 117 * to this implementation. 118 */ 119 protected Entry<E> standardFirstEntry() { 120 Iterator<Entry<E>> entryIterator = entrySet().iterator(); 121 if (!entryIterator.hasNext()) { 122 return null; 123 } 124 Entry<E> entry = entryIterator.next(); 125 return Multisets.immutableEntry(entry.getElement(), entry.getCount()); 126 } 127 128 @Override 129 public Entry<E> lastEntry() { 130 return delegate().lastEntry(); 131 } 132 133 /** 134 * A sensible definition of {@link #lastEntry()} in terms of {@code 135 * descendingMultiset().entrySet().iterator()}. 136 * 137 * If you override {@link #descendingMultiset} or {@link #entrySet()}, you may wish to override 138 * {@link #firstEntry()} to forward to this implementation. 139 */ 140 protected Entry<E> standardLastEntry() { 141 Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator(); 142 if (!entryIterator.hasNext()) { 143 return null; 144 } 145 Entry<E> entry = entryIterator.next(); 146 return Multisets.immutableEntry(entry.getElement(), entry.getCount()); 147 } 148 149 @Override 150 public Entry<E> pollFirstEntry() { 151 return delegate().pollFirstEntry(); 152 } 153 154 /** 155 * A sensible definition of {@link #pollFirstEntry()} in terms of {@code entrySet().iterator()}. 156 * 157 * If you override {@link #entrySet()}, you may wish to override {@link #pollFirstEntry()} to 158 * forward to this implementation. 159 */ 160 protected Entry<E> standardPollFirstEntry() { 161 Iterator<Entry<E>> entryIterator = entrySet().iterator(); 162 if (!entryIterator.hasNext()) { 163 return null; 164 } 165 Entry<E> entry = entryIterator.next(); 166 entry = Multisets.immutableEntry(entry.getElement(), entry.getCount()); 167 entryIterator.remove(); 168 return entry; 169 } 170 171 @Override 172 public Entry<E> pollLastEntry() { 173 return delegate().pollLastEntry(); 174 } 175 176 /** 177 * A sensible definition of {@link #pollLastEntry()} in terms of {@code 178 * descendingMultiset().entrySet().iterator()}. 179 * 180 * If you override {@link #descendingMultiset()} or {@link #entrySet()}, you may wish to override 181 * {@link #pollLastEntry()} to forward to this implementation. 182 */ 183 protected Entry<E> standardPollLastEntry() { 184 Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator(); 185 if (!entryIterator.hasNext()) { 186 return null; 187 } 188 Entry<E> entry = entryIterator.next(); 189 entry = Multisets.immutableEntry(entry.getElement(), entry.getCount()); 190 entryIterator.remove(); 191 return entry; 192 } 193 194 @Override 195 public SortedMultiset<E> headMultiset(E upperBound, BoundType boundType) { 196 return delegate().headMultiset(upperBound, boundType); 197 } 198 199 @Override 200 public SortedMultiset<E> subMultiset( 201 E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) { 202 return delegate().subMultiset(lowerBound, lowerBoundType, upperBound, upperBoundType); 203 } 204 205 /** 206 * A sensible definition of {@link #subMultiset(Object, BoundType, Object, BoundType)} in terms 207 * of {@link #headMultiset(Object, BoundType) headMultiset} and 208 * {@link #tailMultiset(Object, BoundType) tailMultiset}. 209 * 210 * If you override either of these methods, you may wish to override 211 * {@link #subMultiset(Object, BoundType, Object, BoundType)} to forward to this implementation. 212 */ 213 protected SortedMultiset<E> standardSubMultiset( 214 E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) { 215 return tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, upperBoundType); 216 } 217 218 @Override 219 public SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType) { 220 return delegate().tailMultiset(lowerBound, boundType); 221 } 222}