strangedb/src/main/java/it/cavallium/strangedb/database/DatabaseEnhancedObjectUpgra...

62 lines
2.1 KiB
Java
Raw Normal View History

2019-03-07 11:41:45 +01:00
package it.cavallium.strangedb.database;
2019-03-02 17:47:24 +01:00
2019-03-07 11:41:45 +01:00
import it.cavallium.strangedb.annotations.DbDataType;
import it.cavallium.strangedb.annotations.DbPrimitiveType;
2019-03-06 17:25:56 +01:00
import it.unimi.dsi.fastutil.longs.LongList;
2019-03-07 11:41:45 +01:00
import it.cavallium.strangedb.EnhancedObjectUpgrader;
2019-03-02 17:47:24 +01:00
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;
2019-03-06 17:25:56 +01:00
private final long primitiveDataRef;
2019-03-02 17:47:24 +01:00
2019-03-06 17:25:56 +01:00
public DatabaseEnhancedObjectUpgrader(DatabaseObjectsIO objectsIO, long[] fieldRefs, long[] methodRefs, long primitiveDataRef) {
2019-03-02 17:47:24 +01:00
this.objectsIO = objectsIO;
this.fieldRefs = fieldRefs;
this.methodRefs = methodRefs;
2019-03-06 17:25:56 +01:00
this.primitiveDataRef = primitiveDataRef;
2019-03-02 17:47:24 +01:00
}
@Override
@SuppressWarnings("unchecked")
2019-03-07 11:41:45 +01:00
public Object getField(int id, DbDataType type, Supplier<Class<?>> enhancedClassType) throws IOException {
2019-03-02 17:47:24 +01:00
return objectsIO.loadData(type, fieldRefs[id], enhancedClassType);
}
@Override
@SuppressWarnings("unchecked")
2019-03-07 11:41:45 +01:00
public Object getMethod(int id, DbDataType type, Supplier<Class<?>> enhancedClassType) throws IOException {
2019-03-02 17:47:24 +01:00
return objectsIO.loadData(type, methodRefs[id], enhancedClassType);
}
2019-03-06 17:25:56 +01:00
@SuppressWarnings("unchecked")
@Override
2019-03-07 11:41:45 +01:00
public <T> T getPrimitiveField(int id, DbPrimitiveType type) throws IOException {
2019-03-06 17:25:56 +01:00
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");
}
}
2019-03-02 17:47:24 +01:00
}