All Downloads are FREE. Search and download functionalities are using the official Maven repository.

persistence.PersistentObject Maven / Gradle / Ivy

The newest version!
package persistence;

import java.util.Iterator;

public class PersistentObject implements Cloneable {
	transient PersistentClass clazz;
	transient Store store;
	transient Long base;

	public PersistentObject() {
	}

	public PersistentObject(final Store store) {
		this(store, null);
	}

	PersistentObject(final Store store, final PersistentClass clazz) {
		this.store = store;
		this.clazz = clazz == null?store.persistentClass(this):clazz;
		store.create(this);
	}

	final PersistentClass createClass() {
		final persistence.annotation.PersistentClass ann = getClass().getAnnotation(persistence.annotation.PersistentClass.class);
		final Class cls = ann == null?PersistentClass.class:ann.value();
		try {
			return cls.getConstructor(PersistentObject.class).newInstance(this);
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}
	}

	protected final  T get(String name) {
		return get(clazz.getField(name));
	}

	protected final  T set(String name, T value) {
		return set(clazz.getField(name),value);
	}

	@SuppressWarnings("unchecked")
	 T get(Field field) {
		return (T)store.get(base,field);
	}

	synchronized  T set(Field field, T value) {
		T obj=get(field);
		store.set(base,field,value);
		return obj;
	}

	protected final Store getStore() {
		return store;
	}

	public int hashCode() {
		return base.hashCode();
	}

	public final boolean equals(Object obj) {
		return this == obj || (obj instanceof PersistentObject && equals((PersistentObject)obj));
	}

	protected boolean equals(PersistentObject obj) {
		return base.equals(obj.base);
	}

	public String toString() {
		return clazz.name()+"@"+Long.toHexString(base);
	}

	public PersistentObject clone() throws CloneNotSupportedException {
		final PersistentObject obj = (PersistentObject)super.clone();
		store.create(obj);
		final Iterator t = clazz.fieldIterator();
		while (t.hasNext()) {
			final Field field = (Field)t.next();
			obj.set(field, get(field));
		}
		return obj;
	}

	protected final void finalize() {
		store.release(this);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy