Add UnmodifiableSet

This commit is contained in:
Andrea Cavalli 2020-11-28 22:55:32 +01:00
parent c64a272bd1
commit faafc3fe29
3 changed files with 240 additions and 0 deletions

View File

@ -2,12 +2,14 @@ package org.warp.commonutils.type;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectSets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
public interface UnmodifiableIterableSet<K> extends Iterable<K> {
@ -20,6 +22,8 @@ public interface UnmodifiableIterableSet<K> extends Iterable<K> {
Set<K> toUnmodifiableSet();
Stream<K> stream();
<V> UnmodifiableIterableMap<K,V> toUnmodifiableIterableMapSetValues(V[] values);
<K2> UnmodifiableIterableMap<K2,K> toUnmodifiableIterableMapSetKeys(K2[] keys);
@ -69,6 +73,11 @@ public interface UnmodifiableIterableSet<K> extends Iterable<K> {
return ObjectSets.EMPTY_SET;
}
@Override
public Stream<K> stream() {
return Stream.empty();
}
@Override
public <V> UnmodifiableIterableMap<K, V> toUnmodifiableIterableMapSetValues(V[] values) {
return UnmodifiableIterableMap.of(null, values);
@ -114,6 +123,11 @@ public interface UnmodifiableIterableSet<K> extends Iterable<K> {
return ObjectSets.unmodifiable(new ObjectOpenHashSet<>(items, 1.0f));
}
@Override
public Stream<K> stream() {
return Arrays.stream(items);
}
@Override
public <V> UnmodifiableIterableMap<K, V> toUnmodifiableIterableMapSetValues(V[] values) {
return UnmodifiableIterableMap.of(items, values);
@ -165,6 +179,11 @@ public interface UnmodifiableIterableSet<K> extends Iterable<K> {
return Collections.unmodifiableSet(items);
}
@Override
public Stream<K> stream() {
return items.stream();
}
@Override
public <V> UnmodifiableIterableMap<K, V> toUnmodifiableIterableMapSetValues(V[] values) {
return UnmodifiableIterableMap.of(items.toArray(generator), values);

View File

@ -4,6 +4,7 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import java.lang.reflect.Array;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
@ -139,6 +140,23 @@ public interface UnmodifiableMap<K, V> extends UnmodifiableIterableMap<K, V> {
return new MappedUnmodifiableMap<K, V>(map);
}
@SuppressWarnings("SuspiciousSystemArraycopy")
static <K, V> UnmodifiableMap<K, V> ofObjects(Object[] keys, Object[] values) {
if (keys == null || values == null || (keys.length == 0 && values.length == 0)) {
return UnmodifiableMap.of(null, null);
} else if (keys.length == values.length) {
//noinspection unchecked
K[] keysArray = (K[]) Array.newInstance(keys[0].getClass(), keys.length);
System.arraycopy(keys, 0, keysArray, 0, keys.length);
//noinspection unchecked
V[] valuesArray = (V[]) Array.newInstance(values[0].getClass(), keys.length);
System.arraycopy(values, 0, valuesArray, 0, values.length);
return UnmodifiableMap.of(keysArray, valuesArray);
} else {
throw new IllegalArgumentException("The number of keys doesn't match the number of values.");
}
}
class EmptyUnmodifiableMap<K, V> implements UnmodifiableMap<K, V> {
private EmptyUnmodifiableMap() {}

View File

@ -0,0 +1,203 @@
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.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 ObjectSets.EMPTY_SET;
}
@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());
}
}
}