KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > collection > AbstractCollectionDecorator


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 org.apache.commons.collections.collection;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
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  * @since Commons Collections 3.0
37  * @version $Revision: 1.4 $ $Date: 2004/06/02 21:53:03 $
38  *
39  * @author Stephen Colebourne
40  * @author Paul Jack
41  */

42 public abstract class AbstractCollectionDecorator implements Collection JavaDoc {
43
44     /** The collection being decorated */
45     protected Collection JavaDoc 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 JavaDoc coll) {
62         if (coll == null) {
63             throw new IllegalArgumentException JavaDoc("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 JavaDoc getCollection() {
74         return collection;
75     }
76
77     //-----------------------------------------------------------------------
78
public boolean add(Object JavaDoc object) {
79         return collection.add(object);
80     }
81
82     public boolean addAll(Collection JavaDoc coll) {
83         return collection.addAll(coll);
84     }
85
86     public void clear() {
87         collection.clear();
88     }
89
90     public boolean contains(Object JavaDoc object) {
91         return collection.contains(object);
92     }
93
94     public boolean isEmpty() {
95         return collection.isEmpty();
96     }
97
98     public Iterator JavaDoc iterator() {
99         return collection.iterator();
100     }
101
102     public boolean remove(Object JavaDoc object) {
103         return collection.remove(object);
104     }
105
106     public int size() {
107         return collection.size();
108     }
109
110     public Object JavaDoc[] toArray() {
111         return collection.toArray();
112     }
113
114     public Object JavaDoc[] toArray(Object JavaDoc[] object) {
115         return collection.toArray(object);
116     }
117
118     public boolean containsAll(Collection JavaDoc coll) {
119         return collection.containsAll(coll);
120     }
121
122     public boolean removeAll(Collection JavaDoc coll) {
123         return collection.removeAll(coll);
124     }
125
126     public boolean retainAll(Collection JavaDoc coll) {
127         return collection.retainAll(coll);
128     }
129
130     public boolean equals(Object JavaDoc 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 JavaDoc toString() {
142         return collection.toString();
143     }
144
145 }
146
Popular Tags