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.io.Serializable;
20  import java.util.Collection;
21  
22  /**
23   * Decorates another <code>Collection</code> to provide additional behaviour
24   * without guaranteeing that the provided <code>Collection</code> type is the
25   * same as that of the decorated <code>Collection</code>.
26   * <p>
27   * Each untyped method call made on this <code>Collection</code> is forwarded to the
28   * decorated <code>Collection</code>. This class is used as a framework on which
29   * to build to extensions such as synchronized and unmodifiable behaviour. The
30   * main advantage of decoration is that one decorator can wrap any
31   * implementation of <code>Collection</code>, whereas sub-classing requires a
32   * new class to be written for each implementation.
33   * <p>
34   * This implementation does not perform any special processing with
35   * {@link #iterator()}. Instead it simply returns the value from the wrapped
36   * collection. This may be undesirable, for example if you are trying to write
37   * an unmodifiable implementation it might provide a loophole.
38   *
39   * @param <D> the type of the elements in the decorated collection
40   * @param <E> the element type of the Collection implementation
41   * @since 4.0
42   * @version $Id: AbstractUntypedCollectionDecorator.java 1429905 2013-01-07 17:15:14Z ggregory $
43   */
44  public abstract class AbstractUntypedCollectionDecorator<E, D> implements Collection<E>, Serializable {
45  
46      /** Serialization version */
47      private static final long serialVersionUID = -8016691444524268856L;
48  
49      /** The collection being decorated */
50      protected Collection<D> collection;
51  
52      /**
53       * Create a new AbstractUntypedCollectionDecorator.
54       */
55      public AbstractUntypedCollectionDecorator() {
56          super();
57      }
58  
59      /**
60       * Gets the collection being decorated. All access to the decorated
61       * collection goes via this method.
62       * 
63       * @return the decorated collection
64       */
65      protected Collection<D> decorated() {
66          return collection;
67      }
68  
69      public void clear() {
70          decorated().clear();
71      }
72  
73      public boolean contains(final Object object) {
74          return decorated().contains(object);
75      }
76  
77      public boolean isEmpty() {
78          return decorated().isEmpty();
79      }
80  
81      public boolean remove(final Object object) {
82          return decorated().remove(object);
83      }
84  
85      public int size() {
86          return decorated().size();
87      }
88  
89      public Object[] toArray() {
90          return decorated().toArray();
91      }
92  
93      public <T> T[] toArray(final T[] object) {
94          return decorated().toArray(object);
95      }
96  
97      public boolean containsAll(final Collection<?> coll) {
98          return decorated().containsAll(coll);
99      }
100 
101     public boolean removeAll(final Collection<?> coll) {
102         return decorated().removeAll(coll);
103     }
104 
105     public boolean retainAll(final Collection<?> coll) {
106         return decorated().retainAll(coll);
107     }
108 
109     @Override
110     public boolean equals(final Object object) {
111         return object == this || decorated().equals(object);
112     }
113 
114     @Override
115     public int hashCode() {
116         return decorated().hashCode();
117     }
118 
119     @Override
120     public String toString() {
121         return decorated().toString();
122     }
123 
124 }