ZotCore
Добавлено api для работы с поинтерами
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.reider745.api.pointers;
|
||||
|
||||
public class PointerClass {
|
||||
public final long pointer;
|
||||
public final PointersStorage storage;
|
||||
|
||||
public PointerClass(){
|
||||
final Class<?> cl = getClass();
|
||||
storage = PointersStorage.getStorageForClassType(cl.getPackage()+"."+cl.getName());
|
||||
pointer = storage.addPointer(this);
|
||||
}
|
||||
|
||||
public final long getPointer(){
|
||||
return pointer;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void finalize() throws Throwable {
|
||||
storage.removePointer(pointer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.reider745.api.pointers;
|
||||
|
||||
import com.reider745.api.pointers.pointer_gen.IBasePointerGen;
|
||||
import com.reider745.api.pointers.pointer_gen.PointerGenSlowest;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PointersStorage {
|
||||
private static final HashMap<String, PointersStorage> storages = new HashMap<>();
|
||||
private static final HashMap<String, PointersStorage> storagesByClassType = new HashMap<>();
|
||||
|
||||
private final HashMap<Long, WeakReference<PointerClass>> pointers = new HashMap<>();
|
||||
private final IBasePointerGen pointerGen;
|
||||
|
||||
public PointersStorage(String type, Class<? extends PointerClass> class_type, final IBasePointerGen pointerGen){
|
||||
this.pointerGen = pointerGen;
|
||||
storages.put(type, this);
|
||||
final Class<?> cl = getClass();
|
||||
storagesByClassType.put(cl.getPackage()+"."+cl.getName(), this);
|
||||
}
|
||||
|
||||
public PointersStorage(String type, Class<? extends PointerClass> class_type){
|
||||
this(type, class_type, new PointerGenSlowest());
|
||||
}
|
||||
|
||||
public final long addPointer(PointerClass pointerClass){
|
||||
long ptr = pointerGen.next();
|
||||
pointers.put(ptr, new WeakReference<>(pointerClass));
|
||||
return ptr;
|
||||
}
|
||||
|
||||
public final PointerClass get(long ptr){
|
||||
return pointers.get(ptr).get();
|
||||
}
|
||||
|
||||
public final void removePointer(long pointer){
|
||||
pointers.remove(pointer);
|
||||
pointerGen.remove(pointer);
|
||||
}
|
||||
|
||||
public static PointersStorage getStorageForType(final String type){
|
||||
return storages.get(type);
|
||||
}
|
||||
|
||||
public static PointersStorage getStorageForClassType(final String class_type){
|
||||
return storagesByClassType.get(class_type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.reider745.api.pointers.pointer_gen;
|
||||
|
||||
public interface IBasePointerGen {
|
||||
long next();
|
||||
void remove(final long ptr);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.reider745.api.pointers.pointer_gen;
|
||||
|
||||
public class PointerGenFastest implements IBasePointerGen {
|
||||
public long pre = Long.MIN_VALUE;
|
||||
|
||||
@Override
|
||||
public final long next() {
|
||||
final long result = pre;
|
||||
pre++;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void remove(long ptr) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.reider745.api.pointers.pointer_gen;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class PointerGenSlowest implements IBasePointerGen {
|
||||
public final ArrayList<Long> usePointers = new ArrayList<>();
|
||||
public final Random random = new Random();
|
||||
|
||||
@Override
|
||||
public final long next() {
|
||||
while (true){
|
||||
final long ptr = random.nextLong();
|
||||
if(!usePointers.contains(ptr)){
|
||||
usePointers.add(ptr);
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void remove(long ptr) {
|
||||
usePointers.remove(ptr);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,9 @@ import com.google.common.io.ByteStreams;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.reider745.InnerCoreServer;
|
||||
import com.reider745.api.pointers.PointerClass;
|
||||
import com.reider745.api.pointers.PointersStorage;
|
||||
import com.reider745.api.pointers.pointer_gen.PointerGenFastest;
|
||||
import com.reider745.block.CustomBlock;
|
||||
import com.reider745.item.CustomItem;
|
||||
import com.zhekasmirnov.apparatus.api.player.NetworkPlayerRegistry;
|
||||
@@ -95,6 +98,11 @@ public class Apparatus {
|
||||
CustomBlock.init();
|
||||
|
||||
Logger.info("INNERCORE", "end load, time: "+(System.currentTimeMillis()-start));
|
||||
|
||||
new PointersStorage("test", PointerClass.class);
|
||||
|
||||
for(int i = 0;i < 100;i++)
|
||||
new PointerClass();
|
||||
}
|
||||
|
||||
public static void initCreativeItems(){
|
||||
|
||||
Reference in New Issue
Block a user