205 lines
4.3 KiB
Java
205 lines
4.3 KiB
Java
package org.warp.commonutils.type;
|
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
|
import it.unimi.dsi.fastutil.objects.ObjectIterators;
|
|
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
|
import it.unimi.dsi.fastutil.objects.ObjectSet;
|
|
import it.unimi.dsi.fastutil.objects.ObjectSets;
|
|
import java.util.Collections;
|
|
import java.util.HashSet;
|
|
import java.util.Iterator;
|
|
import java.util.NoSuchElementException;
|
|
import java.util.Set;
|
|
import java.util.function.Consumer;
|
|
import java.util.stream.Stream;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
public interface UnmodifiableSet<K> extends UnmodifiableIterableSet<K> {
|
|
|
|
boolean contains(Object value);
|
|
|
|
@NotNull
|
|
ObjectIterator<K> fastIterator();
|
|
|
|
void forEach(Consumer<? super K> action);
|
|
|
|
static <K> UnmodifiableSet<K> of(K[] values) {
|
|
int valuesSize = (values != null) ? values.length : 0;
|
|
|
|
if (valuesSize == 0) {
|
|
// return mutable map
|
|
return new EmptyUnmodifiableSet<>();
|
|
}
|
|
|
|
return new MappedUnmodifiableSet<>(new ObjectOpenHashSet<>(values, 1.0f));
|
|
}
|
|
|
|
static <K> UnmodifiableSet<K> of(Set<K> set) {
|
|
return new MappedUnmodifiableSet<K>(set);
|
|
}
|
|
|
|
class EmptyUnmodifiableSet<K> implements UnmodifiableSet<K> {
|
|
|
|
private EmptyUnmodifiableSet() {}
|
|
|
|
@Override
|
|
public int size() {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean contains(Object value) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void forEach(Consumer<? super K> action) {
|
|
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
public Iterator<K> iterator() {
|
|
return new Iterator<>() {
|
|
@Override
|
|
public boolean hasNext() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public K next() {
|
|
throw new NoSuchElementException();
|
|
}
|
|
};
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
public ObjectIterator<K> fastIterator() {
|
|
return new ObjectIterator<>() {
|
|
@Override
|
|
public boolean hasNext() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public K next() {
|
|
throw new NoSuchElementException();
|
|
}
|
|
};
|
|
}
|
|
|
|
@Override
|
|
public Set<K> toUnmodifiableSet() {
|
|
//noinspection unchecked
|
|
return new HashSet<>();
|
|
}
|
|
|
|
@Override
|
|
public Stream<K> stream() {
|
|
return Stream.empty();
|
|
}
|
|
|
|
@Override
|
|
public <V> UnmodifiableIterableMap<K, V> toUnmodifiableIterableMapSetValues(V[] values) {
|
|
return UnmodifiableIterableMap.of(null, values);
|
|
}
|
|
|
|
@Override
|
|
public <K2> UnmodifiableIterableMap<K2, K> toUnmodifiableIterableMapSetKeys(K2[] keys) {
|
|
return UnmodifiableIterableMap.of(keys, null);
|
|
}
|
|
|
|
@Override
|
|
public <V> UnmodifiableMap<K, V> toUnmodifiableMapSetValues(V[] values) {
|
|
return UnmodifiableMap.of(null, values);
|
|
}
|
|
|
|
@Override
|
|
public <K2> UnmodifiableMap<K2, K> toUnmodifiableMapSetKeys(K2[] keys) {
|
|
return UnmodifiableMap.of(keys, null);
|
|
}
|
|
}
|
|
|
|
class MappedUnmodifiableSet<K> implements UnmodifiableSet<K> {
|
|
|
|
private final Set<K> set;
|
|
|
|
private MappedUnmodifiableSet(@NotNull Set<K> set) {
|
|
this.set = set;
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
return set.size();
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
return set.isEmpty();
|
|
}
|
|
|
|
@Override
|
|
public boolean contains(Object key) {
|
|
//noinspection SuspiciousMethodCalls
|
|
return set.contains(key);
|
|
}
|
|
|
|
@Override
|
|
public void forEach(Consumer<? super K> action) {
|
|
set.forEach(action);
|
|
}
|
|
|
|
@Override
|
|
public Set<K> toUnmodifiableSet() {
|
|
return Collections.unmodifiableSet(set);
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
public Iterator<K> iterator() {
|
|
return set.iterator();
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
public ObjectIterator<K> fastIterator() {
|
|
if (set instanceof ObjectSet) {
|
|
return ((ObjectSet<K>) set).iterator();
|
|
} else {
|
|
return ObjectIterators.asObjectIterator(set.iterator());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Stream<K> stream() {
|
|
return set.stream();
|
|
}
|
|
|
|
@Override
|
|
public <V> UnmodifiableIterableMap<K, V> toUnmodifiableIterableMapSetValues(V[] values) {
|
|
return UnmodifiableIterableMap.ofObjects(set.toArray(), values);
|
|
}
|
|
|
|
@Override
|
|
public <K2> UnmodifiableIterableMap<K2, K> toUnmodifiableIterableMapSetKeys(K2[] keys) {
|
|
return UnmodifiableIterableMap.ofObjects(keys, set.toArray());
|
|
}
|
|
|
|
@Override
|
|
public <V> UnmodifiableMap<K, V> toUnmodifiableMapSetValues(V[] values) {
|
|
return UnmodifiableMap.ofObjects(set.toArray(), values);
|
|
}
|
|
|
|
@Override
|
|
public <K2> UnmodifiableMap<K2, K> toUnmodifiableMapSetKeys(K2[] keys) {
|
|
return UnmodifiableMap.ofObjects(keys, set.toArray());
|
|
}
|
|
}
|
|
}
|