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