KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > data > BasicDataObject


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: BasicDataObject.java,v 1.8 2007/01/07 06:14:17 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.core.data;
23
24 import java.sql.Timestamp JavaDoc;
25
26 import org.opensubsystems.core.util.GlobalConstants;
27 import org.opensubsystems.core.util.HashCodeUtils;
28
29 /**
30  * Base class for all data objects, which want to track when they were created
31  * and which belongs to some partition (called domain).
32  *
33  * @version $Id: BasicDataObject.java,v 1.8 2007/01/07 06:14:17 bastafidli Exp $
34  * @author Miro Halas
35  * @code.reviewer Miro Halas
36  * @code.reviewed 1.6 2005/08/22 06:29:10 bastafidli
37  */

38 public abstract class BasicDataObject extends DataObject
39 {
40    // Attributes ///////////////////////////////////////////////////////////////
41

42    /**
43     * ID of the data object. This is private so that we can maintain the
44     * cached object value without fear that these two won't be in sync.
45     */

46    private int m_iId;
47    
48    /**
49     * Id of the domain this data object belongs to. Domain represents default
50     * partition where the data object belongs to (since it is pressumed that
51     * each data object belongs to some partitions).
52     */

53    protected int m_iDomainId;
54
55    /**
56     * Creation timestamp when the data object was created.
57     */

58    protected Timestamp JavaDoc m_creationTimestamp;
59
60    // Chached values ///////////////////////////////////////////////////////////
61

62    /**
63     * This is constructed only when somebody asks for it. It is private so that
64     * we can keep it in sync with the real id. It is here to implement Flyweight
65     * pattern as described in http://homepage.mac.com/loeffler/java/patterns/flywt.html
66     * by GoF95 (http://homepage.mac.com/loeffler/java/patterns.html)
67     * since if multiple clients of this class need object version of the id, this
68     * one object can be shared by all of them.
69     */

70    private Integer JavaDoc m_iIdObject;
71    
72    // Constructors /////////////////////////////////////////////////////////////
73

74    /**
75     * Default constructor.
76     */

77    public BasicDataObject(
78    )
79    {
80       this(DataObject.NEW_ID);
81    }
82
83    /**
84     * Simple constructor creating new data object in particular domain.
85     *
86     * @param iDomainId - domain this data object belongs to
87     */

88    public BasicDataObject(
89       int iDomainId
90    )
91    {
92       this(DataObject.NEW_ID, iDomainId, null);
93    }
94
95    /**
96     * Full constructor.
97     *
98     * @param iId - id of this data object
99     * @param iDomainId - domain this data object belongs to
100     * @param creationTimestamp - timestamp when the data object was created.
101     */

102    public BasicDataObject(
103       int iId,
104       int iDomainId,
105       Timestamp JavaDoc creationTimestamp
106    )
107    {
108       restore(iId, iDomainId, creationTimestamp);
109    }
110    
111    // Accessors ////////////////////////////////////////////////////////////////
112

113    /**
114     * @return int
115     */

116    public int getDomainId(
117    )
118    {
119       return m_iDomainId;
120    }
121    
122    /**
123     * {@inheritDoc}
124     */

125    public int getId()
126    {
127       return m_iId;
128    }
129
130    /**
131     * {@inheritDoc}
132     */

133    public Integer JavaDoc getIdAsObject()
134    {
135       // This doesn't have to be synchronized since it really doesn't matter
136
// if we create two objects in case of concurrent access
137
if (m_iIdObject == null)
138       {
139          if (m_iId == DataObject.NEW_ID)
140          {
141             m_iIdObject = NEW_ID_OBJ;
142          }
143          else
144          {
145             m_iIdObject = new Integer JavaDoc(m_iId);
146          }
147       }
148       
149       return m_iIdObject;
150    }
151
152    /**
153     * @param iNewId - new id of the data object
154     */

155    public void setId(
156       int iNewId
157    )
158    {
159       if (GlobalConstants.ERROR_CHECKING)
160       {
161          // Prevent changing id by accident
162
assert ((m_iId == DataObject.NEW_ID) || (iNewId == DataObject.NEW_ID))
163                 : "Cannot set id for object which already has id " + m_iId;
164       }
165
166       if (m_iId != iNewId)
167       {
168          m_iId = iNewId;
169          m_iIdObject = null;
170       }
171    }
172
173    /**
174     * Creation timestamp is timestamp when the data object was created.
175     *
176     * @return Timestamp
177     */

178    public Timestamp JavaDoc getCreationTimestamp()
179    {
180       return m_creationTimestamp;
181    }
182    
183    /**
184     * Creation timestamp is timestamp when the data object was created.
185     *
186     * @param creationTimestamp - new creation timestamp
187     */

188    public void setCreationTimestamp(
189       Timestamp JavaDoc creationTimestamp
190    )
191    {
192       if (GlobalConstants.ERROR_CHECKING)
193       {
194          assert ((m_creationTimestamp == null) || (creationTimestamp == null))
195                  : "Creation timestamp can be set only if it is null.";
196       }
197
198       m_creationTimestamp = creationTimestamp;
199    }
200
201    /**
202     * {@inheritDoc}
203     */

204    public boolean equals(
205       Object JavaDoc oObject
206    )
207    {
208       boolean bReturn = false;
209       BasicDataObject helper;
210
211       if (oObject == this)
212       {
213          bReturn = true;
214       }
215       else if ((oObject != null) && (oObject instanceof BasicDataObject))
216       {
217          helper = (BasicDataObject) oObject;
218          bReturn = (m_iId == helper.m_iId)
219                   && (m_iDomainId == helper.m_iDomainId)
220                   && ((m_creationTimestamp == null && helper.m_creationTimestamp == null)
221                      || ((m_creationTimestamp != null)
222                          && (m_creationTimestamp.equals(helper.m_creationTimestamp))))
223                   // And now compare all other "business" related attributes
224
&& (isSame(oObject));
225       }
226       return bReturn;
227    }
228
229    /**
230     * {@inheritDoc}
231     */

232    public int hashCode()
233    {
234       int iResult = HashCodeUtils.SEED;
235       iResult = HashCodeUtils.hash(iResult, m_iId);
236       return iResult;
237    }
238
239    // Helper methods ///////////////////////////////////////////////////////////
240

241    /**
242     * Restore all values from specified values. This is here to reinitialize
243     * object in case it needs to be reused or reconstructed (e.g. when rollback
244     * is issued).
245     *
246     * @param iId - id of this data object
247     * @param iDomainId - domain this data object belongs to
248     * @param creationTimestamp - timestamp when the data object was created.
249     */

250    protected void restore(
251       int iId,
252       int iDomainId,
253       Timestamp JavaDoc creationTimestamp
254    )
255    {
256       m_iId = iId;
257       m_iDomainId = iDomainId;
258       m_creationTimestamp = creationTimestamp;
259       m_iIdObject = null;
260    }
261 }
262
Popular Tags