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 extends DescendingMultiset<E> { 096 /** Constructor for use by subclasses. */ 097 public StandardDescendingMultiset() {} 098 099 @Override 100 SortedMultiset<E> forwardMultiset() { 101 return ForwardingSortedMultiset.this; 102 } 103 } 104 105 @Override 106 public Entry<E> firstEntry() { 107 return delegate().firstEntry(); 108 } 109 110 /** 111 * A sensible definition of {@link #firstEntry()} in terms of {@code entrySet().iterator()}. 112 * 113 * If you override {@link #entrySet()}, you may wish to override {@link #firstEntry()} to forward 114 * to this implementation. 115 */ 116 protected Entry<E> standardFirstEntry() { 117 Iterator<Entry<E>> entryIterator = entrySet().iterator(); 118 if (!entryIterator.hasNext()) { 119 return null; 120 } 121 Entry<E> entry = entryIterator.next(); 122 return Multisets.immutableEntry(entry.getElement(), entry.getCount()); 123 } 124 125 @Override 126 public Entry<E> lastEntry() { 127 return delegate().lastEntry(); 128 } 129 130 /** 131 * A sensible definition of {@link #lastEntry()} in terms of {@code 132 * descendingMultiset().entrySet().iterator()}. 133 * 134 * If you override {@link #descendingMultiset} or {@link #entrySet()}, you may wish to override 135 * {@link #firstEntry()} to forward to this implementation. 136 */ 137 protected Entry<E> standardLastEntry() { 138 Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator(); 139 if (!entryIterator.hasNext()) { 140 return null; 141 } 142 Entry<E> entry = entryIterator.next(); 143 return Multisets.immutableEntry(entry.getElement(), entry.getCount()); 144 } 145 146 @Override 147 public Entry<E> pollFirstEntry() { 148 return delegate().pollFirstEntry(); 149 } 150 151 /** 152 * A sensible definition of {@link #pollFirstEntry()} in terms of {@code entrySet().iterator()}. 153 * 154 * If you override {@link #entrySet()}, you may wish to override {@link #pollFirstEntry()} to 155 * forward to this implementation. 156 */ 157 protected Entry<E> standardPollFirstEntry() { 158 Iterator<Entry<E>> entryIterator = entrySet().iterator(); 159 if (!entryIterator.hasNext()) { 160 return null; 161 } 162 Entry<E> entry = entryIterator.next(); 163 entry = Multisets.immutableEntry(entry.getElement(), entry.getCount()); 164 entryIterator.remove(); 165 return entry; 166 } 167 168 @Override 169 public Entry<E> pollLastEntry() { 170 return delegate().pollLastEntry(); 171 } 172 173 /** 174 * A sensible definition of {@link #pollLastEntry()} in terms of {@code 175 * descendingMultiset().entrySet().iterator()}. 176 * 177 * If you override {@link #descendingMultiset()} or {@link #entrySet()}, you may wish to override 178 * {@link #pollLastEntry()} to forward to this implementation. 179 */ 180 protected Entry<E> standardPollLastEntry() { 181 Iterator<Entry<E>> entryIterator = descendingMultiset().entrySet().iterator(); 182 if (!entryIterator.hasNext()) { 183 return null; 184 } 185 Entry<E> entry = entryIterator.next(); 186 entry = Multisets.immutableEntry(entry.getElement(), entry.getCount()); 187 entryIterator.remove(); 188 return entry; 189 } 190 191 @Override 192 public SortedMultiset<E> headMultiset(E upperBound, BoundType boundType) { 193 return delegate().headMultiset(upperBound, boundType); 194 } 195 196 @Override 197 public SortedMultiset<E> subMultiset( 198 E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) { 199 return delegate().subMultiset(lowerBound, lowerBoundType, upperBound, upperBoundType); 200 } 201 202 /** 203 * A sensible definition of {@link #subMultiset(Object, BoundType, Object, BoundType)} in terms 204 * of {@link #headMultiset(Object, BoundType) headMultiset} and 205 * {@link #tailMultiset(Object, BoundType) tailMultiset}. 206 * 207 * If you override either of these methods, you may wish to override 208 * {@link #subMultiset(Object, BoundType, Object, BoundType)} to forward to this implementation. 209 */ 210 protected SortedMultiset<E> standardSubMultiset( 211 E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType) { 212 return tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, upperBoundType); 213 } 214 215 @Override 216 public SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType) { 217 return delegate().tailMultiset(lowerBound, boundType); 218 } 219}