KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > Objects


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software 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 GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.Array JavaDoc;
26
27 import java.lang.ref.Reference JavaDoc;
28
29 import java.io.IOException JavaDoc;
30 import java.io.ObjectOutputStream JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.ByteArrayOutputStream JavaDoc;
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.Serializable JavaDoc;
35
36 import org.jboss.util.stream.Streams;
37
38 /**
39  * A collection of <code>Object</code> utilities.
40  *
41  * @version <tt>$Revision: 1958 $</tt>
42  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
43  */

44 public final class Objects
45 {
46    /////////////////////////////////////////////////////////////////////////
47
// Coercion Methods //
48
/////////////////////////////////////////////////////////////////////////
49

50    /**
51     * Get a compatible constructor for the given value type
52     *
53     * @param type Class to look for constructor in
54     * @param valueType Argument type for constructor
55     * @return Constructor or null
56     */

57    public static Constructor JavaDoc getCompatibleConstructor(final Class JavaDoc type,
58                                                       final Class JavaDoc valueType)
59    {
60       // first try and find a constructor with the exact argument type
61
try {
62          return type.getConstructor(new Class JavaDoc[] { valueType });
63       }
64       catch (Exception JavaDoc ignore) {
65          // if the above failed, then try and find a constructor with
66
// an compatible argument type
67

68          // get an array of compatible types
69
Class JavaDoc[] types = type.getClasses();
70
71          for (int i=0; i<types.length; i++) {
72             try {
73                return type.getConstructor(new Class JavaDoc[] { types[i] });
74             }
75             catch (Exception JavaDoc ignore2) {}
76          }
77       }
78
79       // if we get this far, then we can't find a compatible constructor
80
return null;
81    }
82
83    /////////////////////////////////////////////////////////////////////////
84
// Cloning Methods //
85
/////////////////////////////////////////////////////////////////////////
86

87    /**
88     * Copy an serializable object deeply.
89     *
90     * @param obj Object to copy.
91     * @return Copied object.
92     *
93     * @throws IOException
94     * @throws ClassCastException
95     */

96    public static Object JavaDoc copy(final Serializable JavaDoc obj)
97       throws IOException JavaDoc, ClassNotFoundException JavaDoc
98    {
99       ObjectOutputStream JavaDoc out = null;
100       ObjectInputStream JavaDoc in = null;
101       Object JavaDoc copy = null;
102       
103       try {
104          // write the object
105
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
106          out = new ObjectOutputStream JavaDoc(baos);
107          out.writeObject(obj);
108          out.flush();
109
110          // read in the copy
111
byte data[] = baos.toByteArray();
112          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(data);
113          in = new ObjectInputStream JavaDoc(bais);
114          copy = in.readObject();
115       }
116       finally {
117          Streams.close(out);
118          Streams.close(in);
119       }
120
121       return copy;
122    }
123    
124
125    /////////////////////////////////////////////////////////////////////////
126
// Misc Methods //
127
/////////////////////////////////////////////////////////////////////////
128

129    /**
130     * Dereference the given object if it is <i>non-null</i> and is an
131     * instance of <code>Reference</code>. If the object is <i>null</i>
132     * then <i>null</i> is returned. If the object is not an instance of
133     * <code>Reference</code>, then the object is returned.
134     *
135     * @param obj Object to dereference.
136     * @return Dereferenced object.
137     */

138    public static Object JavaDoc deref(final Object JavaDoc obj) {
139       if (obj != null && obj instanceof Reference JavaDoc) {
140          Reference JavaDoc ref = (Reference JavaDoc)obj;
141          return ref.get();
142       }
143
144       return obj;
145    }
146       
147    /**
148     * Check if the given object is an array (primitve or native).
149     *
150     * @param obj Object to test.
151     * @return True of the object is an array.
152     */

153    public static boolean isArray(final Object JavaDoc obj) {
154       if (obj != null)
155          return obj.getClass().isArray();
156       return false;
157    }
158
159    /**
160     * Return an Object array for the given object.
161     *
162     * @param obj Object to convert to an array. Converts primitive
163     * arrays to Object arrays consisting of their wrapper
164     * classes. If the object is not an array (object or primitve)
165     * then a new array of the given type is created and the
166     * object is set as the sole element.
167     */

168    public static Object JavaDoc[] toArray(final Object JavaDoc obj) {
169       // if the object is an array, the cast and return it.
170
if (obj instanceof Object JavaDoc[]) {
171          return (Object JavaDoc[])obj;
172       }
173
174       // if the object is an array of primitives then wrap the array
175
Class JavaDoc type = obj.getClass();
176       Object JavaDoc array;
177       if (type.isArray()) {
178          int length = Array.getLength(obj);
179          Class JavaDoc componentType = type.getComponentType();
180          array = Array.newInstance(componentType, length);
181          for (int i=0; i<length; i++) {
182             Array.set(array, i, Array.get(obj, i));
183          }
184       }
185       else {
186          array = Array.newInstance(type, 1);
187          Array.set(array, 0, obj);
188       }
189
190       return (Object JavaDoc[])array;
191    }
192
193    /**
194     * Test the equality of two object arrays.
195     *
196     * @param a The first array.
197     * @param b The second array.
198     * @param deep True to traverse elements which are arrays.
199     * @return True if arrays are equal.
200     */

201    public static boolean equals(final Object JavaDoc[] a, final Object JavaDoc[] b,
202                                 final boolean deep)
203    {
204       if (a == b) return true;
205       if (a == null || b == null) return false;
206       if (a.length != b.length) return false;
207
208       for (int i=0; i<a.length; i++) {
209          Object JavaDoc x = a[i];
210          Object JavaDoc y = b[i];
211
212          if (x != y) return false;
213          if (x == null || y == null) return false;
214          if (deep) {
215             if (x instanceof Object JavaDoc[] && y instanceof Object JavaDoc[]) {
216                if (! equals((Object JavaDoc[])x, (Object JavaDoc[])y, true)) return false;
217             }
218             else {
219                return false;
220             }
221          }
222          if (! x.equals(y)) return false;
223       }
224
225       return true;
226    }
227
228    /**
229     * Test the equality of two object arrays.
230     *
231     * @param a The first array.
232     * @param b The second array.
233     * @return True if arrays are equal.
234     */

235    public static boolean equals(final Object JavaDoc[] a, final Object JavaDoc[] b) {
236       return equals(a, b, true);
237    }
238 }
239
Popular Tags