KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > ExplorerUtils


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2005 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library 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 library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================
26 $Id: ExplorerUtils.java,v 1.2 2005/07/06 15:36:01 moroy Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.explorer;
30
31 import java.net.MalformedURLException JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 import org.objectweb.util.explorer.core.common.lib.ClassResolver;
38 import org.objectweb.util.trace.TraceSystem;
39
40 /**
41  * Util class.
42  *
43  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
44  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
45  *
46  * @version 0.1
47  */

48 public abstract class ExplorerUtils
49 {
50
51     //==================================================================
52
//
53
// Internal States.
54
//
55
// ==================================================================
56

57     // ==================================================================
58
//
59
// Constructors.
60
//
61
// ==================================================================
62

63     // ==================================================================
64
//
65
// No internal method.
66
//
67
// ==================================================================
68

69     /**
70      * Merges to array of objects.
71      * It adds the contents of the second Objects array into the first one.
72      * <ul>
73      * <li>If an element of the second array already exists into the first one, this element will not be added.<br>
74      * <code>{1,2,3} + {1,3,4} => {1,2,3,4}</code>
75      * </li>
76      * <li>If the two parameters are <code>null</code>, an empty array is returned.<br>
77      * <code>null + null => {}</code>
78      * </li>
79      * <ul>
80      * @param a1 The first objects arrays.
81      * @param a2 The second objects arrays.
82      * @return An array containing the content of both the first array and the second one.
83      */

84     protected static Object JavaDoc[] merge(Object JavaDoc[] a1, Object JavaDoc[] a2){
85         List JavaDoc l = (a1==null ? new Vector JavaDoc() : Arrays.asList(a1));
86         if(a2!=null && a2.length>0){
87             l = new Vector JavaDoc(l);
88             for(int i=0 ; i<a2.length ; i++){
89                 if(!l.contains(a2[i])){
90                     l.add(a2[i]);
91                 }
92             }
93         }
94         return l.toArray();
95     }
96     
97     
98     // ==================================================================
99
//
100
// Public methods.
101
//
102
// ==================================================================
103

104     /**
105      * Merges to array of objects.
106      * It adds the contents of the second Objects array into the first one.
107      * <ul>
108      * <li>If an element of the second array already exists into the first one, this element will not be added.<br>
109      * <code>{1,2,3} + {1,3,4} => {1,2,3,4}</code>
110      * </li>
111      * <li>If the two parameters are <code>null</code>, an empty array is returned.<br>
112      * <code>null + null => {}</code>
113      * </li>
114      * <ul>
115      * @param a1 The first objects arrays.
116      * @param a2 The second objects arrays.
117      * @return An array containing the content of both the first array and the second one.
118      */

119     public static Object JavaDoc[] mergeArrays(Object JavaDoc[] a1, Object JavaDoc[] a2){
120         return merge(a1,a2);
121     }
122
123     /**
124      * Merges to array of objects.
125      * @see java.lang.Object[]#mergeArrays(java.lang.Object[],java.lang.Object[])
126      */

127     public static String JavaDoc[] mergeArrays(String JavaDoc[] a1, String JavaDoc[] a2){
128         Object JavaDoc[] result = merge(a1, a2);
129         return (String JavaDoc[])(Arrays.asList(result)).toArray(new String JavaDoc[result.length]);
130     }
131     
132     /**
133      * Provides a String representation of an array.<br>
134      * <code>i.e. "{[elt1],[elt2],...}"</code>
135      */

136     public static String JavaDoc arrayToString(Object JavaDoc[] m){
137         if(m!=null){
138             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
139             sb.append("{");
140             for(int i=0 ; i<m.length ; i++){
141                 sb.append(m[i]);
142                 if(i<m.length-1){
143                     sb.append(",");
144                 }
145             }
146             sb.append("}");
147             return sb.toString();
148         }
149         return "null";
150     }
151
152     /**
153      * Two objects are considered as equals if they have the same value
154      * (call to <code>equals()</code> method).
155      * If both parameters are null, they are considered as equals.
156      * @param o1 The first object to test
157      * @param o2 The second object to test
158      * @return true if the values are equals.
159      */

160     public static boolean compareObjects(Object JavaDoc o1, Object JavaDoc o2){
161         if(o1==null && o2==null)
162             return true;
163         if(o1!=null)
164             return o1.equals(o2);
165         return false;
166     }
167     
168     /**
169      * Provides the hashCode of an Object, or 0 if the given object is null.
170      * @return The hashCode of an object.
171      * @see java.lang.Object#hashCode()
172      */

173     public static int getHashCode(Object JavaDoc o){
174         if(o!=null)
175             return o.hashCode();
176         return 0;
177     }
178     
179     /**
180      * Returns a String representation of an Object.
181      */

182     public static String JavaDoc toString(Object JavaDoc o){
183         return (o==null)?"null":o.toString();
184     }
185  
186     /**
187      * Indicates if the given value is empty.
188      * i.e.: <code>value==null || value.equals("")</code>
189      * @param value The String to check.
190      * @return true if the given value is empty.
191      */

192     public static boolean isEmpty(String JavaDoc value){
193         return value==null || value.equals("");
194     }
195     
196     /**
197      * Tries to locate the resource pointed out by the given resource location.
198      * <ul>
199      * <li>1. First, it looks for the resource in the current class loader.</li>
200      * <li>2. Then, it tries to load the resource as an URL.</li>
201      * </ul>
202      * @param url The location of the resource.
203      * @return The located resource (or null).
204      */

205     public static URL JavaDoc getURL(String JavaDoc url){
206         try {
207             URL JavaDoc theURL = ClassResolver.getResource(url);
208             if(theURL==null){
209                 theURL = new URL JavaDoc(url);
210             }
211             return theURL;
212         } catch (MalformedURLException JavaDoc e) {
213             TraceSystem.get("explorer").info(url + ": Resource not found!");
214         }
215         return null;
216     }
217 }
Popular Tags