KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.commons.collections.BoundedCollection;
22 import org.apache.commons.collections.iterators.UnmodifiableIterator;
23
24 /**
25  * <code>UnmodifiableBoundedCollection</code> decorates another
26  * <code>BoundedCollection</code> to ensure it can't be altered.
27  * <p>
28  * If a BoundedCollection is first wrapped in some other collection decorator,
29  * such as synchronized or predicated, the BoundedCollection methods are no
30  * longer accessible.
31  * The factory on this class will attempt to retrieve the bounded nature by
32  * examining the package scope variables.
33  * <p>
34  * This class is Serializable from Commons Collections 3.1.
35  *
36  * @since Commons Collections 3.0
37  * @version $Revision: 1.10 $ $Date: 2004/06/03 22:02:13 $
38  *
39  * @author Stephen Colebourne
40  */

41 public final class UnmodifiableBoundedCollection
42         extends AbstractSerializableCollectionDecorator
43         implements BoundedCollection {
44
45     /** Serialization version */
46     private static final long serialVersionUID = -7112672385450340330L;
47
48     /**
49      * Factory method to create an unmodifiable bounded collection.
50      *
51      * @param coll the <code>BoundedCollection</code> to decorate, must not be null
52      * @return a new unmodifiable bounded collection
53      * @throws IllegalArgumentException if bag is null
54      */

55     public static BoundedCollection decorate(BoundedCollection coll) {
56         return new UnmodifiableBoundedCollection(coll);
57     }
58     
59     /**
60      * Factory method to create an unmodifiable bounded collection.
61      * <p>
62      * This method is capable of drilling down through up to 1000 other decorators
63      * to find a suitable BoundedCollection.
64      *
65      * @param coll the <code>BoundedCollection</code> to decorate, must not be null
66      * @return a new unmodifiable bounded collection
67      * @throws IllegalArgumentException if bag is null
68      */

69     public static BoundedCollection decorateUsing(Collection JavaDoc coll) {
70         if (coll == null) {
71             throw new IllegalArgumentException JavaDoc("The collection must not be null");
72         }
73         
74         // handle decorators
75
for (int i = 0; i < 1000; i++) { // counter to prevent infinite looping
76
if (coll instanceof BoundedCollection) {
77                 break; // normal loop exit
78
} else if (coll instanceof AbstractCollectionDecorator) {
79                 coll = ((AbstractCollectionDecorator) coll).collection;
80             } else if (coll instanceof SynchronizedCollection) {
81                 coll = ((SynchronizedCollection) coll).collection;
82             } else {
83                 break; // normal loop exit
84
}
85         }
86             
87         if (coll instanceof BoundedCollection == false) {
88             throw new IllegalArgumentException JavaDoc("The collection is not a bounded collection");
89         }
90         return new UnmodifiableBoundedCollection((BoundedCollection) coll);
91     }
92     
93     /**
94      * Constructor that wraps (not copies).
95      *
96      * @param coll the collection to decorate, must not be null
97      * @throws IllegalArgumentException if coll is null
98      */

99     private UnmodifiableBoundedCollection(BoundedCollection coll) {
100         super(coll);
101     }
102
103     //-----------------------------------------------------------------------
104
public Iterator JavaDoc iterator() {
105         return UnmodifiableIterator.decorate(getCollection().iterator());
106     }
107
108     public boolean add(Object JavaDoc object) {
109         throw new UnsupportedOperationException JavaDoc();
110     }
111
112     public boolean addAll(Collection JavaDoc coll) {
113         throw new UnsupportedOperationException JavaDoc();
114     }
115
116     public void clear() {
117         throw new UnsupportedOperationException JavaDoc();
118     }
119
120     public boolean remove(Object JavaDoc object) {
121         throw new UnsupportedOperationException JavaDoc();
122     }
123
124     public boolean removeAll(Collection JavaDoc coll) {
125         throw new UnsupportedOperationException JavaDoc();
126     }
127
128     public boolean retainAll(Collection JavaDoc coll) {
129         throw new UnsupportedOperationException JavaDoc();
130     }
131
132     //-----------------------------------------------------------------------
133
public boolean isFull() {
134         return ((BoundedCollection) collection).isFull();
135     }
136
137     public int maxSize() {
138         return ((BoundedCollection) collection).maxSize();
139     }
140
141 }
142
Popular Tags