2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.persistence;
19 * Detachable implementation that takes care of the basic logic for detachable
20 * objects. All that needs to be done is to implement {@link #load()}.
23 * The type of the object to be attached/detached
25 * The type of the reference to store when the object is detached.
27 public abstract class AbstractDetachable<T, Ref> implements Detachable<T> {
30 private Ref reference;
33 * Constructs the detachable.
37 * @throws IllegalArgumentException
38 * When the object passed in is null.
40 protected AbstractDetachable(T aObject) {
41 if (aObject == null) {
42 throw new IllegalArgumentException("Object '" + aObject +
52 * @see org.wamblee.persistence.Detachable#detach()
54 public void detach() {
56 return; // Nothing to do.
58 reference = getReference(object);
59 if (reference == null) {
60 throw new IllegalStateException("Object '" + object +
61 "' not persisted yet'");
69 * @see org.wamblee.persistence.Detachable#get()
73 object = load(reference);
82 * @return current object.
91 * @return The reference.
98 * Loads the object based on a reference.
102 * @return Object (may be null ).
104 protected abstract T load(Ref aReference);
107 * Obtains the reference for a given object.
113 protected abstract Ref getReference(T aObject);