62 lines
2.1 KiB
Java
62 lines
2.1 KiB
Java
package org.warp.cowdb.database;
|
|
|
|
import it.unimi.dsi.fastutil.longs.LongList;
|
|
import org.warp.cowdb.EnhancedObjectUpgrader;
|
|
import org.warp.jcwdb.ann.DBDataType;
|
|
import org.warp.jcwdb.ann.DBPrimitiveType;
|
|
|
|
import java.io.IOException;
|
|
import java.util.function.Supplier;
|
|
|
|
public class DatabaseEnhancedObjectUpgrader implements EnhancedObjectUpgrader {
|
|
private final DatabaseObjectsIO objectsIO;
|
|
private final long[] fieldRefs;
|
|
private final long[] methodRefs;
|
|
private final long primitiveDataRef;
|
|
|
|
public DatabaseEnhancedObjectUpgrader(DatabaseObjectsIO objectsIO, long[] fieldRefs, long[] methodRefs, long primitiveDataRef) {
|
|
this.objectsIO = objectsIO;
|
|
this.fieldRefs = fieldRefs;
|
|
this.methodRefs = methodRefs;
|
|
this.primitiveDataRef = primitiveDataRef;
|
|
}
|
|
|
|
@Override
|
|
@SuppressWarnings("unchecked")
|
|
public Object getField(int id, DBDataType type, Supplier<Class<?>> enhancedClassType) throws IOException {
|
|
return objectsIO.loadData(type, fieldRefs[id], enhancedClassType);
|
|
}
|
|
|
|
@Override
|
|
@SuppressWarnings("unchecked")
|
|
public Object getMethod(int id, DBDataType type, Supplier<Class<?>> enhancedClassType) throws IOException {
|
|
return objectsIO.loadData(type, methodRefs[id], enhancedClassType);
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
@Override
|
|
public <T> T getPrimitiveField(int id, DBPrimitiveType type) throws IOException {
|
|
LongList data = objectsIO.loadPrimitiveData(primitiveDataRef);
|
|
switch (type) {
|
|
case BOOLEAN:
|
|
return (T) ((Object) (data.getLong(id) % 2 == 1));
|
|
case BYTE:
|
|
return (T) ((Object) ((byte) data.getLong(id)));
|
|
case SHORT:
|
|
return (T) ((Object) ((short) data.getLong(id)));
|
|
case CHAR:
|
|
return (T) ((Object) ((char) data.getLong(id)));
|
|
case INTEGER:
|
|
return (T) ((Object) ((int) data.getLong(id)));
|
|
case LONG:
|
|
return (T) ((Object) ((long) data.getLong(id)));
|
|
case FLOAT:
|
|
return (T) ((Object) ((float) Float.intBitsToFloat((int) data.getLong(id))));
|
|
case DOUBLE:
|
|
return (T) ((Object) ((double) Double.longBitsToDouble(data.getLong(id))));
|
|
default:
|
|
throw new IOException("Type " + type + " not implemented");
|
|
}
|
|
}
|
|
}
|