package org.warp.jcwdb; import java.io.IOException; /** * You must have only a maximum of 1 reference for each index * @param */ public class EntryReference implements Castable { private final JCWDatabase.EntryReferenceTools db; private final long entryIndex; private final DBTypeParser parser; public T value; private volatile boolean closed; private final Object closeLock = new Object(); public EntryReference(JCWDatabase.EntryReferenceTools db, long entryId, DBTypeParser parser) throws IOException { this.db = db; this.entryIndex = entryId; this.parser = parser; this.value = db.read(entryId, parser.getReader()); } public EntryReference(JCWDatabase.EntryReferenceTools db, long entryId, DBTypeParser parser, T value) { this.db = db; this.entryIndex = entryId; this.parser = parser; this.value = value; } public DBTypeParser getParser() { return parser; } public long getIndex() { return entryIndex; } public void save() throws IOException { if (!closed) { db.write(entryIndex, parser.getWriter(value)); } } @Override public T cast() { return (T) this; } protected void close() throws IOException { if (closed) { return; } synchronized (closeLock) { if (closed) { return; } save(); closed = true; } } }