KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > util > Misc


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Mar 26, 2005
10  *
11  * Author Ben Yu
12  * ZBS
13  */

14 package jfun.util;
15
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.lang.reflect.Array JavaDoc;
22 import java.lang.reflect.Field JavaDoc;
23 import java.lang.reflect.Modifier JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 /**
29  * This utility class includes miscellenous utility functions.
30  * <p>
31  * Zephyr Business Solutions Corp.
32  *
33  * @author Ben Yu
34  *
35  */

36 public class Misc {
37   /**
38    * An empty array.
39    */

40   public static final Object JavaDoc[] array0 = new Object JavaDoc[0];
41   /**
42    * Get the array type of a Class object.
43    * getArrayType(int.class) returns int[].class.
44    * @param ctype the component type.
45    * @return the array type.
46    */

47   public static Class JavaDoc getArrayType(Class JavaDoc ctype){
48     return Array.newInstance(ctype, 0).getClass();
49   }
50
51   /**
52    * Get the human readable type name of a class.
53    * for array type such as int[], it returns "int[]"
54    * @param c the class object.
55    * @return the array type string.
56    */

57   public static String JavaDoc getTypeName(Class JavaDoc c){
58     if(c==null) return ""+c;
59     if(c.isArray()){
60       return getTypeName(c.getComponentType())+"[]";
61     }
62     else return c.getName();
63   }
64
65   /**
66    * If the type is a primitive type, get the corresponding wrapper type.
67    * @param t the type.
68    * @return the wrapper type if t is primitive, t is returned otherwise.
69    */

70   public static Class JavaDoc getWrapperType(Class JavaDoc t) {
71     final Object JavaDoc rt = primitives.get(t);
72     return (rt == null) ? t : (Class JavaDoc) rt;
73   }
74   /**
75    * Determines whether t2 is a wrapper type of t1.
76    * @param t1 the first type.
77    * @param t2 the second type.
78    * @return true if t1 is primitive type and t2 is the wrapper type.
79    */

80   public static boolean isPrimitiveOf(Class JavaDoc t1, Class JavaDoc t2) {
81     if (t1.isPrimitive()) {
82       return t2.equals(primitives.get(t1));
83     } else
84       return false;
85   }
86   private static final HashMap JavaDoc primitives = new HashMap JavaDoc();
87   static {
88     primitives.put(int.class, Integer JavaDoc.class);
89     primitives.put(long.class, Long JavaDoc.class);
90     primitives.put(short.class, Short JavaDoc.class);
91     primitives.put(byte.class, Byte JavaDoc.class);
92     primitives.put(char.class, Character JavaDoc.class);
93     primitives.put(boolean.class, Boolean JavaDoc.class);
94     primitives.put(double.class, Double JavaDoc.class);
95     primitives.put(float.class, Float JavaDoc.class);
96   }
97   /**
98    * Reads all the constants value and put them in a map.
99    * @param c the class.
100    * @return the result map.
101    */

102   public static Map JavaDoc readConstants(Class JavaDoc c){
103     final HashMap JavaDoc map = new HashMap JavaDoc();
104     if(!Modifier.isPublic(c.getModifiers())) return map;
105     final Field JavaDoc[] flds = c.getFields();
106     for(int i=0; i<flds.length; i++){
107       final Field JavaDoc fld = flds[i];
108       final int mod = fld.getModifiers();
109       if(Modifier.isStatic(mod) && Modifier.isFinal(mod)){
110         try{
111           map.put(fld.getName(), fld.get(null));
112         }
113         catch(IllegalAccessException JavaDoc e){
114           throw new IllegalStateException JavaDoc(e.getMessage());
115         }
116       }
117     }
118     return map;
119   }
120
121   /**
122    * Get the hashcode for an array of objects using value semantics.
123    * @param arr the array.
124    * @return the hashcode.
125    */

126   public static int getArrayHashcode(Object JavaDoc[] arr){
127     int r = 0;
128     for(int i=0; i<arr.length; i++){
129       r = r*31+hashcode(arr[i]);
130     }
131     return r;
132   }
133
134   /**
135    * Get the hashcode for an object. 0 is returned if obj is null.
136    * @param obj the object.
137    * @return the hashcode.
138    */

139   public static int hashcode(Object JavaDoc obj){
140     return obj==null?0:obj.hashCode();
141   }
142   /**
143    * Compares two objects. if o1==null, (o2==null) is returned.
144    * @param o1 the first object.
145    * @param o2 the second object.
146    * @return true if equal.
147    */

148   public static boolean equals(Object JavaDoc o1, Object JavaDoc o2){
149     return o1==null?o2==null:o1.equals(o2);
150   }
151   /**
152    * Get the File object for the real path designated by
153    * root/path when path is relative,
154    * or path when it is absolute or starts with a '/'
155    * @param root the root.
156    * @param path the path.
157    * @return the real File.
158    */

159   public static File JavaDoc getAbsolutePath(final File JavaDoc root,
160       File JavaDoc path)throws IOException JavaDoc{
161     if(root==null)
162       return path;
163     if(path.isAbsolute()){
164       return path;
165     }
166     else{
167       final String JavaDoc pathname = path.getPath();
168       if(path==null || pathname.length()==0)
169         return root;
170       final char c = pathname.charAt(0);
171       if(c == File.separatorChar)
172         return path;
173       return new File JavaDoc(root.getAbsolutePath()+File.separatorChar+
174       (c==File.separatorChar?pathname.substring(1):pathname))
175       .getCanonicalFile();
176     }
177   }
178
179   /**
180    * To read a property file from a class loader
181    * into a Properties object.
182    * @param loader the ClassLoader used to load resource.
183    * @param resource the resource name.
184    * @return the Properties object.
185    * @throws IOException when loading fails.
186    */

187   public static Properties JavaDoc loadResourceProperties(ClassLoader JavaDoc loader, String JavaDoc resource)
188   throws IOException JavaDoc{
189     final Properties JavaDoc props = new Properties JavaDoc();
190     final InputStream JavaDoc in = loader.getResourceAsStream(resource);
191     if(in==null){
192       throw new FileNotFoundException JavaDoc(resource);
193     }
194     try{
195       props.load(in);
196     }
197     finally{
198       try{
199         in.close();
200       }
201       catch(Exception JavaDoc e){}
202     }
203     return props;
204   }
205   /**
206    * To load properties from a properties file.
207    * @param file the properties file.
208    * @return the Properties object.
209    * @throws IOException when io error happens.
210    */

211   public static Properties JavaDoc loadPropertiesFile(File JavaDoc file)
212   throws IOException JavaDoc{
213     final Properties JavaDoc props = new Properties JavaDoc();
214     final InputStream JavaDoc in = new FileInputStream JavaDoc(file);
215     try{
216       props.load(in);
217     }
218     finally{
219       try{
220         in.close();
221       }
222       catch(Exception JavaDoc e){}
223     }
224     return props;
225   }
226   
227 }
228
Popular Tags