001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.collection;
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020:
021: /**
022: * Decorates another <code>Collection</code> to provide additional behaviour.
023: * <p>
024: * Each method call made on this <code>Collection</code> is forwarded to the
025: * decorated <code>Collection</code>. This class is used as a framework on which
026: * to build to extensions such as synchronized and unmodifiable behaviour. The
027: * main advantage of decoration is that one decorator can wrap any implementation
028: * of <code>Collection</code>, whereas sub-classing requires a new class to be
029: * written for each implementation.
030: * <p>
031: * This implementation does not perform any special processing with
032: * {@link #iterator()}. Instead it simply returns the value from the
033: * wrapped collection. This may be undesirable, for example if you are trying
034: * to write an unmodifiable implementation it might provide a loophole.
035: *
036: * @since Commons Collections 3.0
037: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
038: *
039: * @author Stephen Colebourne
040: * @author Paul Jack
041: */
042: public abstract class AbstractCollectionDecorator implements Collection {
043:
044: /** The collection being decorated */
045: protected Collection collection;
046:
047: /**
048: * Constructor only used in deserialization, do not use otherwise.
049: * @since Commons Collections 3.1
050: */
051: protected AbstractCollectionDecorator() {
052: super ();
053: }
054:
055: /**
056: * Constructor that wraps (not copies).
057: *
058: * @param coll the collection to decorate, must not be null
059: * @throws IllegalArgumentException if the collection is null
060: */
061: protected AbstractCollectionDecorator(Collection coll) {
062: if (coll == null) {
063: throw new IllegalArgumentException(
064: "Collection must not be null");
065: }
066: this .collection = coll;
067: }
068:
069: /**
070: * Gets the collection being decorated.
071: *
072: * @return the decorated collection
073: */
074: protected Collection getCollection() {
075: return collection;
076: }
077:
078: //-----------------------------------------------------------------------
079: public boolean add(Object object) {
080: return collection.add(object);
081: }
082:
083: public boolean addAll(Collection coll) {
084: return collection.addAll(coll);
085: }
086:
087: public void clear() {
088: collection.clear();
089: }
090:
091: public boolean contains(Object object) {
092: return collection.contains(object);
093: }
094:
095: public boolean isEmpty() {
096: return collection.isEmpty();
097: }
098:
099: 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: }
|