net.minestom.server.utils.collection.MappedCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of minestom-snapshots Show documentation
Show all versions of minestom-snapshots Show documentation
1.20.4 Lightweight Minecraft server
package net.minestom.server.utils.collection;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
@ApiStatus.Internal
public record MappedCollection(@NotNull Collection original,
@NotNull Function mapper) implements Collection {
public static , R> MappedCollection plainReferences(@NotNull Collection original) {
return new MappedCollection<>(original, AtomicReference::getPlain);
}
@Override
public int size() {
return original.size();
}
@Override
public boolean isEmpty() {
return original.isEmpty();
}
@Override
public boolean contains(Object o) {
for (var entry : original) {
if (mapper.apply(entry).equals(o)) return true;
}
return false;
}
@Override
public @NotNull Iterator iterator() {
var iterator = original.iterator();
return new Iterator<>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public R next() {
return mapper.apply(iterator.next());
}
};
}
@Override
public @NotNull Object @NotNull [] toArray() {
// TODO
throw new UnsupportedOperationException("Unsupported array object");
}
@Override
public @NotNull T @NotNull [] toArray(@NotNull T @NotNull [] a) {
// TODO
throw new UnsupportedOperationException("Unsupported array generic");
}
@Override
public boolean containsAll(@NotNull Collection> c) {
if (c.size() > original.size()) return false;
for (var entry : c) {
if (!contains(entry)) return false;
}
return true;
}
@Override
public boolean add(R t) {
throw new UnsupportedOperationException("Unmodifiable collection");
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException("Unmodifiable collection");
}
@Override
public boolean addAll(@NotNull Collection extends R> c) {
throw new UnsupportedOperationException("Unmodifiable collection");
}
@Override
public boolean removeAll(@NotNull Collection> c) {
throw new UnsupportedOperationException("Unmodifiable collection");
}
@Override
public boolean retainAll(@NotNull Collection> c) {
throw new UnsupportedOperationException("Unmodifiable collection");
}
@Override
public void clear() {
throw new UnsupportedOperationException("Unmodifiable collection");
}
}