package it.cavallium.strangedb.database; import it.cavallium.strangedb.annotations.DbDataType; import it.cavallium.strangedb.annotations.DbPrimitiveType; import it.unimi.dsi.fastutil.longs.LongList; import it.cavallium.strangedb.EnhancedObjectUpgrader; 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> enhancedClassType) throws IOException { return objectsIO.loadData(type, fieldRefs[id], enhancedClassType); } @Override @SuppressWarnings("unchecked") public Object getMethod(int id, DbDataType type, Supplier> enhancedClassType) throws IOException { return objectsIO.loadData(type, methodRefs[id], enhancedClassType); } @SuppressWarnings("unchecked") @Override public 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"); } } }