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.util.Collection;
20 import java.util.Iterator;
21
22 import org.apache.commons.collections.Unmodifiable;
23 import org.apache.commons.collections.iterators.UnmodifiableIterator;
24
25 /**
26 * Decorates another <code>Collection</code> to ensure it can't be altered.
27 * <p>
28 * This class is Serializable from Commons Collections 3.1.
29 *
30 * @since Commons Collections 3.0
31 * @version $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
32 *
33 * @author Stephen Colebourne
34 */
35 public final class UnmodifiableCollection
36 extends AbstractSerializableCollectionDecorator
37 implements Unmodifiable {
38
39 /** Serialization version */
40 private static final long serialVersionUID = -239892006883819945L;
41
42 /**
43 * Factory method to create an unmodifiable collection.
44 * <p>
45 * If the collection passed in is already unmodifiable, it is returned.
46 *
47 * @param coll the collection to decorate, must not be null
48 * @return an unmodifiable collection
49 * @throws IllegalArgumentException if collection is null
50 */
51 public static Collection decorate(Collection coll) {
52 if (coll instanceof Unmodifiable) {
53 return coll;
54 }
55 return new UnmodifiableCollection(coll);
56 }
57
58 //-----------------------------------------------------------------------
59 /**
60 * Constructor that wraps (not copies).
61 *
62 * @param coll the collection to decorate, must not be null
63 * @throws IllegalArgumentException if collection is null
64 */
65 private UnmodifiableCollection(Collection coll) {
66 super(coll);
67 }
68
69 //-----------------------------------------------------------------------
70 public Iterator iterator() {
71 return UnmodifiableIterator.decorate(getCollection().iterator());
72 }
73
74 public boolean add(Object object) {
75 throw new UnsupportedOperationException();
76 }
77
78 public boolean addAll(Collection coll) {
79 throw new UnsupportedOperationException();
80 }
81
82 public void clear() {
83 throw new UnsupportedOperationException();
84 }
85
86 public boolean remove(Object object) {
87 throw new UnsupportedOperationException();
88 }
89
90 public boolean removeAll(Collection coll) {
91 throw new UnsupportedOperationException();
92 }
93
94 public boolean retainAll(Collection coll) {
95 throw new UnsupportedOperationException();
96 }
97
98 }
99