1 /** 2 * The utillib library. 3 * More information is available at http://www.jinchess.com/. 4 * Copyright (C) 2002, 2003 Alexander Maryanovsky. 5 * All rights reserved. 6 * 7 * The utillib library is free software; you can redistribute 8 * it and/or modify it under the terms of the GNU Lesser General Public License 9 * as published by the Free Software Foundation; either version 2 of the 10 * License, or (at your option) any later version. 11 * 12 * The utillib library is distributed in the hope that it will 13 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with utillib library; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA 20 */ 21 22 package free.util; 23 24 import java.util.Hashtable; 25 import java.util.Enumeration; 26 import java.util.NoSuchElementException; 27 28 29 /** 30 * A collection of general utility methods. 31 */ 32 33 public class Utilities{ 34 35 36 37 /** 38 * A 0-length Object array. 39 */ 40 41 public static final Object [] EMPTY_ARRAY = new Object[0]; 42 43 44 45 /** 46 * A 0-length long array. 47 */ 48 49 public static final long [] EMPTY_LONG_ARRAY = new long[0]; 50 51 52 53 /** 54 * A 0-length int array. 55 */ 56 57 public static final int [] EMPTY_INT_ARRAY = new int[0]; 58 59 60 61 /** 62 * A 0-length short array. 63 */ 64 65 public static final short [] EMPTY_SHORT_ARRAY = new short[0]; 66 67 68 69 /** 70 * A 0-length byte array. 71 */ 72 73 public static final byte [] EMPTY_BYTE_ARRAY = new byte[0]; 74 75 76 77 /** 78 * A 0-length char array. 79 */ 80 81 public static final char [] EMPTY_CHAR_ARRAY = new char[0]; 82 83 84 85 /** 86 * A 0-length double array. 87 */ 88 89 public static final double [] EMPTY_DOUBLE_ARRAY = new double[0]; 90 91 92 93 /** 94 * A 0-length float array. 95 */ 96 97 public static final float [] EMPTY_FLOAT_ARRAY = new float[0]; 98 99 100 101 /** 102 * A 0-length String array. 103 */ 104 105 public static final String [] EMPTY_STRING_ARRAY = new String[0]; 106 107 108 109 /** 110 * An empty enumeration. 111 */ 112 113 public static final Enumeration EMPTY_ENUM = new Enumeration(){ 114 public boolean hasMoreElements(){return false;} 115 public Object nextElement(){throw new NoSuchElementException();} 116 }; 117 118 119 120 /** 121 * Returns <code>true</code> if the two specified objects are the same. 122 * Returns <code>false</code> otherwise. To be considered the same, the two 123 * references must either both be null or invoking <code>equals</code> on one 124 * of them with the other must return <code>true</code>. 125 */ 126 areEqual(Object obj1, Object obj2)127 public static boolean areEqual(Object obj1, Object obj2){ 128 return (obj1 == obj2) || (obj1 == null ? false : obj1.equals(obj2)); 129 } 130 131 132 133 134 /** 135 * Maps the specified key to the specified value in the specified 136 * <code>Hashtable</code>. If the specified value is <code>null</code> any 137 * existing mapping of the specified key is removed from the 138 * <code>Hashtable</code>. The old value mapped to the specified key 139 * is returned, or <code>null</code> if no value was mapped to the key. 140 */ 141 put(Hashtable table, Object key, Object value)142 public static Object put(Hashtable table, Object key, Object value){ 143 return value == null ? table.remove(key) : table.put(key, value); 144 } 145 146 147 148 /** 149 * Returns <code>true</code> if the specified object is an element of the 150 * specified array. The specified array may not be <code>null</code>. The 151 * specified object may be <code>null</code>, in which case this method will 152 * return <code>true</code> iff one of the indices in the array is empty 153 * (contains <code>null</code>). 154 */ 155 contains(Object [] array, Object item)156 public static boolean contains(Object [] array, Object item){ 157 return (indexOf(array, item) != -1); 158 } 159 160 161 162 /** 163 * Returns the index of the first occurrance of specified object in the 164 * specified array, or -1 if the specified object is not an element of the 165 * specified array. The specified object may be <code>null</code> in which 166 * case the returned index will be the index of the first <code>null</code> 167 * in the array. 168 */ 169 indexOf(Object [] array, Object item)170 public static int indexOf(Object [] array, Object item){ 171 if (array == null) 172 throw new IllegalArgumentException("The specified array may not be null"); 173 174 for (int i = 0; i < array.length; i++) 175 if (areEqual(item, array[i])) 176 return i; 177 178 return -1; 179 } 180 181 182 183 184 /** 185 * Returns the index of the first occurrance of specified integer in the 186 * specified array, or -1 if the specified integer is not an element of the 187 * specified array. 188 */ 189 indexOf(int [] arr, int val)190 public static int indexOf(int [] arr, int val){ 191 if (arr == null) 192 throw new IllegalArgumentException("The specified array may not be null"); 193 194 for (int i = 0; i < arr.length; i++) 195 if (arr[i] == val) 196 return i; 197 198 return -1; 199 } 200 201 202 203 /** 204 * Converts the specified array into a string by appending all its elements 205 * separated by a semicolon. 206 */ 207 arrayToString(Object [] arr)208 public static String arrayToString(Object [] arr){ 209 StringBuffer buf = new StringBuffer(); 210 for (int i = 0; i < arr.length; i++){ 211 buf.append(arr[i]); 212 buf.append("; "); 213 } 214 if (arr.length > 0) 215 buf.setLength(buf.length() - 2); // get rid of the extra "; " 216 217 return buf.toString(); 218 } 219 220 221 222 223 /** 224 * Converts the specified <code>Hashtable</code> into a string by putting 225 * each key and value on a separate line (separated by '\n') and an arrow 226 * (" -> ") between them. 227 */ 228 hashtableToString(Hashtable hashtable)229 public static String hashtableToString(Hashtable hashtable){ 230 StringBuffer buf = new StringBuffer(); 231 Enumeration keys = hashtable.keys(); 232 while (keys.hasMoreElements()){ 233 Object key = keys.nextElement(); 234 Object value = hashtable.get(key); 235 buf.append(key.toString()); 236 buf.append(" -> "); 237 buf.append(value.toString()); 238 buf.append("\n"); 239 } 240 241 return buf.toString(); 242 } 243 244 245 246 247 /** 248 * Returns the maximum element in the specified integer array. 249 */ 250 max(int [] arr)251 public static int max(int [] arr){ 252 if (arr == null) 253 throw new IllegalArgumentException("The specified array may not be null"); 254 if (arr.length == 0) 255 throw new IllegalArgumentException("The specified array must have at least one element"); 256 257 int n = arr[0]; 258 for (int i = 1; i < arr.length; i++) 259 if (arr[i] > n) 260 n = arr[i]; 261 262 return n; 263 } 264 265 266 267 /** 268 * Returns a hash code for the specified double value. 269 */ 270 hashCode(double val)271 public static int hashCode(double val){ 272 return hashCode(Double.doubleToLongBits(val)); 273 } 274 275 276 277 /** 278 * Returns a hash code for the specified long value. 279 */ 280 hashCode(long val)281 public static int hashCode(long val){ 282 return (int)(val ^ (val >>> 32)); 283 } 284 285 286 287 /** 288 * Returns the name of the package of the specified class. 289 */ 290 getPackageName(Class c)291 public static String getPackageName(Class c){ 292 return getPackageName(c.getName()); 293 } 294 295 296 297 /** 298 * Returns the name of the package of the class with the specified (full) name. 299 */ 300 getPackageName(String className)301 public static String getPackageName(String className){ 302 int lastDotIndex = className.lastIndexOf("."); 303 return lastDotIndex == -1 ? "" : className.substring(0, lastDotIndex); 304 } 305 306 307 308 /** 309 * Returns the short name (excluding the package name) of the specified class. 310 */ 311 getClassName(Class c)312 public static String getClassName(Class c){ 313 return getClassName(c.getName()); 314 } 315 316 317 318 /** 319 * Returns the short name (excluding the package name) of the class with the 320 * specified fully qualified name. 321 */ 322 getClassName(String className)323 public static String getClassName(String className){ 324 int lastDotIndex = className.lastIndexOf("."); 325 return lastDotIndex == -1 ? className : className.substring(lastDotIndex + 1); 326 } 327 328 329 330 } 331