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; 019 020import java.util.Comparator; 021import java.util.Iterator; 022import java.util.NavigableSet; 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 027 * per 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 031 * {@link #add(Object, int)} alone <b>will not</b> change the behavior of {@link #add(Object)}, 032 * which can lead to unexpected behavior. In this case, you should override {@code add(Object)} as 033 * well, either providing your own implementation, or delegating to the provided {@code 034 * standardAdd} method. 035 * 036 * <p>The {@code standard} methods and any collection views they return are not guaranteed to be 037 * thread-safe, even when all of the methods that they depend on are thread-safe. 038 * 039 * @author Louis Wasserman 040 * @since 15.0 041 */ 042@Beta 043@GwtCompatible(emulated = true) 044public abstract class ForwardingSortedMultiset<E> extends ForwardingMultiset<E> 045 implements SortedMultiset<E> { 046 /** Constructor for use by subclasses. */ 047 protected ForwardingSortedMultiset() {} 048 049 @Override 050 protected abstract SortedMultiset<E> delegate(); 051 052 @Override 053 public NavigableSet<E> elementSet() { 054 return (NavigableSet<E>) super.elementSet(); 055 } 056 057 /** 058 * A sensible implementation of {@link SortedMultiset#elementSet} in terms of the following 059 * methods: {@link SortedMultiset#clear}, {@link SortedMultiset#comparator}, {@link 060 * SortedMultiset#contains}, {@link SortedMultiset#containsAll}, {@link SortedMultiset#count}, 061 * {@link SortedMultiset#firstEntry} {@link SortedMultiset#headMultiset}, {@link 062 * SortedMultiset#isEmpty}, {@link SortedMultiset#lastEntry}, {@link SortedMultiset#subMultiset}, 063 * {@link SortedMultiset#tailMultiset}, the {@code size()} and {@code iterator()} methods of 064 * {@link SortedMultiset#entrySet}, and {@link SortedMultiset#remove(Object, int)}. In many 065 * situations, you may wish to override {@link SortedMultiset#elementSet} to forward to this 066 * implementation or a subclass thereof. 067 */ 068 protected class StandardElementSet extends SortedMultisets.NavigableElementSet<E> { 069 /** Constructor for use by subclasses. */ 070 public StandardElementSet() { 071 super(ForwardingSortedMultiset.this); 072 } 073 } 074 075 @Override 076 public Comparator<? super E> comparator() { 077 return delegate().comparator(); 078 } 079 080 @Override 081 public SortedMultiset<E> descendingMultiset() { 082 return delegate().descendingMultiset(); 083 } 084 085 /** 086 * A skeleton implementation of a descending multiset view. Normally, 087 * {@link #descendingMultiset()} will not reflect any changes you make to the behavior of methods 088 * such as {@link #add(Object)} or {@link #pollFirstEntry}. This skeleton implementation 089 * correctly delegates each of its operations to the appropriate methods of this {@code 090 * ForwardingSortedMultiset}. 091 * 092 * In many cases, you may wish to override {@link #descendingMultiset()} to return an instance of 093 * a subclass of {@code StandardDescendingMultiset}. 094 */ 095 protected abstract class StandardDescendingMultiset 096 extends DescendingMultiset<E> { 097 /** Constructor for use by subclasses. */ 098 public StandardDescendingMultiset() {} 099 100 @Override 101 SortedMultiset<E> forwardMultiset() { 102 return ForwardingSortedMultiset.this; 103 } 104 } 105 106 @Override 107 public Entry<E> firstEntry() { 108 return delegate().firstEntry(); 109 } 110 111 /** 112 * A sensible definition of {@link #firstEntry()} in terms of {@code entrySet().iterator()}. 113 * 114 * If you override {@link #entrySet()}, you may wish to override {@link #firstEntry()} to forward 115 * to this implementation. 116 */ 117 protected Entry<E> standardFirstEntry() { 118 Iterator<Entry<E>> entryIterator = entrySet().iterator(); 119 if (!entryIterator.hasNext()) { 120 return null; 121 } 122 Entry<E> entry = entryIterator.next(); 123 return Multisets.immutableEntry(entry.getElement(), entry.getCount()); 124 } 125 126 @Override 127 public Entry<E> lastEntry() { 128 return delegate().lastEntry(); 129 } 130 131 /** 132 * A sensible definition of {@link #lastEntry()} in terms of {@code 133 * descendingMultiset().entrySet().iterator()}. 134 * 135 * If you override {@link #descendingMultiset} or {@link #entrySet()}, you may wish to override 136 * {@link #firstEntry()} to forward to this implementation. 137 */ 138 protected Entry<E> standardLastEntry() { 139 Iterator<Entry<E>> entryIterator = descendingMultiset() 140 .entrySet() 141 .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() 185 .entrySet() 186 .iterator(); 187 if (!entryIterator.hasNext()) { 188 return null; 189 } 190 Entry<E> entry = entryIterator.next(); 191 entry = Multisets.immutableEntry(entry.getElement(), entry.getCount()); 192 entryIterator.remove(); 193 return entry; 194 } 195 196 @Override 197 public SortedMultiset<E> headMultiset(E upperBound, BoundType boundType) { 198 return delegate().headMultiset(upperBound, boundType); 199 } 200 201 @Override 202 public SortedMultiset<E> subMultiset( 203 E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) { 204 return delegate().subMultiset(lowerBound, lowerBoundType, upperBound, upperBoundType); 205 } 206 207 /** 208 * A sensible definition of {@link #subMultiset(Object, BoundType, Object, BoundType)} in terms 209 * of {@link #headMultiset(Object, BoundType) headMultiset} and 210 * {@link #tailMultiset(Object, BoundType) tailMultiset}. 211 * 212 * If you override either of these methods, you may wish to override 213 * {@link #subMultiset(Object, BoundType, Object, BoundType)} to forward to this implementation. 214 */ 215 protected SortedMultiset<E> standardSubMultiset( 216 E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) { 217 return tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, upperBoundType); 218 } 219 220 @Override 221 public SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType) { 222 return delegate().tailMultiset(lowerBound, boundType); 223 } 224 225}