001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.collection;
018
019import java.io.Serializable;
020import java.util.Collection;
021import java.util.Iterator;
022import java.util.Objects;
023import java.util.function.Predicate;
024
025/**
026 * Decorates another {@code Collection} to provide additional behavior.
027 * <p>
028 * Each method call made on this {@code Collection} is forwarded to the
029 * decorated {@code Collection}. This class is used as a framework on which
030 * to build to extensions such as synchronized and unmodifiable behavior. The
031 * main advantage of decoration is that one decorator can wrap any implementation
032 * of {@code Collection}, whereas sub-classing requires a new class to be
033 * written for each implementation.
034 * </p>
035 * <p>
036 * This implementation does not perform any special processing with
037 * {@link #iterator()}. Instead it simply returns the value from the
038 * wrapped collection. This may be undesirable, for example if you are trying
039 * to write an unmodifiable implementation it might provide a loophole.
040 * </p>
041 * <p>
042 * This implementation does not forward the hashCode and equals methods through
043 * to the backing object, but relies on Object's implementation. This is necessary
044 * to preserve the symmetry of equals. Custom definitions of equality are usually
045 * based on an interface, such as Set or List, so that the implementation of equals
046 * can cast the object being tested for equality to the custom interface.
047 * AbstractCollectionDecorator does not implement such custom interfaces directly;
048 * they are implemented only in subclasses. Therefore, forwarding equals would break
049 * symmetry, as the forwarding object might consider itself equal to the object being
050 * tested, but the reverse could not be true. This behavior is consistent with the
051 * JDK's collection wrappers, such as {@link java.util.Collections#unmodifiableCollection(Collection)}.
052 * Use an interface-specific subclass of AbstractCollectionDecorator, such as
053 * AbstractListDecorator, to preserve equality behavior, or override equals directly.
054 * </p>
055 *
056 * @param <E> the type of the elements in the collection
057 * @since 3.0
058 */
059public abstract class AbstractCollectionDecorator<E>
060        implements Collection<E>, Serializable {
061
062    /** Serialization version */
063    private static final long serialVersionUID = 6249888059822088500L;
064
065    /** The collection being decorated */
066    private Collection<E> collection;
067
068    /**
069     * Constructor only used in deserialization, do not use otherwise.
070     * @since 3.1
071     */
072    protected AbstractCollectionDecorator() {
073    }
074
075    /**
076     * Constructor that wraps (not copies).
077     *
078     * @param collection  the collection to decorate, must not be null
079     * @throws NullPointerException if the collection is null
080     */
081    protected AbstractCollectionDecorator(final Collection<E> collection) {
082        this.collection = Objects.requireNonNull(collection, "collection");
083    }
084
085    @Override
086    public boolean add(final E object) {
087        return decorated().add(object);
088    }
089
090    @Override
091    public boolean addAll(final Collection<? extends E> coll) {
092        return decorated().addAll(coll);
093    }
094
095    @Override
096    public void clear() {
097        decorated().clear();
098    }
099
100    @Override
101    public boolean contains(final Object object) {
102        return decorated().contains(object);
103    }
104
105    @Override
106    public boolean containsAll(final Collection<?> coll) {
107        return decorated().containsAll(coll);
108    }
109
110    /**
111     * Gets the collection being decorated.
112     * All access to the decorated collection goes via this method.
113     *
114     * @return the decorated collection
115     */
116    protected Collection<E> decorated() {
117        return collection;
118    }
119
120    @Override
121    public boolean isEmpty() {
122        return decorated().isEmpty();
123    }
124
125    @Override
126    public Iterator<E> iterator() {
127        return decorated().iterator();
128    }
129
130    @Override
131    public boolean remove(final Object object) {
132        return decorated().remove(object);
133    }
134
135    @Override
136    public boolean removeAll(final Collection<?> coll) {
137        return decorated().removeAll(coll);
138    }
139
140    /**
141     * @since 4.4
142     */
143    @Override
144    public boolean removeIf(final Predicate<? super E> filter) {
145        return decorated().removeIf(filter);
146    }
147
148    @Override
149    public boolean retainAll(final Collection<?> coll) {
150        return decorated().retainAll(coll);
151    }
152
153    /**
154     * Sets the collection being decorated.
155     * <p>
156     * <b>NOTE:</b> this method should only be used during deserialization
157     *
158     * @param coll  the decorated collection
159     */
160    protected void setCollection(final Collection<E> coll) {
161        this.collection = coll;
162    }
163
164    @Override
165    public int size() {
166        return decorated().size();
167    }
168
169    @Override
170    public Object[] toArray() {
171        return decorated().toArray();
172    }
173
174    @Override
175    public <T> T[] toArray(final T[] object) {
176        return decorated().toArray(object);
177    }
178
179    @Override
180    public String toString() {
181        return decorated().toString();
182    }
183
184}