diff --git a/src/main/java/org/warp/commonutils/type/ImmutableLinkedSet.java b/src/main/java/org/warp/commonutils/type/ImmutableLinkedSet.java new file mode 100644 index 0000000..eea5fb6 --- /dev/null +++ b/src/main/java/org/warp/commonutils/type/ImmutableLinkedSet.java @@ -0,0 +1,157 @@ +package org.warp.commonutils.type; + +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.Spliterator; +import java.util.function.Consumer; +import java.util.function.IntFunction; +import java.util.function.Predicate; +import java.util.stream.Stream; +import org.jetbrains.annotations.NotNull; + +public class ImmutableLinkedSet implements Set { + + private final LinkedHashSet set; + + public ImmutableLinkedSet(LinkedHashSet set) { + this.set = set; + } + + @Override + public int size() { + return set.size(); + } + + @Override + public boolean isEmpty() { + return set.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return set.contains(o); + } + + @NotNull + @Override + public Iterator iterator() { + var it = set.iterator(); + return new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public E next() { + return it.next(); + } + }; + } + + @Override + public void forEach(Consumer action) { + set.forEach(action); + } + + @NotNull + @Override + public Object[] toArray() { + return set.toArray(); + } + + @NotNull + @Override + public T[] toArray(@NotNull T[] a) { + //noinspection SuspiciousToArrayCall + return set.toArray(a); + } + + @Override + public T[] toArray(IntFunction generator) { + //noinspection SuspiciousToArrayCall + return set.toArray(generator); + } + + @Override + public boolean add(E e) { + throw new UnsupportedOperationException("Read only"); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException("Read only"); + } + + @Override + public boolean containsAll(@NotNull Collection c) { + return set.containsAll(c); + } + + @Override + public boolean addAll(@NotNull Collection c) { + throw new UnsupportedOperationException("Read only"); + } + + @Override + public boolean retainAll(@NotNull Collection c) { + throw new UnsupportedOperationException("Read only"); + } + + @Override + public boolean removeAll(@NotNull Collection c) { + throw new UnsupportedOperationException("Read only"); + } + + @Override + public boolean removeIf(Predicate filter) { + throw new UnsupportedOperationException("Read only"); + } + + @Override + public void clear() { + throw new UnsupportedOperationException("Read only"); + } + + @Override + public Spliterator spliterator() { + var spl = set.spliterator(); + return new Spliterator() { + @Override + public boolean tryAdvance(Consumer action) { + return spl.tryAdvance(action); + } + + @Override + public Spliterator trySplit() { + return spl.trySplit(); + } + + @Override + public long estimateSize() { + return spl.estimateSize(); + } + + @Override + public int characteristics() { + return spl.characteristics(); + } + }; + } + + @Override + public Stream stream() { + return set.stream(); + } + + @Override + public Stream parallelStream() { + return set.parallelStream(); + } + + public LinkedHashSet copyMutable() { + return new LinkedHashSet<>(set); + } +}