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 * https://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.collections4.set;
18
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.Set;
22 import java.util.function.Predicate;
23
24 import org.apache.commons.collections4.Unmodifiable;
25 import org.apache.commons.collections4.iterators.UnmodifiableIterator;
26
27 /**
28 * Decorates another {@code Set} to ensure it can't be altered.
29 * <p>
30 * This class is Serializable from Commons Collections 3.1.
31 * </p>
32 * <p>
33 * Attempts to modify it will result in an UnsupportedOperationException.
34 * </p>
35 *
36 * @param <E> The type of the elements in this set
37 * @since 3.0
38 */
39 public final class UnmodifiableSet<E>
40 extends AbstractSerializableSetDecorator<E>
41 implements Unmodifiable {
42
43 /** Serialization version */
44 private static final long serialVersionUID = 6499119872185240161L;
45
46 /**
47 * Factory method to create an unmodifiable set.
48 *
49 * @param <E> The element type
50 * @param set The set to decorate, must not be null
51 * @return A new unmodifiable set
52 * @throws NullPointerException if set is null
53 * @since 4.0
54 */
55 public static <E> Set<E> unmodifiableSet(final Set<? extends E> set) {
56 if (set instanceof Unmodifiable) {
57 @SuppressWarnings("unchecked") // safe to upcast
58 final Set<E> tmpSet = (Set<E>) set;
59 return tmpSet;
60 }
61 return new UnmodifiableSet<>(set);
62 }
63
64 /**
65 * Constructor that wraps (not copies).
66 *
67 * @param set The set to decorate, must not be null
68 * @throws NullPointerException if set is null
69 */
70 @SuppressWarnings("unchecked") // safe to upcast
71 private UnmodifiableSet(final Set<? extends E> set) {
72 super((Set<E>) set);
73 }
74
75 /**
76 * Always throws {@link UnsupportedOperationException}.
77 *
78 * @param object Ignored.
79 * @throws UnsupportedOperationException Always thrown.
80 */
81 @Override
82 public boolean add(final E object) {
83 throw new UnsupportedOperationException();
84 }
85
86 /**
87 * Always throws {@link UnsupportedOperationException}.
88 *
89 * @param coll Ignored.
90 * @throws UnsupportedOperationException Always thrown.
91 */
92 @Override
93 public boolean addAll(final Collection<? extends E> coll) {
94 throw new UnsupportedOperationException();
95 }
96
97 /**
98 * Always throws {@link UnsupportedOperationException}.
99 *
100 * @throws UnsupportedOperationException Always thrown.
101 */
102 @Override
103 public void clear() {
104 throw new UnsupportedOperationException();
105 }
106
107 @Override
108 public Iterator<E> iterator() {
109 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
110 }
111
112 /**
113 * Always throws {@link UnsupportedOperationException}.
114 *
115 * @param object Ignored.
116 * @throws UnsupportedOperationException Always thrown.
117 */
118 @Override
119 public boolean remove(final Object object) {
120 throw new UnsupportedOperationException();
121 }
122
123 /**
124 * Always throws {@link UnsupportedOperationException}.
125 *
126 * @param coll Ignored.
127 * @throws UnsupportedOperationException Always thrown.
128 */
129 @Override
130 public boolean removeAll(final Collection<?> coll) {
131 throw new UnsupportedOperationException();
132 }
133
134 /**
135 * Always throws {@link UnsupportedOperationException}.
136 *
137 * @param filter Ignored.
138 * @throws UnsupportedOperationException Always thrown.
139 * @since 4.4
140 */
141 @Override
142 public boolean removeIf(final Predicate<? super E> filter) {
143 throw new UnsupportedOperationException();
144 }
145
146 /**
147 * Always throws {@link UnsupportedOperationException}.
148 *
149 * @param coll Ignored.
150 * @throws UnsupportedOperationException Always thrown.
151 */
152 @Override
153 public boolean retainAll(final Collection<?> coll) {
154 throw new UnsupportedOperationException();
155 }
156
157 }