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 {@link Collection} to ensure it can't be altered.
27 * <p>
28 * This class is Serializable from Commons Collections 3.1.
29 * <p>
30 * Attempts to modify it will result in an UnsupportedOperationException.
31 *
32 * @param <E> the type of the elements in the collection
33 * @since 3.0
34 * @version $Id: UnmodifiableCollection.java 1429905 2013-01-07 17:15:14Z ggregory $
35 */
36 public final class UnmodifiableCollection<E>
37 extends AbstractCollectionDecorator<E>
38 implements Unmodifiable {
39
40 /** Serialization version */
41 private static final long serialVersionUID = -239892006883819945L;
42
43 /**
44 * Factory method to create an unmodifiable collection.
45 * <p>
46 * If the collection passed in is already unmodifiable, it is returned.
47 *
48 * @param <T> the type of the elements in the collection
49 * @param coll the collection to decorate, must not be null
50 * @return an unmodifiable collection
51 * @throws IllegalArgumentException if collection is null
52 */
53 public static <T> Collection<T> unmodifiableCollection(final Collection<T> coll) {
54 if (coll instanceof Unmodifiable) {
55 return coll;
56 }
57 return new UnmodifiableCollection<T>(coll);
58 }
59
60 //-----------------------------------------------------------------------
61 /**
62 * Constructor that wraps (not copies).
63 *
64 * @param coll the collection to decorate, must not be null
65 * @throws IllegalArgumentException if collection is null
66 */
67 private UnmodifiableCollection(final Collection<E> coll) {
68 super(coll);
69 }
70
71 //-----------------------------------------------------------------------
72 @Override
73 public Iterator<E> iterator() {
74 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
75 }
76
77 @Override
78 public boolean add(final E object) {
79 throw new UnsupportedOperationException();
80 }
81
82 @Override
83 public boolean addAll(final Collection<? extends E> coll) {
84 throw new UnsupportedOperationException();
85 }
86
87 @Override
88 public void clear() {
89 throw new UnsupportedOperationException();
90 }
91
92 @Override
93 public boolean remove(final Object object) {
94 throw new UnsupportedOperationException();
95 }
96
97 @Override
98 public boolean removeAll(final Collection<?> coll) {
99 throw new UnsupportedOperationException();
100 }
101
102 @Override
103 public boolean retainAll(final Collection<?> coll) {
104 throw new UnsupportedOperationException();
105 }
106
107 }