KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > state > LifeCycleState


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: LifeCycleState.java,v 1.7 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.state;
12
13 import javax.jdo.Transaction;
14 import org.apache.log4j.Category;
15
16
17 abstract class LifeCycleState
18 {
19     protected static final Category LOG = Category.getInstance(LifeCycleState.class);
20
21     public static final int
22         HOLLOW = 0,
23         P_CLEAN = 1,
24         P_DIRTY = 2,
25         P_NEW = 3,
26         P_NEW_DELETED = 4,
27         P_DELETED = 5,
28         P_NONTRANS = 6,
29         T_CLEAN = 7,
30         T_DIRTY = 8,
31         TRANSIENT = 9,
32         TOTAL = 10;
33
34     protected boolean isPersistent;
35     protected boolean isTransactional;
36     protected boolean isDirty;
37     protected boolean isNew;
38     protected boolean isDeleted;
39
40     protected int stateType;
41
42     private static LifeCycleState states[];
43
44     static
45     {
46         states = new LifeCycleState[TOTAL];
47
48         states[HOLLOW] = new Hollow();
49         states[P_CLEAN] = new PersistentClean();
50         states[P_DIRTY] = new PersistentDirty();
51         states[P_NEW] = new PersistentNew();
52         states[P_NEW_DELETED] = new PersistentNewDeleted();
53         states[P_DELETED] = new PersistentDeleted();
54         states[P_NONTRANS] = new PersistentNontransactional();
55         states[T_CLEAN] = new TransientClean();
56         states[T_DIRTY] = new TransientDirty();
57         states[TRANSIENT] = null;
58     }
59
60     /**
61      * Returns the LifeCycleState for the state constant.
62      *
63      * @param stateType the type as integer
64      *
65      * @return the type as LifeCycleState object
66      */

67
68     public static LifeCycleState getLifeCycleState(int stateType)
69     {
70         return states[stateType];
71     }
72
73     /**
74      * Returns the type of the life cycle state
75      *
76      * @return the type of this life cycle state
77      */

78
79     public final int stateType()
80     {
81         return stateType;
82     }
83
84     protected final LifeCycleState changeState(StateManagerImpl sm, int newStateType)
85     {
86         return changeState(sm, states[newStateType]);
87     }
88
89     private LifeCycleState changeState(StateManagerImpl sm, LifeCycleState newState)
90     {
91         if (LOG.isDebugEnabled())
92             LOG.debug(sm.toString() + ": " + this + "->" + newState);
93
94         return newState;
95     }
96
97     public LifeCycleState transitionRollbackState(StateManagerImpl sm, LifeCycleState oldState)
98     {
99         if (isTransactional)
100         {
101             if (oldState == null || !oldState.isTransactional)
102                 sm.evictFromTransaction();
103         }
104         else
105         {
106             if (oldState != null && oldState.isTransactional)
107                 sm.enlistInTransaction();
108         }
109
110         return changeState(sm, oldState);
111     }
112
113     public LifeCycleState transitionMakePersistent(StateManagerImpl sm, Transaction tx)
114     {
115         return this;
116     }
117
118     public LifeCycleState transitionDeletePersistent(StateManagerImpl sm, Transaction tx)
119     {
120         return this;
121     }
122
123     public LifeCycleState transitionMakeTransactional(StateManagerImpl sm, Transaction tx)
124     {
125         return this;
126     }
127
128     public LifeCycleState transitionMakeNontransactional(StateManagerImpl sm)
129     {
130         return this;
131     }
132
133     public LifeCycleState transitionMakeTransient(StateManagerImpl sm)
134     {
135         return this;
136     }
137
138     public LifeCycleState transitionCommit(StateManagerImpl sm, Transaction tx)
139     {
140         return this;
141     }
142
143     public LifeCycleState transitionRollback(StateManagerImpl sm, Transaction tx)
144     {
145         return this;
146     }
147
148     public LifeCycleState transitionRefresh(StateManagerImpl sm, Transaction tx)
149     {
150         return this;
151     }
152
153     public LifeCycleState transitionEvict(StateManagerImpl sm)
154     {
155         return this;
156     }
157
158     public LifeCycleState transitionReadField(StateManagerImpl sm, Transaction tx)
159     {
160         return this;
161     }
162
163     public LifeCycleState transitionWriteField(StateManagerImpl sm, Transaction tx)
164     {
165         return this;
166     }
167
168     public LifeCycleState transitionRetrieve(StateManagerImpl sm, Transaction tx, boolean DFGOnly)
169     {
170         return this;
171     }
172
173     /**
174      * Returns whether the state is persistent.
175      */

176
177     public final boolean isPersistent()
178     {
179         return isPersistent;
180     }
181
182     /**
183      * Returns whether the state is transactional.
184      */

185
186     public final boolean isTransactional()
187     {
188         return isTransactional;
189     }
190
191     /**
192      * Returns whether the state is dirty, ie the object has been changed
193      * (created, updated, deleted) in this Tx.
194      */

195
196     public final boolean isDirty()
197     {
198         return isDirty;
199     }
200
201     /**
202      * Returns whether the state represents a newly created object.
203      */

204
205     public final boolean isNew()
206     {
207         return isNew;
208     }
209
210     /**
211      * Return whether the state represents a deleted object.
212      */

213
214     public final boolean isDeleted()
215     {
216         return isDeleted;
217     }
218 }
219
Popular Tags