View Javadoc
1 package jsdsi; 2 3 import java.net.MalformedURLException; 4 import java.net.URI; 5 import java.net.URISyntaxException; 6 import java.net.URL; 7 8 /*** 9 * Static utility methods. 10 * 11 * @author Sameer Ajmani 12 * @author Sean Radford 13 * @version $Revision: 1.5.2.1 $ $Date: 2005/11/08 03:12:52 $ 14 */ 15 class Util { 16 /*** 17 * Returns <code>true</code> iff both parameters are 18 * <code>null</code> or they are equals(). 19 * 20 * @param a object to compare with <code>b</code>. 21 * @param b object to compare with <code>a</code>. 22 * @return <code>true</code> iff both parameters are <code>null</code> or 23 * they are equals(), <code>false</code> otherwise. 24 */ 25 static boolean equals(Object a, Object b) { 26 if (a == null && b == null) { 27 return true; 28 } 29 if (a == null || b == null) { 30 return false; 31 } 32 if ((a instanceof Object[]) && (b instanceof Object[])) { 33 return equals((Object[]) a, (Object[]) b); 34 } else if ((a instanceof byte[]) && (b instanceof byte[])) { 35 return equals((byte[])a, (byte[])b); 36 } 37 return a.equals(b); 38 } 39 40 /*** 41 * Returns <code>true</code> iff both parameters are <code>null</code> or 42 * if elements are equals(). 43 * 44 * @param a array of objects to compare with <code>b</code>. 45 * @param b array of objects to compare with <code>a</code>. 46 * @return <code>true</code> if both parameters are <code>null</code> or 47 * if elements are equals(), <code>false</code> otherwise. 48 */ 49 static boolean equals(Object[] a, Object[] b) { 50 if (a == null && b == null) { 51 return true; 52 } 53 if (a == null || b == null) { 54 return false; 55 } 56 if (a.length != b.length) { 57 return false; 58 } 59 for (int i = 0; i < a.length; i++) { 60 if (!equals(a[i], b[i])) { 61 return false; 62 } 63 } 64 return true; 65 } 66 67 /*** 68 * @param a array of bytes to compare with <code>b</code> 69 * @param b array of bytes to compare with <code>a</code> 70 * @return <code>true</code> if both parameters are 71 * <code>null</code> or if the elements are equal, 72 * <code>false</code> otherwise. 73 */ 74 static boolean equals(byte[] a, byte[] b) { 75 if (a == null && b == null) { 76 return true; 77 } 78 if (a == null || b == null) { 79 return false; 80 } 81 if (a.length != b.length) { 82 return false; 83 } 84 for (int i=0; i<a.length; i++) { 85 if (a[i] != b[i]) { 86 return false; 87 } 88 } 89 return true; 90 } 91 92 /*** 93 * Returns the hash code of an object (which also can be an object 94 * array). 95 * 96 * @param o object to return the hash-value for. 97 * @return the hash-value for <code>o</code>. 98 */ 99 static int hashCode(Object o) { 100 if (o == null) { 101 return 0; 102 } 103 if (o instanceof Object[]) { 104 return hashCode((Object[]) o); 105 } else if (o instanceof byte[]) { 106 return hashCode((byte[])o); 107 } 108 return o.hashCode(); 109 } 110 111 /*** 112 * Returns the hash-value for an array of objects. 113 * 114 * @param oa object array to create the hash-value from. 115 * @return the hash-value of <code>oa</code>. 116 */ 117 static int hashCode(Object[] oa) { 118 if (oa == null) { 119 return 0; 120 } 121 int hc = 0; 122 for (int i = 0; i < oa.length; i++) { 123 hc = hc ^ hashCode(oa[i]); 124 } 125 return hc; 126 } 127 128 /*** 129 * Returns the hash-value for an array of bytes. Unlike 130 * byte[].hashCode(), this will return a hash code based on the 131 * actual bytes within the array, and not the address. 132 * @param ba the byte array 133 * @return the hash code 134 */ 135 static int hashCode(byte[] ba) { 136 if (ba == null) { 137 return 0; 138 } 139 int hc = 0; 140 for (int i = 0; i < ba.length; i++) { 141 hc = hc ^ ba[i]; 142 } 143 return hc; 144 } 145 146 /*** 147 * Converts an array of java.net.URLs to an array of java.net.URIs 148 * TODO: Remove this when done removing <code>java.net.URL</code>s 149 * from {@link jsdsi.Obj}s. 150 * @param urls 151 * @return 152 */ 153 static URI[] convert(URL[] urls) { 154 if (urls == null) { 155 return null; 156 } 157 URI[] result = new URI[urls.length]; 158 for (int index=0; index<urls.length; index++) { 159 try { 160 result[index] = new URI( urls[index].toString() ); 161 } catch (URISyntaxException e) { 162 throw new RuntimeException(e); 163 } 164 } 165 return result; 166 } 167 168 /*** 169 * Converts an array of java.net.URIs to an array of java.net.URLs 170 * TODO: Remove this when done removing <code>java.net.URL</code>s 171 * from {@link jsdsi.Obj}s. 172 * @param uris 173 * @return 174 */ 175 static URL[] convert(URI[] uris) { 176 if (uris == null) { 177 return null; 178 } 179 URL[] result = new URL[uris.length]; 180 for (int index=0; index<uris.length; index++) { 181 try { 182 result[index] = new URL( uris[index].toString() ); 183 } catch (MalformedURLException e) { 184 throw new RuntimeException(e); 185 } 186 } 187 return result; 188 } 189 190 }

This page was automatically generated by Maven