mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-02 17:00:41 +01:00
Make AcquirableCollection a simple collection wrapper
This commit is contained in:
parent
2509085dc2
commit
2577307da0
@ -54,6 +54,15 @@ public interface Acquirable<T> {
|
||||
}
|
||||
}
|
||||
|
||||
default @NotNull Optional<T> local() {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
final TickThread tickThread = getHandler().getTickThread();
|
||||
if (Objects.equals(currentThread, tickThread)) {
|
||||
return Optional.of(unwrap());
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
default void sync(@NotNull Consumer<T> consumer) {
|
||||
var acquired = lock();
|
||||
consumer.accept(acquired.get());
|
||||
@ -65,15 +74,6 @@ public interface Acquirable<T> {
|
||||
AsyncUtils.runAsync(() -> sync(consumer));
|
||||
}
|
||||
|
||||
default @NotNull Optional<T> local() {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
final TickThread tickThread = getHandler().getTickThread();
|
||||
if (Objects.equals(currentThread, tickThread)) {
|
||||
return Optional.of(unwrap());
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwrap the contained object unsafely.
|
||||
* <p>
|
||||
|
@ -1,19 +1,98 @@
|
||||
package net.minestom.server.acquirable;
|
||||
|
||||
import net.minestom.server.utils.collection.CollectionView;
|
||||
import net.minestom.server.utils.async.AsyncUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class AcquirableCollection<E> extends CollectionView<E, Acquirable<E>> {
|
||||
public class AcquirableCollection<E> implements Collection<Acquirable<E>> {
|
||||
|
||||
private final Collection<Acquirable<E>> acquirableEntityCollection;
|
||||
private final Collection<Acquirable<E>> acquirableCollection;
|
||||
|
||||
public AcquirableCollection(@NotNull Collection<Acquirable<E>> acquirableEntityCollection) {
|
||||
super(acquirableEntityCollection,
|
||||
null,
|
||||
//Entity::getAcquirable,
|
||||
acquirableEntity -> (E) acquirableEntity.unwrap());
|
||||
this.acquirableEntityCollection = acquirableEntityCollection;
|
||||
public AcquirableCollection(Collection<Acquirable<E>> acquirableCollection) {
|
||||
this.acquirableCollection = acquirableCollection;
|
||||
}
|
||||
|
||||
public void forEachSync(@NotNull Consumer<E> consumer) {
|
||||
Acquisition.acquireForEach(acquirableCollection, consumer);
|
||||
}
|
||||
|
||||
public void forEachAsync(@NotNull Consumer<E> consumer) {
|
||||
AsyncUtils.runAsync(() -> forEachSync(consumer));
|
||||
}
|
||||
|
||||
public @NotNull Stream<E> unwrap() {
|
||||
return acquirableCollection.stream().map(Acquirable::unwrap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return acquirableCollection.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return acquirableCollection.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return acquirableCollection.contains(o);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<Acquirable<E>> iterator() {
|
||||
return acquirableCollection.iterator();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return acquirableCollection.toArray();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <T> T[] toArray(@NotNull T[] a) {
|
||||
return acquirableCollection.toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Acquirable<E> eAcquirable) {
|
||||
return acquirableCollection.add(eAcquirable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return acquirableCollection.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(@NotNull Collection<?> c) {
|
||||
return acquirableCollection.containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(@NotNull Collection<? extends Acquirable<E>> c) {
|
||||
return acquirableCollection.addAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(@NotNull Collection<?> c) {
|
||||
return acquirableCollection.removeAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(@NotNull Collection<?> c) {
|
||||
return acquirableCollection.retainAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
this.acquirableCollection.clear();
|
||||
}
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ public final class Acquisition {
|
||||
* @param collection the collection to acquire
|
||||
* @param consumer the consumer called for each of the collection element
|
||||
*/
|
||||
public static <T> void acquireForEach(@NotNull Collection<Acquirable<T>> collection,
|
||||
@NotNull Consumer<T> consumer) {
|
||||
protected static <T> void acquireForEach(@NotNull Collection<Acquirable<T>> collection,
|
||||
@NotNull Consumer<T> consumer) {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
var threadEntitiesMap = retrieveOptionalThreadMap(collection, currentThread, consumer);
|
||||
|
||||
|
@ -1,170 +0,0 @@
|
||||
package net.minestom.server.utils.collection;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* A CollectionView is a class which is mapped to another collection
|
||||
* and convert every result using a given function. It is more efficient
|
||||
* than filling a new collection every time, as long as the two types are interchangeable.
|
||||
* <p>
|
||||
* The view is not thread-safe.
|
||||
*
|
||||
* @param <E> the type that the collection should return
|
||||
* @param <V> the type of the viewed collection
|
||||
*/
|
||||
public class CollectionView<E, V> implements Collection<E> {
|
||||
|
||||
private final Collection<V> collectionView;
|
||||
private final Function<E, V> toViewFunction;
|
||||
private final Function<V, E> toTypeFunction;
|
||||
|
||||
public CollectionView(@NotNull Collection<V> collectionView,
|
||||
@NotNull Function<E, V> toViewFunction,
|
||||
@NotNull Function<V, E> toTypeFunction) {
|
||||
this.collectionView = collectionView;
|
||||
this.toViewFunction = toViewFunction;
|
||||
this.toTypeFunction = toTypeFunction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return collectionView.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return collectionView.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
try {
|
||||
return collectionView.contains(toViewFunction.apply((E) o));
|
||||
} catch (ClassCastException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new IteratorView<>(collectionView.iterator(), toTypeFunction);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
Object[] array = new Object[size()];
|
||||
|
||||
int i = 0;
|
||||
for (E e : this) {
|
||||
array[i++] = e;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <T1> T1[] toArray(@NotNull T1[] a) {
|
||||
int i = 0;
|
||||
for (E e : this) {
|
||||
a[i++] = (T1) e;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
return collectionView.add(toViewFunction.apply(e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
try {
|
||||
return collectionView.remove(toViewFunction.apply((E) o));
|
||||
} catch (ClassCastException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(@NotNull Collection<?> c) {
|
||||
for (Object e : c) {
|
||||
if (!contains(e))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(@NotNull Collection<? extends E> c) {
|
||||
boolean changed = false;
|
||||
try {
|
||||
for (Object e : c) {
|
||||
if (add((E) e))
|
||||
changed = true;
|
||||
}
|
||||
} catch (ClassCastException ignored) {
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(@NotNull Collection<?> c) {
|
||||
boolean changed = false;
|
||||
for (Object e : c) {
|
||||
if (remove(e))
|
||||
changed = true;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(@NotNull Collection<?> c) {
|
||||
boolean changed = false;
|
||||
for (Object e : c) {
|
||||
if (!contains(e)) {
|
||||
remove(e);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
this.collectionView.clear();
|
||||
}
|
||||
|
||||
public static class IteratorView<T, V> implements Iterator<T> {
|
||||
|
||||
private final Iterator<V> iteratorView;
|
||||
private final Function<V, T> toTypeFunction;
|
||||
|
||||
public IteratorView(Iterator<V> iteratorView,
|
||||
Function<V, T> toTypeFunction) {
|
||||
this.iteratorView = iteratorView;
|
||||
this.toTypeFunction = toTypeFunction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iteratorView.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
final V viewElement = iteratorView.next();
|
||||
return toTypeFunction.apply(viewElement);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user