(no commit message)
[utils] / support / general / src / main / java / org / wamblee / persistence / AbstractDetachable.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.persistence;
17
18 /**
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()}.
21  * 
22  * @param <T>
23  *            The type of the object to be attached/detached
24  * @param <Ref>
25  *            The type of the reference to store when the object is detached.
26  */
27 public abstract class AbstractDetachable<T, Ref> implements Detachable<T> {
28
29     private T object;
30     private Ref reference;
31
32     /**
33      * Constructs the detachable.
34      * 
35      * @param aObject
36      *            Object.
37      * @throws IllegalArgumentException
38      *             When the object passed in is null.
39      */
40     protected AbstractDetachable(T aObject) {
41         if (aObject == null) {
42             throw new IllegalArgumentException("Object is null");
43         }
44         object = aObject;
45         reference = null;
46     }
47
48     /*
49      * (non-Javadoc)
50      * 
51      * @see org.wamblee.persistence.Detachable#detach()
52      */
53     public void detach() {
54         if (object == null) {
55             return; // Nothing to do.
56         }
57         reference = getReference(object);
58         if (reference == null) {
59             throw new IllegalStateException("Object '" + object +
60                 "' not persisted yet'");
61         }
62         object = null;
63     }
64
65     /*
66      * (non-Javadoc)
67      * 
68      * @see org.wamblee.persistence.Detachable#get()
69      */
70     public T get() {
71         if (object == null) {
72             object = load(reference);
73             reference = null;
74         }
75         return object;
76     }
77
78     /**
79      * For testing.
80      * 
81      * @return current object.
82      */
83     T getObject() {
84         return object;
85     }
86
87     /**
88      * For testing.
89      * 
90      * @return The reference.
91      */
92     Ref getReference() {
93         return reference;
94     }
95
96     /**
97      * Loads the object based on a reference.
98      * 
99      * @param aReference
100      *            Reference.
101      * @return Object (may be null ).
102      */
103     protected abstract T load(Ref aReference);
104
105     /**
106      * Obtains the reference for a given object.
107      * 
108      * @param aObject
109      *            Object.
110      * @return Reference.
111      */
112     protected abstract Ref getReference(T aObject);
113
114 }