Skip to content

Commit 5e2e2be

Browse files
authored
Merge pull request #1837 from fabriciofx/1836
#1836: Implements Immutable Set
2 parents de760e1 + 3f046e9 commit 5e2e2be

4 files changed

Lines changed: 469 additions & 2 deletions

File tree

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2017-2026 Yegor Bugayenko
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
package org.cactoos.set;
6+
7+
import java.util.Collection;
8+
import java.util.Iterator;
9+
import java.util.Set;
10+
11+
/**
12+
* Decorator that doesn't allow mutations of the wrapped {@link Set}.
13+
*
14+
* <p>There is no thread-safety guarantee.</p>
15+
*
16+
* @param <T> Element type
17+
* @since 0.58.0
18+
*/
19+
@SuppressWarnings("PMD.TooManyMethods")
20+
public final class Immutable<T> implements Set<T> {
21+
/**
22+
* Encapsulated set.
23+
*/
24+
private final Set<? extends T> set;
25+
26+
/**
27+
* Ctor.
28+
* @param src Source set
29+
*/
30+
public Immutable(final Set<? extends T> src) {
31+
this.set = src;
32+
}
33+
34+
@Override
35+
public int size() {
36+
return this.set.size();
37+
}
38+
39+
@Override
40+
public boolean isEmpty() {
41+
return this.set.isEmpty();
42+
}
43+
44+
@Override
45+
public boolean contains(final Object item) {
46+
return this.set.contains(item);
47+
}
48+
49+
@Override
50+
public Iterator<T> iterator() {
51+
return new org.cactoos.iterator.Immutable<>(this.set.iterator());
52+
}
53+
54+
@Override
55+
public Object[] toArray() {
56+
return this.set.toArray();
57+
}
58+
59+
@Override
60+
public <X> X[] toArray(final X[] array) {
61+
return this.set.toArray(array);
62+
}
63+
64+
@Override
65+
public boolean add(final T item) {
66+
throw new UnsupportedOperationException(
67+
"#add(T): the set is read-only"
68+
);
69+
}
70+
71+
@Override
72+
public boolean remove(final Object item) {
73+
throw new UnsupportedOperationException(
74+
"#remove(Object): the set is read-only"
75+
);
76+
}
77+
78+
@Override
79+
public boolean containsAll(final Collection<?> items) {
80+
return this.set.containsAll(items);
81+
}
82+
83+
@Override
84+
public boolean addAll(final Collection<? extends T> items) {
85+
throw new UnsupportedOperationException(
86+
"#addAll(Collection): the set is read-only"
87+
);
88+
}
89+
90+
@Override
91+
public boolean retainAll(final Collection<?> items) {
92+
throw new UnsupportedOperationException(
93+
"#retainAll(Collection): the set is read-only"
94+
);
95+
}
96+
97+
@Override
98+
public boolean removeAll(final Collection<?> items) {
99+
throw new UnsupportedOperationException(
100+
"#removeAll(Collection): the set is read-only"
101+
);
102+
}
103+
104+
@Override
105+
public void clear() {
106+
throw new UnsupportedOperationException(
107+
"#clear(): the set is read-only"
108+
);
109+
}
110+
111+
@Override
112+
public boolean equals(final Object other) {
113+
return this.set.equals(other);
114+
}
115+
116+
@Override
117+
public int hashCode() {
118+
return this.set.hashCode();
119+
}
120+
121+
@Override
122+
public String toString() {
123+
return this.set.toString();
124+
}
125+
}

src/test/java/org/cactoos/io/ReaderOfTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ void readsCharSequenceWithCharset() {
262262
final String input =
263263
"char sequence with charset on äÄ üÜ öÖ ß жш";
264264
new Assertion<>(
265-
"Must read from char sequance with charset",
265+
"Must read from char sequence with charset",
266266
new TextOf(
267267
new ReaderOf(
268268
input,

0 commit comments

Comments
 (0)