KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > helpers > ObjectHandle


1 /**
2  * $Id: ObjectHandle.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 1997-2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.helpers;
30
31 import java.io.Serializable JavaDoc;
32
33 /**
34  * Luggage class to "pass-by-pointer" an object reference. One implementation of
35  * Martin Fowler's "pass-in-result-object" pattern. Is as serializable as the underlying
36  * object.
37  *
38  * @since JWare/core 0.2
39  * @author ssmc, &copy;1997-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
40  * @version 0.5
41  * @.safety multiple
42  * @.group impl,helper
43  * @.pattern Fowler.PassInResultObject
44  **/

45
46 public class ObjectHandle implements Serializable JavaDoc
47 {
48     /* -- seekrits -- */
49     private Object JavaDoc m_underlying;
50
51
52     /**
53      * Create new object handle initialized to <i>null</i>.
54      **/

55     public ObjectHandle()
56     {
57         super();
58         m_underlying=null;
59     }
60
61
62     /**
63      * Create new object handle initialized to given object.
64      **/

65     public ObjectHandle(Object JavaDoc o)
66     {
67         super();
68         m_underlying= o;
69     }
70
71
72     /**
73      * Create new object handle for given <span class="src">char</span>.
74      **/

75     public ObjectHandle(char c)
76     {
77         this(new Character JavaDoc(c));
78     }
79
80
81     /**
82      * Create new object handle for given <span class="src">integer</span>.
83      **/

84     public ObjectHandle(int i)
85     {
86         this(new Integer JavaDoc(i));
87     }
88
89
90     /**
91      * Create new object handle for given <span class="src">long</span>.
92      **/

93     public ObjectHandle(long l)
94     {
95         this(new Long JavaDoc(l));
96     }
97
98
99     /**
100      * Create new object handle for given <span class="src">float</span>.
101      **/

102     public ObjectHandle(float e)
103     {
104         this(new Float JavaDoc(e));
105     }
106
107
108     /**
109      * Create new object handle for given <span class="src">double</span>.
110      **/

111     public ObjectHandle(double d)
112     {
113         this(new Double JavaDoc(d));
114     }
115
116
117     /**
118      * Create new object handle for given <span class="src">boolean</span>.
119      **/

120     public ObjectHandle(boolean b)
121     {
122         this(b ? Boolean.TRUE : Boolean.FALSE);
123     }
124
125
126     /**
127      * Returns underlying object (can be <i>null</i>).
128      **/

129     public Object JavaDoc get()
130     {
131         return m_underlying;
132     }
133
134
135     /**
136      * Updates underlying object (can be set to <i>null</i>).
137      * @param o new object
138      **/

139     public void set(Object JavaDoc o)
140     {
141         m_underlying= o;
142     }
143
144
145     /**
146      * Updates underlying value to new <span class="src">integer</span> value.
147      * @param i new value
148      **/

149     public void set(int i)
150     {
151         m_underlying= new Integer JavaDoc(i);
152     }
153
154
155     /**
156      * Updates underlying value to new <span class="src">long</span> value.
157      * @param l new value
158      **/

159     public void set(long l)
160     {
161         m_underlying= new Long JavaDoc(l);
162     }
163
164
165     /**
166      * Updates underlying value to new <span class="src">float</span> value.
167      * @param e new value
168      **/

169     public void set(float e)
170     {
171         m_underlying= new Float JavaDoc(e);
172     }
173
174
175     /**
176      * Updates underlying value to new <span class="src">double</span> value.
177      * @param d new value
178      **/

179     public void set(double d)
180     {
181         m_underlying= new Double JavaDoc(d);
182     }
183
184
185     /**
186      * Updates underlying value to new <span class="src">boolean</span> value.
187      * @param b new value
188      **/

189     public void set(boolean b)
190     {
191         m_underlying= b ? Boolean.TRUE : Boolean.FALSE;
192     }
193
194
195     /**
196      * Updates underlying value to new <span class="src">char</span> value.
197      * @param c new value
198      **/

199     public void set(char c)
200     {
201         m_underlying= new Character JavaDoc(c);
202     }
203
204
205     /**
206      * Returns underlying object as a string (can be <i>null</i>).
207      **/

208     public String JavaDoc asString()
209     {
210         return (m_underlying==null) ? null : m_underlying.toString();
211     }
212
213
214     /**
215      * Returns underlying object as a string or
216      * "<span class="src">null</span>" if not defined.
217      **/

218     public String JavaDoc toString()
219     {
220         return String.valueOf(m_underlying);
221     }
222
223     /**
224      * Returns underlying object if a non-null <span class="src">String</span>
225      * or the empty string if it is <i>null</i>.
226      **/

227     public String JavaDoc asNonNullString()
228     {
229         return (m_underlying instanceof String JavaDoc) ? m_underlying.toString() : "";
230     }
231
232
233     /**
234      * Returns whether underlying object is <i>null</i>.
235      **/

236     public final boolean isnull()
237     {
238         return (m_underlying==null) ? true : false;
239     }
240
241
242     /**
243      * Determine if the given object's content is semantically equivalent
244      * to this handle's content.
245      * @param o object to compare (must be other ObjectHandle
246      * @return <i>true</i> if equivalent
247      **/

248     public boolean equals(Object JavaDoc o)
249     {
250         if (o==null) {
251             return false;
252         }
253         if (o==this) {
254             return true;
255         }
256         if (getClass().equals(o.getClass())) {
257             ObjectHandle oh= (ObjectHandle)o;
258             if (m_underlying==null) {
259                 return oh.m_underlying==null;
260             }
261             if (oh.m_underlying==null) {
262                 return false;
263             }
264             return m_underlying.equals(oh.m_underlying);
265         }
266         return false;
267     }
268
269
270     /**
271      * Returns hash value for this object handle. A handle's underlying
272      * object determines its has value (so the hash value changes if
273      * {@linkplain #set(java.lang.Object) set()} is used).
274      **/

275     public int hashCode()
276     {
277         return (m_underlying==null) ? 13 : m_underlying.hashCode();
278     }
279 }
280
281 /* end-of-ObjectHandle.java */
282
Popular Tags