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.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 @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 Iterator<E> iterator() {
92 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
93 }
94
95 @Override
96 public boolean remove(final Object object) {
97 throw new UnsupportedOperationException();
98 }
99
100 @Override
101 public boolean removeAll(final Collection<?> coll) {
102 throw new UnsupportedOperationException();
103 }
104
105 /**
106 * @since 4.4
107 */
108 @Override
109 public boolean removeIf(final Predicate<? super E> filter) {
110 throw new UnsupportedOperationException();
111 }
112
113 @Override
114 public boolean retainAll(final Collection<?> coll) {
115 throw new UnsupportedOperationException();
116 }
117
118 }