View Javadoc

1   /*
2    *  Copyright 2003-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.collections15.collection;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  
21  /***
22   * Decorates another <code>Collection</code> to provide additional behaviour.
23   * <p>
24   * Each method call made on this <code>Collection</code> is forwarded to the
25   * decorated <code>Collection</code>. This class is used as a framework on which
26   * to build to extensions such as synchronized and unmodifiable behaviour. The
27   * main advantage of decoration is that one decorator can wrap any implementation
28   * of <code>Collection</code>, whereas sub-classing requires a new class to be
29   * written for each implementation.
30   * <p>
31   * This implementation does not perform any special processing with
32   * {@link #iterator()}. Instead it simply returns the value from the 
33   * wrapped collection. This may be undesirable, for example if you are trying
34   * to write an unmodifiable implementation it might provide a loophole.
35   *
36   * @version $Revision: 1.1 $ $Date: 2005/05/03 22:45:38 $
37   * 
38   * @author Stephen Colebourne
39   * @author Paul Jack
40   * @since Collections15 1.0
41   */
42  public abstract class AbstractCollectionDecorator<E> implements Collection<E> {
43  
44      /*** The collection being decorated */
45      protected Collection<E> collection;
46  
47      /***
48       * Constructor only used in deserialization, do not use otherwise.
49       * @since Commons Collections 3.1
50       */
51      protected AbstractCollectionDecorator() {
52          super();
53      }
54  
55      /***
56       * Constructor that wraps (not copies).
57       * 
58       * @param coll  the collection to decorate, must not be null
59       * @throws IllegalArgumentException if the collection is null
60       */
61      protected AbstractCollectionDecorator(Collection<E> coll) {
62          if (coll == null) {
63              throw new IllegalArgumentException("Collection must not be null");
64          }
65          this.collection = coll;
66      }
67  
68      /***
69       * Gets the collection being decorated.
70       * 
71       * @return the decorated collection
72       */
73      protected Collection<E> getCollection() {
74          return collection;
75      }
76  
77      //-----------------------------------------------------------------------
78      public boolean add(E object) {
79          return collection.add(object);
80      }
81  
82      public boolean addAll(Collection<? extends E> coll) {
83          return collection.addAll(coll);
84      }
85  
86      public void clear() {
87          collection.clear();
88      }
89  
90      public boolean contains(Object object) {
91          return collection.contains(object);
92      }
93  
94      public boolean isEmpty() {
95          return collection.isEmpty();
96      }
97  
98      public Iterator<E> iterator() {
99          return collection.iterator();
100     }
101 
102     public boolean remove(Object object) {
103         return collection.remove(object);
104     }
105 
106     public int size() {
107         return collection.size();
108     }
109 
110     public Object[] toArray() {
111         return collection.toArray();
112     }
113 
114     public <T> T[] toArray(T[] object) {
115         return collection.toArray(object);
116     }
117 
118     public boolean containsAll(Collection<?> coll) {
119         return collection.containsAll(coll);
120     }
121 
122     public boolean removeAll(Collection<?> coll) {
123         return collection.removeAll(coll);
124     }
125 
126     public boolean retainAll(Collection<?> coll) {
127         return collection.retainAll(coll);
128     }
129 
130     public boolean equals(Object object) {
131         if (object == this) {
132             return true;
133         }
134         return collection.equals(object);
135     }
136 
137     public int hashCode() {
138         return collection.hashCode();
139     }
140 
141     public String toString() {
142         return collection.toString();
143     }
144 
145 }