X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fpersistence%2FAbstractDetachable.java;fp=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fpersistence%2FAbstractDetachable.java;h=1e7461bf1101ea7029affad90925625a4b3672e4;hb=0160b65d4c0f112ae839140980e80b4fa6f348d5;hp=0000000000000000000000000000000000000000;hpb=7636c30a1fecb9598a4c5bc35aaa53d765390a42;p=utils diff --git a/support/general/src/main/java/org/wamblee/persistence/AbstractDetachable.java b/support/general/src/main/java/org/wamblee/persistence/AbstractDetachable.java new file mode 100644 index 00000000..1e7461bf --- /dev/null +++ b/support/general/src/main/java/org/wamblee/persistence/AbstractDetachable.java @@ -0,0 +1,115 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.persistence; + +/** + * Detachable implementation that takes care of the basic logic for detachable + * objects. All that needs to be done is to implement {@link #load()}. + * + * @param + * The type of the object to be attached/detached + * @param + * The type of the reference to store when the object is detached. + */ +public abstract class AbstractDetachable implements Detachable { + + private T object; + private Ref reference; + + /** + * Constructs the detachable. + * + * @param aObject + * Object. + * @throws IllegalArgumentException + * When the object passed in is null. + */ + protected AbstractDetachable(T aObject) { + if (aObject == null) { + throw new IllegalArgumentException("Object '" + aObject + + "' is null"); + } + object = aObject; + reference = null; + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.persistence.Detachable#detach() + */ + public void detach() { + if (object == null) { + return; // Nothing to do. + } + reference = getReference(object); + if (reference == null) { + throw new IllegalStateException("Object '" + object + + "' not persisted yet'"); + } + object = null; + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.persistence.Detachable#get() + */ + public T get() { + if (object == null) { + object = load(reference); + reference = null; + } + return object; + } + + /** + * For testing. + * + * @return current object. + */ + T getObject() { + return object; + } + + /** + * For testing. + * + * @return The reference. + */ + Ref getReference() { + return reference; + } + + /** + * Loads the object based on a reference. + * + * @param aReference + * Reference. + * @return Object (may be null ). + */ + protected abstract T load(Ref aReference); + + /** + * Obtains the reference for a given object. + * + * @param aObject + * Object. + * @return Reference. + */ + protected abstract Ref getReference(T aObject); + +}