mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-08 12:34:52 +00:00
Add convenience method to ObjectPooler (#130)
This commit is contained in:
1 parent
92afa3a1e0
commit
af04614d26
1 file changed
+22
-3
@@ -1,16 +1,21 @@
|
||||
package com.gtnewhorizon.gtnhlib.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
|
||||
public class ObjectPooler<T> {
|
||||
|
||||
private final Supplier<T> instanceSupplier;
|
||||
private final ArrayList<T> availableInstances;
|
||||
private final ObjectArrayList<T> availableInstances;
|
||||
|
||||
public ObjectPooler(Supplier<T> instanceSupplier) {
|
||||
this.instanceSupplier = instanceSupplier;
|
||||
this.availableInstances = new ArrayList<>();
|
||||
this.availableInstances = new ObjectArrayList<>();
|
||||
}
|
||||
|
||||
public T getInstance() {
|
||||
@@ -21,6 +26,20 @@ public class ObjectPooler<T> {
|
||||
}
|
||||
|
||||
public void releaseInstance(T instance) {
|
||||
if (instance == null) return;
|
||||
this.availableInstances.add(instance);
|
||||
}
|
||||
|
||||
public void releaseInstances(Collection<T> instances) {
|
||||
instances.forEach(this::releaseInstance);
|
||||
instances.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses arraycopy instead of a loop. Faster, but doesn't check that the input is nonnull. Use with care!
|
||||
*/
|
||||
public void releaseInstances(@NotNull T[] instances) {
|
||||
this.availableInstances.addElements(availableInstances.size(), instances);
|
||||
Arrays.fill(instances, null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user