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 */
054public abstract class AbstractCollectionDecorator<E>
055        implements Collection<E>, Serializable {
056
057    /** Serialization version */
058    private static final long serialVersionUID = 6249888059822088500L;
059
060    /** The collection being decorated */
061    private Collection<E> collection;
062
063    /**
064     * Constructor only used in deserialization, do not use otherwise.
065     * @since 3.1
066     */
067    protected AbstractCollectionDecorator() {
068        super();
069    }
070
071    /**
072     * Constructor that wraps (not copies).
073     *
074     * @param coll  the collection to decorate, must not be null
075     * @throws NullPointerException if the collection is null
076     */
077    protected AbstractCollectionDecorator(final Collection<E> coll) {
078        if (coll == null) {
079            throw new NullPointerException("Collection must not be null.");
080        }
081        this.collection = coll;
082    }
083
084    /**
085     * Gets the collection being decorated.
086     * All access to the decorated collection goes via this method.
087     *
088     * @return the decorated collection
089     */
090    protected Collection<E> decorated() {
091        return collection;
092    }
093
094    /**
095     * Sets the collection being decorated.
096     * <p>
097     * <b>NOTE:</b> this method should only be used during deserialization
098     *
099     * @param coll  the decorated collection
100     */
101    protected void setCollection(final Collection<E> coll) {
102        this.collection = coll;
103    }
104
105    //-----------------------------------------------------------------------
106
107    @Override
108    public boolean add(final E object) {
109        return decorated().add(object);
110    }
111
112    @Override
113    public boolean addAll(final Collection<? extends E> coll) {
114        return decorated().addAll(coll);
115    }
116
117    @Override
118    public void clear() {
119        decorated().clear();
120    }
121
122    @Override
123    public boolean contains(final Object object) {
124        return decorated().contains(object);
125    }
126
127    @Override
128    public boolean isEmpty() {
129        return decorated().isEmpty();
130    }
131
132    @Override
133    public Iterator<E> iterator() {
134        return decorated().iterator();
135    }
136
137    @Override
138    public boolean remove(final Object object) {
139        return decorated().remove(object);
140    }
141
142    @Override
143    public int size() {
144        return decorated().size();
145    }
146
147    @Override
148    public Object[] toArray() {
149        return decorated().toArray();
150    }
151
152    @Override
153    public <T> T[] toArray(final T[] object) {
154        return decorated().toArray(object);
155    }
156
157    @Override
158    public boolean containsAll(final Collection<?> coll) {
159        return decorated().containsAll(coll);
160    }
161
162    @Override
163    public boolean removeAll(final Collection<?> coll) {
164        return decorated().removeAll(coll);
165    }
166
167    @Override
168    public boolean retainAll(final Collection<?> coll) {
169        return decorated().retainAll(coll);
170    }
171
172    @Override
173    public String toString() {
174        return decorated().toString();
175    }
176
177}