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 (imported from Google Collections Library) 046 */ 047@GwtCompatible 048public abstract class ForwardingCollection<E> extends ForwardingObject 049 implements Collection<E> { 050 // TODO(user): identify places where thread safety is actually lost 051 052 /** Constructor for use by subclasses. */ 053 protected ForwardingCollection() {} 054 055 @Override 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 for (Object o : collection) { 142 if (!contains(o)) { 143 return false; 144 } 145 } 146 return true; 147 } 148 149 /** 150 * A sensible definition of {@link #addAll} in terms of {@link #add}. If you 151 * override {@link #add}, you may wish to override {@link #addAll} to forward 152 * to this implementation. 153 * 154 * @since 7.0 155 */ 156 protected boolean standardAddAll(Collection<? extends E> collection) { 157 return Iterators.addAll(this, collection.iterator()); 158 } 159 160 /** 161 * A sensible definition of {@link #remove} in terms of {@link #iterator}, 162 * using the iterator's {@code remove} method. If you override {@link 163 * #iterator}, you may wish to override {@link #remove} to forward to this 164 * implementation. 165 * 166 * @since 7.0 167 */ 168 protected boolean standardRemove(@Nullable Object object) { 169 Iterator<E> iterator = iterator(); 170 while (iterator.hasNext()) { 171 if (Objects.equal(iterator.next(), object)) { 172 iterator.remove(); 173 return true; 174 } 175 } 176 return false; 177 } 178 179 /** 180 * A sensible definition of {@link #removeAll} 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 #removeAll} to forward to this 183 * implementation. 184 * 185 * @since 7.0 186 */ 187 protected boolean standardRemoveAll(Collection<?> collection) { 188 return Iterators.removeAll(iterator(), collection); 189 } 190 191 /** 192 * A sensible definition of {@link #retainAll} 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 #retainAll} to forward to this 195 * implementation. 196 * 197 * @since 7.0 198 */ 199 protected boolean standardRetainAll(Collection<?> collection) { 200 return Iterators.retainAll(iterator(), collection); 201 } 202 203 /** 204 * A sensible definition of {@link #clear} in terms of {@link #iterator}, 205 * using the iterator's {@code remove} method. If you override {@link 206 * #iterator}, you may wish to override {@link #clear} to forward to this 207 * implementation. 208 * 209 * @since 7.0 210 */ 211 protected void standardClear() { 212 Iterators.clear(iterator()); 213 } 214 215 /** 216 * A sensible definition of {@link #isEmpty} as {@code !iterator().hasNext}. 217 * If you override {@link #isEmpty}, you may wish to override {@link #isEmpty} 218 * to forward to this implementation. Alternately, it may be more efficient to 219 * implement {@code isEmpty} as {@code size() == 0}. 220 * 221 * @since 7.0 222 */ 223 protected boolean standardIsEmpty() { 224 return !iterator().hasNext(); 225 } 226 227 /** 228 * A sensible definition of {@link #toString} in terms of {@link #iterator}. 229 * If you override {@link #iterator}, you may wish to override {@link 230 * #toString} to forward to this implementation. 231 * 232 * @since 7.0 233 */ 234 protected String standardToString() { 235 return Collections2.toStringImpl(this); 236 } 237 238 /** 239 * A sensible definition of {@link #toArray()} in terms of {@link 240 * #toArray(Object[])}. If you override {@link #toArray(Object[])}, you may 241 * wish to override {@link #toArray} to forward to this implementation. 242 * 243 * @since 7.0 244 */ 245 protected Object[] standardToArray() { 246 Object[] newArray = new Object[size()]; 247 return toArray(newArray); 248 } 249 250 /** 251 * A sensible definition of {@link #toArray(Object[])} in terms of {@link 252 * #size} and {@link #iterator}. If you override either of these methods, you 253 * may wish to override {@link #toArray} to forward to this implementation. 254 * 255 * @since 7.0 256 */ 257 protected <T> T[] standardToArray(T[] array) { 258 return ObjectArrays.toArrayImpl(this, array); 259 } 260}