package org.warp.jcwdb.ann; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.nio.file.Path; import java.util.function.Supplier; public class JCWDatabase { private final DatabaseManager database; private final DataLoader dataLoader; private final DataInitializer dataInitializer; public JCWDatabase(Path dataFile, Path metadataFile, boolean registrationRequired) throws IOException { this.dataLoader = new DataLoader(this, dataFile, metadataFile, registrationRequired); this.dataInitializer = new DataInitializer(dataLoader); this.database = new DatabaseManager(dataLoader); } public T loadRoot(Class rootClass) throws IOException { return loadRoot(rootClass, () -> { try { return rootClass.getConstructor(JCWDatabase.class).newInstance(this); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new IOException(e); } }); } public T loadRoot(Class rootClass, SupplierWithIO ifAbsent) throws IOException { return database.loadRoot(rootClass, ifAbsent); } public void registerClass(Class clazz, int id) { if (id < 0) { throw new IllegalArgumentException(); } dataLoader.registerClass(clazz, id); } public void close() throws IOException { database.close(); } public DataLoader getDataLoader() { return dataLoader; } public DataInitializer getDataInitializer() { return dataInitializer; } public void initializeDBObject(DBObject dbObject) throws IOException { dataInitializer.initializeDBObject(dbObject); } }