001 /* 002 * Copyright (C) 2007 Google Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package com.google.common.collect; 018 019 import com.google.common.annotations.Beta; 020 import com.google.common.annotations.GwtCompatible; 021 import com.google.common.base.Objects; 022 023 import java.util.Collection; 024 import java.util.Iterator; 025 026 import javax.annotation.Nullable; 027 028 /** 029 * A collection which forwards all its method calls to another collection. 030 * Subclasses should override one or more methods to modify the behavior of the 031 * backing collection as desired per the <a 032 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 033 * 034 * <p><b>Warning:</b> The methods of {@code ForwardingCollection} forward 035 * <b>indiscriminately</b> to the methods of the delegate. For example, 036 * overriding {@link #add} alone <b>will not</b> change the behavior of {@link 037 * #addAll}, which can lead to unexpected behavior. In this case, you should 038 * override {@code addAll} as well, either providing your own implementation, or 039 * delegating to the provided {@code standardAddAll} method. 040 * 041 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even 042 * when all of the methods that they depend on are thread-safe. 043 * 044 * @author Kevin Bourrillion 045 * @author Louis Wasserman 046 * @since 2 (imported from Google Collections Library) 047 */ 048 @GwtCompatible 049 public abstract class ForwardingCollection<E> extends ForwardingObject 050 implements Collection<E> { 051 // TODO(user): identify places where thread safety is actually lost 052 053 /** Constructor for use by subclasses. */ 054 protected ForwardingCollection() {} 055 056 @Override protected abstract Collection<E> delegate(); 057 058 public Iterator<E> iterator() { 059 return delegate().iterator(); 060 } 061 062 public int size() { 063 return delegate().size(); 064 } 065 066 public boolean removeAll(Collection<?> collection) { 067 return delegate().removeAll(collection); 068 } 069 070 public boolean isEmpty() { 071 return delegate().isEmpty(); 072 } 073 074 public boolean contains(Object object) { 075 return delegate().contains(object); 076 } 077 078 public boolean add(E element) { 079 return delegate().add(element); 080 } 081 082 public boolean remove(Object object) { 083 return delegate().remove(object); 084 } 085 086 public boolean containsAll(Collection<?> collection) { 087 return delegate().containsAll(collection); 088 } 089 090 public boolean addAll(Collection<? extends E> collection) { 091 return delegate().addAll(collection); 092 } 093 094 public boolean retainAll(Collection<?> collection) { 095 return delegate().retainAll(collection); 096 } 097 098 public void clear() { 099 delegate().clear(); 100 } 101 102 public Object[] toArray() { 103 return delegate().toArray(); 104 } 105 106 public <T> T[] toArray(T[] array) { 107 return delegate().toArray(array); 108 } 109 110 /** 111 * A sensible definition of {@link #contains} in terms of {@link #iterator}. 112 * If you override {@link #iterator}, you may wish to override {@link 113 * #contains} to forward to this implementation. 114 * 115 * @since 7 116 */ 117 @Beta protected boolean standardContains(@Nullable Object object) { 118 return Iterators.contains(iterator(), object); 119 } 120 121 /** 122 * A sensible definition of {@link #containsAll} in terms of {@link #contains} 123 * . If you override {@link #contains}, you may wish to override {@link 124 * #containsAll} to forward to this implementation. 125 * 126 * @since 7 127 */ 128 @Beta protected boolean standardContainsAll(Collection<?> collection) { 129 for (Object o : collection) { 130 if (!contains(o)) { 131 return false; 132 } 133 } 134 return true; 135 } 136 137 /** 138 * A sensible definition of {@link #addAll} in terms of {@link #add}. If you 139 * override {@link #add}, you may wish to override {@link #addAll} to forward 140 * to this implementation. 141 * 142 * @since 7 143 */ 144 @Beta protected boolean standardAddAll(Collection<? extends E> collection) { 145 return Iterators.addAll(this, collection.iterator()); 146 } 147 148 /** 149 * A sensible definition of {@link #remove} in terms of {@link #iterator}, 150 * using the iterator's {@code remove} method. If you override {@link 151 * #iterator}, you may wish to override {@link #remove} to forward to this 152 * implementation. 153 * 154 * @since 7 155 */ 156 @Beta protected boolean standardRemove(@Nullable Object object) { 157 Iterator<E> iterator = iterator(); 158 while (iterator.hasNext()) { 159 if (Objects.equal(iterator.next(), object)) { 160 iterator.remove(); 161 return true; 162 } 163 } 164 return false; 165 } 166 167 /** 168 * A sensible definition of {@link #removeAll} in terms of {@link #iterator}, 169 * using the iterator's {@code remove} method. If you override {@link 170 * #iterator}, you may wish to override {@link #removeAll} to forward to this 171 * implementation. 172 * 173 * @since 7 174 */ 175 @Beta protected boolean standardRemoveAll(Collection<?> collection) { 176 return Iterators.removeAll(iterator(), collection); 177 } 178 179 /** 180 * A sensible definition of {@link #retainAll} in terms of {@link #iterator}, 181 * using the iterator's {@code remove} method. If you override {@link 182 * #iterator}, you may wish to override {@link #retainAll} to forward to this 183 * implementation. 184 * 185 * @since 7 186 */ 187 @Beta protected boolean standardRetainAll(Collection<?> collection) { 188 return Iterators.retainAll(iterator(), collection); 189 } 190 191 /** 192 * A sensible definition of {@link #clear} in terms of {@link #iterator}, 193 * using the iterator's {@code remove} method. If you override {@link 194 * #iterator}, you may wish to override {@link #clear} to forward to this 195 * implementation. 196 * 197 * @since 7 198 */ 199 @Beta protected void standardClear() { 200 Iterator<E> iterator = iterator(); 201 while (iterator.hasNext()) { 202 iterator.next(); 203 iterator.remove(); 204 } 205 } 206 207 /** 208 * A sensible definition of {@link #isEmpty} as {@code !iterator().hasNext}. 209 * If you override {@link #isEmpty}, you may wish to override {@link #isEmpty} 210 * to forward to this implementation. Alternately, it may be more efficient to 211 * implement {@code isEmpty} as {@code size() == 0}. 212 * 213 * @since 7 214 */ 215 @Beta protected boolean standardIsEmpty() { 216 return !iterator().hasNext(); 217 } 218 219 /** 220 * A sensible definition of {@link #toString} in terms of {@link #iterator}. 221 * If you override {@link #iterator}, you may wish to override {@link 222 * #toString} to forward to this implementation. 223 * 224 * @since 7 225 */ 226 @Beta protected String standardToString() { 227 return Collections2.toStringImpl(this); 228 } 229 230 /** 231 * A sensible definition of {@link #toArray()} in terms of {@link 232 * #toArray(Object[])}. If you override {@link #toArray(Object[])}, you may 233 * wish to override {@link #toArray} to forward to this implementation. 234 * 235 * @since 7 236 */ 237 @Beta protected Object[] standardToArray() { 238 Object[] newArray = new Object[size()]; 239 return toArray(newArray); 240 } 241 242 /** 243 * A sensible definition of {@link #toArray(Object[])} in terms of {@link 244 * #size} and {@link #iterator}. If you override either of these methods, you 245 * may wish to override {@link #toArray} to forward to this implementation. 246 * 247 * @since 7 248 */ 249 @Beta protected <T> T[] standardToArray(T[] array) { 250 return ObjectArrays.toArrayImpl(this, array); 251 } 252 }