01: /*
02: * Copyright 2003-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
19: import java.util.Iterator;
20:
21: import org.apache.commons.collections.Unmodifiable;
22: import org.apache.commons.collections.iterators.UnmodifiableIterator;
23:
24: /**
25: * Decorates another <code>Collection</code> to ensure it can't be altered.
26: * <p>
27: * This class is Serializable from Commons Collections 3.1.
28: *
29: * @since Commons Collections 3.0
30: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
31: *
32: * @author Stephen Colebourne
33: */
34: public final class UnmodifiableCollection extends
35: AbstractSerializableCollectionDecorator implements Unmodifiable {
36:
37: /** Serialization version */
38: private static final long serialVersionUID = -239892006883819945L;
39:
40: /**
41: * Factory method to create an unmodifiable collection.
42: * <p>
43: * If the collection passed in is already unmodifiable, it is returned.
44: *
45: * @param coll the collection to decorate, must not be null
46: * @return an unmodifiable collection
47: * @throws IllegalArgumentException if collection is null
48: */
49: public static Collection decorate(Collection coll) {
50: if (coll instanceof Unmodifiable) {
51: return coll;
52: }
53: return new UnmodifiableCollection(coll);
54: }
55:
56: //-----------------------------------------------------------------------
57: /**
58: * Constructor that wraps (not copies).
59: *
60: * @param coll the collection to decorate, must not be null
61: * @throws IllegalArgumentException if collection is null
62: */
63: private UnmodifiableCollection(Collection coll) {
64: super (coll);
65: }
66:
67: //-----------------------------------------------------------------------
68: public Iterator iterator() {
69: return UnmodifiableIterator
70: .decorate(getCollection().iterator());
71: }
72:
73: public boolean add(Object object) {
74: throw new UnsupportedOperationException();
75: }
76:
77: public boolean addAll(Collection coll) {
78: throw new UnsupportedOperationException();
79: }
80:
81: public void clear() {
82: throw new UnsupportedOperationException();
83: }
84:
85: public boolean remove(Object object) {
86: throw new UnsupportedOperationException();
87: }
88:
89: public boolean removeAll(Collection coll) {
90: throw new UnsupportedOperationException();
91: }
92:
93: public boolean retainAll(Collection coll) {
94: throw new UnsupportedOperationException();
95: }
96:
97: }
|