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;
022
023/**
024 * Decorates another <code>Collection</code> to provide additional behaviour.
025 * <p>
026 * Each method call made on this <code>Collection</code> is forwarded to the
027 * decorated <code>Collection</code>. This class is used as a framework on which
028 * to build to extensions such as synchronized and unmodifiable behaviour. The
029 * main advantage of decoration is that one decorator can wrap any implementation
030 * of <code>Collection</code>, whereas sub-classing requires a new class to be
031 * written for each implementation.
032 * <p>
033 * This implementation does not perform any special processing with
034 * {@link #iterator()}. Instead it simply returns the value from the
035 * wrapped collection. This may be undesirable, for example if you are trying
036 * to write an unmodifiable implementation it might provide a loophole.
037 * <p>
038 * This implementation does not forward the hashCode and equals methods through
039 * to the backing object, but relies on Object's implementation. This is necessary
040 * to preserve the symmetry of equals. Custom definitions of equality are usually
041 * based on an interface, such as Set or List, so that the implementation of equals
042 * can cast the object being tested for equality to the custom interface.
043 * AbstractCollectionDecorator does not implement such custom interfaces directly;
044 * they are implemented only in subclasses. Therefore, forwarding equals would break
045 * symmetry, as the forwarding object might consider itself equal to the object being
046 * tested, but the reverse could not be true. This behavior is consistent with the
047 * JDK's collection wrappers, such as {@link java.util.Collections#unmodifiableCollection(Collection)}.
048 * Use an interface-specific subclass of AbstractCollectionDecorator, such as
049 * AbstractListDecorator, to preserve equality behavior, or override equals directly.
050 *
051 * @param <E> the type of the elements in the collection
052 * @since 3.0
053 * @version $Id: AbstractCollectionDecorator.html 972397 2015-11-14 15:01:49Z tn $
054 */
055public abstract class AbstractCollectionDecorator<E>
056        implements Collection<E>, Serializable {
057
058    /** Serialization version */
059    private static final long serialVersionUID = 6249888059822088500L;
060
061    /** The collection being decorated */
062    private Collection<E> collection;
063
064    /**
065     * Constructor only used in deserialization, do not use otherwise.
066     * @since 3.1
067     */
068    protected AbstractCollectionDecorator() {
069        super();
070    }
071
072    /**
073     * Constructor that wraps (not copies).
074     *
075     * @param coll  the collection to decorate, must not be null
076     * @throws NullPointerException if the collection is null
077     */
078    protected AbstractCollectionDecorator(final Collection<E> coll) {
079        if (coll == null) {
080            throw new NullPointerException("Collection must not be null.");
081        }
082        this.collection = coll;
083    }
084
085    /**
086     * Gets the collection being decorated.
087     * All access to the decorated collection goes via this method.
088     *
089     * @return the decorated collection
090     */
091    protected Collection<E> decorated() {
092        return collection;
093    }
094
095    /**
096     * Sets the collection being decorated.
097     * <p>
098     * <b>NOTE:</b> this method should only be used during deserialization
099     *
100     * @param coll  the decorated collection
101     */
102    protected void setCollection(final Collection<E> coll) {
103        this.collection = coll;
104    }
105
106    //-----------------------------------------------------------------------
107
108    @Override
109    public boolean add(final E object) {
110        return decorated().add(object);
111    }
112
113    @Override
114    public boolean addAll(final Collection<? extends E> coll) {
115        return decorated().addAll(coll);
116    }
117
118    @Override
119    public void clear() {
120        decorated().clear();
121    }
122
123    @Override
124    public boolean contains(final Object object) {
125        return decorated().contains(object);
126    }
127
128    @Override
129    public boolean isEmpty() {
130        return decorated().isEmpty();
131    }
132
133    @Override
134    public Iterator<E> iterator() {
135        return decorated().iterator();
136    }
137
138    @Override
139    public boolean remove(final Object object) {
140        return decorated().remove(object);
141    }
142
143    @Override
144    public int size() {
145        return decorated().size();
146    }
147
148    @Override
149    public Object[] toArray() {
150        return decorated().toArray();
151    }
152
153    @Override
154    public <T> T[] toArray(final T[] object) {
155        return decorated().toArray(object);
156    }
157
158    @Override
159    public boolean containsAll(final Collection<?> coll) {
160        return decorated().containsAll(coll);
161    }
162
163    @Override
164    public boolean removeAll(final Collection<?> coll) {
165        return decorated().removeAll(coll);
166    }
167
168    @Override
169    public boolean retainAll(final Collection<?> coll) {
170        return decorated().retainAll(coll);
171    }
172
173    @Override
174    public String toString() {
175        return decorated().toString();
176    }
177
178}