View Javadoc

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