KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > ObjectUtils


1 package org.apache.turbine.util;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.BufferedInputStream JavaDoc;
20 import java.io.BufferedOutputStream JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.ObjectInputStream JavaDoc;
25 import java.io.ObjectOutputStream JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * This is where common Object manipulation routines should go.
33  *
34  * @author <a HREF="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
35  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
36  * @version $Id: ObjectUtils.java,v 1.6.2.3 2004/08/16 23:24:19 henning Exp $
37  */

38 public abstract class ObjectUtils
39 {
40     /**
41      * Returns a default value if the object passed is null.
42      *
43      * @param o The object to test.
44      * @param dflt The default value to return.
45      * @return The object o if it is not null, dflt otherwise.
46      * @deprecated Use org.apache.commons.lang.ObjectUtils.defaultIfNull()
47      */

48     public static Object JavaDoc isNull(Object JavaDoc o, Object JavaDoc dflt)
49     {
50         return org.apache.commons.lang.ObjectUtils.defaultIfNull(o,dflt);
51     }
52
53     /**
54      * Adds an object to a vector, making sure the object is in the
55      * vector only once.
56      *
57      * @param v The vector.
58      * @param o The object.
59      * @deprecated No replacement
60      */

61     public static void addOnce(List JavaDoc l, Object JavaDoc o)
62     {
63         if (!l.contains(o))
64         {
65             l.add(o);
66         }
67     }
68
69     /**
70      * Converts a hashtable to a byte array for storage/serialization.
71      *
72      * @param hash The Hashtable to convert.
73      *
74      * @return A byte[] with the converted Hashtable.
75      *
76      * @exception Exception A generic exception.
77      */

78     public static byte[] serializeHashtable(Hashtable JavaDoc hash)
79         throws Exception JavaDoc
80     {
81         Hashtable JavaDoc saveData = new Hashtable JavaDoc(hash.size());
82         String JavaDoc key = null;
83         Object JavaDoc value = null;
84         byte[] byteArray = null;
85
86         Enumeration JavaDoc keys = hash.keys();
87
88         while (keys.hasMoreElements())
89         {
90             key = (String JavaDoc) keys.nextElement();
91             value = hash.get(key);
92             if (value instanceof Serializable JavaDoc)
93             {
94                 saveData.put (key, value);
95             }
96         }
97
98         ByteArrayOutputStream JavaDoc baos = null;
99         BufferedOutputStream JavaDoc bos = null;
100         ObjectOutputStream JavaDoc out = null;
101         try
102         {
103             // These objects are closed in the finally.
104
baos = new ByteArrayOutputStream JavaDoc();
105             bos = new BufferedOutputStream JavaDoc(baos);
106             out = new ObjectOutputStream JavaDoc(bos);
107
108             out.writeObject(saveData);
109             out.flush();
110             bos.flush();
111
112             byteArray = baos.toByteArray();
113         }
114         finally
115         {
116             if (out != null)
117             {
118                 out.close();
119             }
120             if (bos != null)
121             {
122                 bos.close();
123             }
124             if (baos != null)
125             {
126                 baos.close();
127             }
128         }
129         return byteArray;
130     }
131
132     /**
133      * Deserializes a single object from an array of bytes.
134      *
135      * @param objectData The serialized object.
136      *
137      * @return The deserialized object, or <code>null</code> on failure.
138      */

139     public static Object JavaDoc deserialize(byte[] objectData)
140     {
141         Object JavaDoc object = null;
142
143         if (objectData != null)
144         {
145             // These streams are closed in finally.
146
ObjectInputStream JavaDoc in = null;
147             ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(objectData);
148             BufferedInputStream JavaDoc bufin = new BufferedInputStream JavaDoc(bin);
149             
150             try
151             {
152                 in = new ObjectInputStream JavaDoc(bufin);
153
154                 // If objectData has not been initialized, an
155
// exception will occur.
156
object = in.readObject();
157             }
158             catch (Exception JavaDoc e)
159             {
160             }
161             finally
162             {
163                 try
164                 {
165                     if (in != null)
166                     {
167                         in.close();
168                     }
169                     if (bufin != null)
170                     {
171                         bufin.close();
172                     }
173                     if (bin != null)
174                     {
175                         bin.close();
176                     }
177                 }
178                 catch (IOException JavaDoc e)
179                 {
180                 }
181             }
182         }
183         return object;
184     }
185
186     /**
187      * Compares two Objects, returns true if their values are the
188      * same. It checks for null values prior to an o1.equals(o2)
189      * check
190      *
191      * @param o1 The first object.
192      * @param o2 The second object.
193      * @return True if the values of both xstrings are the same.
194      * @deprecated Use org.apache.commons.lang.ObjectUtils.equals()
195      */

196     public static boolean equals(Object JavaDoc o1, Object JavaDoc o2)
197     {
198         return org.apache.commons.lang.ObjectUtils.equals(o1,o2);
199     }
200
201     /**
202      * Nice method for adding data to a Hashtable in such a way
203      * as to not get NPE's. The point being that if the
204      * value is null, Hashtable.put() will throw an exception.
205      * That blows in the case of this class cause you may want to
206      * essentially treat put("Not Null", null ) == put("Not Null", "")
207      * We will still throw a NPE if the key is null cause that should
208      * never happen.
209      * @deprecated No replacement
210      */

211     public static final void safeAddToHashtable(Hashtable JavaDoc hash, Object JavaDoc key,
212                                                 Object JavaDoc value)
213             throws NullPointerException JavaDoc
214     {
215         if (value == null)
216         {
217             hash.put(key, "");
218         }
219         else
220         {
221             hash.put(key, value);
222         }
223     }
224 }
225
Popular Tags