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