Generics improvements

Dieser Commit ist enthalten in:
MattBDev 2020-03-05 16:43:26 -05:00
Ursprung 50ceb4d715
Commit c1fe16b0e9
6 geänderte Dateien mit 17 neuen und 355 gelöschten Zeilen

Datei anzeigen

@ -3,6 +3,7 @@ package com.boydti.fawe;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger; import static org.slf4j.LoggerFactory.getLogger;
import com.boydti.fawe.beta.IChunkSet;
import com.boydti.fawe.beta.Trimable; import com.boydti.fawe.beta.Trimable;
import com.boydti.fawe.beta.implementation.queue.Pool; import com.boydti.fawe.beta.implementation.queue.Pool;
import com.boydti.fawe.beta.implementation.queue.QueuePool; import com.boydti.fawe.beta.implementation.queue.QueuePool;
@ -42,6 +43,7 @@ import java.util.HashMap;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -52,6 +54,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;
public enum FaweCache implements Trimable { public enum FaweCache implements Trimable {
IMP IMP
@ -64,7 +67,7 @@ public enum FaweCache implements Trimable {
public final char[] EMPTY_CHAR_4096 = new char[4096]; public final char[] EMPTY_CHAR_4096 = new char[4096];
private final IdentityHashMap<Class<?>, Pool> REGISTERED_POOLS = new IdentityHashMap<>(); private final IdentityHashMap<Class<? extends IChunkSet>, Pool<? extends IChunkSet>> REGISTERED_POOLS = new IdentityHashMap<>();
/* /*
Palette buffers / cache Palette buffers / cache
@ -89,15 +92,15 @@ public enum FaweCache implements Trimable {
MUTABLE_BLOCKVECTOR3.clean(); MUTABLE_BLOCKVECTOR3.clean();
SECTION_BITS_TO_CHAR.clean(); SECTION_BITS_TO_CHAR.clean();
} }
for (Map.Entry<Class<?>, Pool> entry : REGISTERED_POOLS.entrySet()) { for (Entry<Class<? extends IChunkSet>, Pool<? extends IChunkSet>> entry : REGISTERED_POOLS.entrySet()) {
Pool pool = entry.getValue(); Pool<? extends IChunkSet> pool = entry.getValue();
pool.clear(); pool.clear();
} }
return false; return false;
} }
public synchronized <T> Pool<T> registerPool(Class<T> clazz, Supplier<T> cache, boolean buffer) { public synchronized <T extends IChunkSet> Pool<T> registerPool(Class<T> clazz, Supplier<T> cache, boolean buffer) {
checkNotNull(cache); checkNotNull(cache);
Pool<T> pool; Pool<T> pool;
if (buffer) { if (buffer) {
@ -105,7 +108,7 @@ public enum FaweCache implements Trimable {
} else { } else {
pool = cache::get; pool = cache::get;
} }
Pool<T> previous = REGISTERED_POOLS.putIfAbsent(clazz, pool); Pool<? extends IChunkSet> previous = REGISTERED_POOLS.putIfAbsent(clazz, pool);
if (previous != null) { if (previous != null) {
throw new IllegalStateException("Previous key"); throw new IllegalStateException("Previous key");
} }
@ -115,7 +118,7 @@ public enum FaweCache implements Trimable {
public <T, V> LoadingCache<T, V> createCache(Supplier<V> withInitial) { public <T, V> LoadingCache<T, V> createCache(Supplier<V> withInitial) {
return CacheBuilder.newBuilder().build(new CacheLoader<T, V>() { return CacheBuilder.newBuilder().build(new CacheLoader<T, V>() {
@Override @Override
public V load(T key) { public V load(@NotNull T key) {
return withInitial.get(); return withInitial.get();
} }
}); });
@ -124,7 +127,7 @@ public enum FaweCache implements Trimable {
public <T, V> LoadingCache<T, V> createCache(Function<T, V> withInitial) { public <T, V> LoadingCache<T, V> createCache(Function<T, V> withInitial) {
return CacheBuilder.newBuilder().build(new CacheLoader<T, V>() { return CacheBuilder.newBuilder().build(new CacheLoader<T, V>() {
@Override @Override
public V load(T key) { public V load(@NotNull T key) {
return withInitial.apply(key); return withInitial.apply(key);
} }
}); });

Datei anzeigen

@ -1,191 +0,0 @@
package com.boydti.fawe.beta;
import com.boydti.fawe.beta.implementation.filter.block.ChunkFilterBlock;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import javax.annotation.Nullable;
/**
* Delegate for IChunk
*
* @param <U> parent class
*/
public interface IDelegateChunk<U extends IQueueChunk> extends IQueueChunk {
U getParent();
@Override
default IQueueChunk getRoot() {
IQueueChunk root = getParent();
while (root instanceof IDelegateChunk) {
root = ((IDelegateChunk) root).getParent();
}
return root;
}
@Override
default <T extends Future<T>> T call(IChunkSet set, Runnable finalize) {
return getParent().call(set, finalize);
}
@Override
default CompoundTag getTile(int x, int y, int z) {
return getParent().getTile(x, y, z);
}
@Override
default boolean hasSection(int layer) {
return getParent().hasSection(layer);
}
// @Override
// default void flood(Flood flood, FilterBlockMask mask, ChunkFilterBlock block) {
// getParent().flood(flood, mask, block);
// }
@Override
default boolean setTile(int x, int y, int z, CompoundTag tag) {
return getParent().setTile(x, y, z, tag);
}
@Override
default boolean setBiome(int x, int y, int z, BiomeType biome) {
return getParent().setBiome(x, y, z, biome);
}
@Override
default <T extends BlockStateHolder<T>> boolean setBlock(int x, int y, int z, T holder) {
return getParent().setBlock(x, y, z, holder);
}
@Override
default BiomeType getBiomeType(int x, int y, int z) {
return getParent().getBiomeType(x, y, z);
}
@Override
default BlockState getBlock(int x, int y, int z) {
return getParent().getBlock(x, y, z);
}
@Override
default BaseBlock getFullBlock(int x, int y, int z) {
return getParent().getFullBlock(x, y, z);
}
@Override
default <E extends IChunk> void init(IQueueExtent<E> extent, int chunkX, int chunkZ) {
getParent().init(extent, chunkX, chunkZ);
}
@Override
default int getX() {
return getParent().getX();
}
@Override
default int getZ() {
return getParent().getZ();
}
@Override
default boolean trim(boolean aggressive) {
return getParent().trim(aggressive);
}
@Override
default Future call() {
return getParent().call();
}
@Override
default void join() throws ExecutionException, InterruptedException {
getParent().join();
}
@Override
default void filterBlocks(Filter filter, ChunkFilterBlock block, @Nullable Region region, boolean full) {
getParent().filterBlocks(filter, block, region, full);
}
@Override
default boolean isEmpty() {
return getParent().isEmpty();
}
@Override
default Map<BlockVector3, CompoundTag> getTiles() {
return getParent().getTiles();
}
@Override
default Set<CompoundTag> getEntities() {
return getParent().getEntities();
}
@Override
default CompoundTag getEntity(UUID uuid) {
return getParent().getEntity(uuid);
}
@Override
default char[] load(int layer) {
return getParent().load(layer);
}
@Override
default void setBlocks(int layer, char[] data) {
getParent().setBlocks(layer, data);
}
@Override
default void setEntity(CompoundTag tag) {
getParent().setEntity(tag);
}
@Override
default void removeEntity(UUID uuid) {
getParent().removeEntity(uuid);
}
@Override
default Set<UUID> getEntityRemoves() {
return getParent().getEntityRemoves();
}
@Override
default BiomeType[] getBiomes() {
return getParent().getBiomes();
}
@Override
default boolean hasBiomes() {
return getParent().hasBiomes();
}
default <T extends IChunk> T findParent(Class<T> clazz) {
IChunk root = getParent();
if (clazz.isAssignableFrom(root.getClass())) {
return (T) root;
}
while (root instanceof IDelegateChunk) {
root = ((IDelegateChunk) root).getParent();
if (clazz.isAssignableFrom(root.getClass())) {
return (T) root;
}
}
return null;
}
}

Datei anzeigen

@ -25,7 +25,7 @@ import java.util.stream.IntStream;
import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Range;
public class CharSetBlocks extends CharBlocks implements IChunkSet { public class CharSetBlocks extends CharBlocks implements IChunkSet {
private static Pool<CharSetBlocks> POOL = FaweCache.IMP.registerPool(CharSetBlocks.class, CharSetBlocks::new, Settings.IMP.QUEUE.POOL); private static final Pool<CharSetBlocks> POOL = FaweCache.IMP.registerPool(CharSetBlocks.class, CharSetBlocks::new, Settings.IMP.QUEUE.POOL);
public static CharSetBlocks newInstance() { public static CharSetBlocks newInstance() {
return POOL.poll(); return POOL.poll();
} }

Datei anzeigen

@ -1,114 +0,0 @@
package com.boydti.fawe.beta.implementation.blocks;
import com.boydti.fawe.beta.IChunkSet;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.annotation.Nullable;
public interface DelegateChunkSet extends IChunkSet {
IChunkSet getParent();
@Override
default boolean setBiome(int x, int y, int z, BiomeType biome) {
return getParent().setBiome(x, y, z, biome);
}
@Override
default <T extends BlockStateHolder<T>> boolean setBlock(int x, int y, int z, T holder) {
return getParent().setBlock(x, y, z, holder);
}
@Override
default boolean isEmpty() {
return getParent().isEmpty();
}
@Override
default boolean setTile(int x, int y, int z, CompoundTag tile) {
return getParent().setTile(x, y, z, tile);
}
@Override
default void setEntity(CompoundTag tag) {
getParent().setEntity(tag);
}
@Override
default void removeEntity(UUID uuid) {
getParent().removeEntity(uuid);
}
@Override
default BlockState getBlock(int x, int y, int z) {
return getParent().getBlock(x, y, z);
}
@Override
default char[] load(int layer) {
return getParent().load(layer);
}
@Override
default BiomeType[] getBiomes() {
return getParent().getBiomes();
}
@Override
default Map<BlockVector3, CompoundTag> getTiles() {
return getParent().getTiles();
}
@Override
default Set<CompoundTag> getEntities() {
return getParent().getEntities();
}
@Override
default Set<UUID> getEntityRemoves() {
return getParent().getEntityRemoves();
}
@Override
default IChunkSet reset() {
IChunkSet parent = getParent();
parent.reset();
return parent;
}
@Override
@Nullable
default Operation commit() {
return getParent().commit();
}
@Override
default boolean hasSection(int layer) {
return getParent().hasSection(layer);
}
@Override
default boolean trim(boolean aggressive) {
return getParent().trim(aggressive);
}
@Override
default <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 position, T block)
throws WorldEditException {
return getParent().setBlock(position, block);
}
@Override
default boolean setBiome(BlockVector2 position, BiomeType biome) {
return getParent().setBiome(position, biome);
}
}

Datei anzeigen

@ -30,7 +30,7 @@ import org.jetbrains.annotations.Range;
*/ */
public class ChunkHolder<T extends Future<T>> implements IQueueChunk<T> { public class ChunkHolder<T extends Future<T>> implements IQueueChunk<T> {
private static Pool<ChunkHolder> POOL = FaweCache.IMP.registerPool(ChunkHolder.class, ChunkHolder::new, Settings.IMP.QUEUE.POOL); private static final Pool<ChunkHolder> POOL = FaweCache.IMP.registerPool(ChunkHolder.class, ChunkHolder::new, Settings.IMP.QUEUE.POOL);
public static ChunkHolder newInstance() { public static ChunkHolder newInstance() {
return POOL.poll(); return POOL.poll();
@ -39,11 +39,11 @@ public class ChunkHolder<T extends Future<T>> implements IQueueChunk<T> {
private IChunkGet chunkExisting; // The existing chunk (e.g. a clipboard, or the world, before changes) private IChunkGet chunkExisting; // The existing chunk (e.g. a clipboard, or the world, before changes)
private IChunkSet chunkSet; // The blocks to be set to the chunkExisting private IChunkSet chunkSet; // The blocks to be set to the chunkExisting
private IBlockDelegate delegate; // delegate handles the abstraction of the chunk layers private IBlockDelegate delegate; // delegate handles the abstraction of the chunk layers
private IQueueExtent extent; // the parent queue extent which has this chunk private IQueueExtent<? extends IChunk> extent; // the parent queue extent which has this chunk
private int chunkX; private int chunkX;
private int chunkZ; private int chunkZ;
public ChunkHolder() { private ChunkHolder() {
this.delegate = NULL; this.delegate = NULL;
} }
@ -346,8 +346,7 @@ public class ChunkHolder<T extends Future<T>> implements IQueueChunk<T> {
} }
/** /**
* Get or create the existing part of this chunk * Get or create the existing part of this chunk.
* @return
*/ */
public final IChunkGet getOrCreateGet() { public final IChunkGet getOrCreateGet() {
if (chunkExisting == null) { if (chunkExisting == null) {
@ -357,8 +356,7 @@ public class ChunkHolder<T extends Future<T>> implements IQueueChunk<T> {
} }
/** /**
* Get or create the settable part of this chunk * Get or create the settable part of this chunk.
* @return
*/ */
public final IChunkSet getOrCreateSet() { public final IChunkSet getOrCreateSet() {
if (chunkSet == null) { if (chunkSet == null) {
@ -371,7 +369,6 @@ public class ChunkHolder<T extends Future<T>> implements IQueueChunk<T> {
* Create a wrapped set object * Create a wrapped set object
* - The purpose of wrapping is to allow different extents to intercept / alter behavior * - The purpose of wrapping is to allow different extents to intercept / alter behavior
* - e.g., caching, optimizations, filtering * - e.g., caching, optimizations, filtering
* @return
*/ */
private IChunkSet newWrappedSet() { private IChunkSet newWrappedSet() {
return extent.getCachedSet(chunkX, chunkZ); return extent.getCachedSet(chunkX, chunkZ);
@ -381,7 +378,6 @@ public class ChunkHolder<T extends Future<T>> implements IQueueChunk<T> {
* Create a wrapped get object * Create a wrapped get object
* - The purpose of wrapping is to allow different extents to intercept / alter behavior * - The purpose of wrapping is to allow different extents to intercept / alter behavior
* - e.g., caching, optimizations, filtering * - e.g., caching, optimizations, filtering
* @return
*/ */
private IChunkGet newWrappedGet() { private IChunkGet newWrappedGet() {
return extent.getCachedGet(chunkX, chunkZ); return extent.getCachedGet(chunkX, chunkZ);
@ -423,9 +419,8 @@ public class ChunkHolder<T extends Future<T>> implements IQueueChunk<T> {
/** /**
* Get the extent this chunk is in * Get the extent this chunk is in
* @return
*/ */
public IQueueExtent getExtent() { public IQueueExtent<? extends IChunk> getExtent() {
return extent; return extent;
} }

Datei anzeigen

@ -1,31 +0,0 @@
package com.boydti.fawe.beta.implementation.chunk;
import com.boydti.fawe.beta.IChunk;
import com.boydti.fawe.beta.IDelegateChunk;
import com.boydti.fawe.beta.IQueueChunk;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.world.biome.BiomeType;
import java.util.Set;
import java.util.UUID;
/**
* Implementation of IDelegateChunk
*/
public class DelegateChunk<T extends IQueueChunk> implements IDelegateChunk<T> {
private T parent;
public DelegateChunk(final T parent) {
this.parent = parent;
}
@Override
public final T getParent() {
return parent;
}
public final void setParent(final T parent) {
this.parent = parent;
}
}