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: import org.apache.commons.collections.BoundedCollection;
022: import org.apache.commons.collections.iterators.UnmodifiableIterator;
023:
024: /**
025: * <code>UnmodifiableBoundedCollection</code> decorates another
026: * <code>BoundedCollection</code> to ensure it can't be altered.
027: * <p>
028: * If a BoundedCollection is first wrapped in some other collection decorator,
029: * such as synchronized or predicated, the BoundedCollection methods are no
030: * longer accessible.
031: * The factory on this class will attempt to retrieve the bounded nature by
032: * examining the package scope variables.
033: * <p>
034: * This class is Serializable from Commons Collections 3.1.
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: */
041: public final class UnmodifiableBoundedCollection extends
042: AbstractSerializableCollectionDecorator implements
043: BoundedCollection {
044:
045: /** Serialization version */
046: private static final long serialVersionUID = -7112672385450340330L;
047:
048: /**
049: * Factory method to create an unmodifiable bounded collection.
050: *
051: * @param coll the <code>BoundedCollection</code> to decorate, must not be null
052: * @return a new unmodifiable bounded collection
053: * @throws IllegalArgumentException if bag is null
054: */
055: public static BoundedCollection decorate(BoundedCollection coll) {
056: return new UnmodifiableBoundedCollection(coll);
057: }
058:
059: /**
060: * Factory method to create an unmodifiable bounded collection.
061: * <p>
062: * This method is capable of drilling down through up to 1000 other decorators
063: * to find a suitable BoundedCollection.
064: *
065: * @param coll the <code>BoundedCollection</code> to decorate, must not be null
066: * @return a new unmodifiable bounded collection
067: * @throws IllegalArgumentException if bag is null
068: */
069: public static BoundedCollection decorateUsing(Collection coll) {
070: if (coll == null) {
071: throw new IllegalArgumentException(
072: "The collection must not be null");
073: }
074:
075: // handle decorators
076: for (int i = 0; i < 1000; i++) { // counter to prevent infinite looping
077: if (coll instanceof BoundedCollection) {
078: break; // normal loop exit
079: } else if (coll instanceof AbstractCollectionDecorator) {
080: coll = ((AbstractCollectionDecorator) coll).collection;
081: } else if (coll instanceof SynchronizedCollection) {
082: coll = ((SynchronizedCollection) coll).collection;
083: } else {
084: break; // normal loop exit
085: }
086: }
087:
088: if (coll instanceof BoundedCollection == false) {
089: throw new IllegalArgumentException(
090: "The collection is not a bounded collection");
091: }
092: return new UnmodifiableBoundedCollection(
093: (BoundedCollection) coll);
094: }
095:
096: /**
097: * Constructor that wraps (not copies).
098: *
099: * @param coll the collection to decorate, must not be null
100: * @throws IllegalArgumentException if coll is null
101: */
102: private UnmodifiableBoundedCollection(BoundedCollection coll) {
103: super (coll);
104: }
105:
106: //-----------------------------------------------------------------------
107: public Iterator iterator() {
108: return UnmodifiableIterator
109: .decorate(getCollection().iterator());
110: }
111:
112: public boolean add(Object object) {
113: throw new UnsupportedOperationException();
114: }
115:
116: public boolean addAll(Collection coll) {
117: throw new UnsupportedOperationException();
118: }
119:
120: public void clear() {
121: throw new UnsupportedOperationException();
122: }
123:
124: public boolean remove(Object object) {
125: throw new UnsupportedOperationException();
126: }
127:
128: public boolean removeAll(Collection coll) {
129: throw new UnsupportedOperationException();
130: }
131:
132: public boolean retainAll(Collection coll) {
133: throw new UnsupportedOperationException();
134: }
135:
136: //-----------------------------------------------------------------------
137: public boolean isFull() {
138: return ((BoundedCollection) collection).isFull();
139: }
140:
141: public int maxSize() {
142: return ((BoundedCollection) collection).maxSize();
143: }
144:
145: }
|