001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v1.util;
024:
025: import com.mchange.v2.lang.ObjectUtils;
026:
027: public final class ArrayUtils {
028: /**
029: * The array may contain nulls, but <TT>o</TT>
030: * must be non-null.
031: */
032: public static int indexOf(Object[] array, Object o) {
033: for (int i = 0, len = array.length; i < len; ++i)
034: if (o.equals(array[i]))
035: return i;
036: return -1;
037: }
038:
039: public static int identityIndexOf(Object[] array, Object o) {
040: for (int i = 0, len = array.length; i < len; ++i)
041: if (o == array[i])
042: return i;
043: return -1;
044: }
045:
046: public static boolean startsWith(byte[] checkMe, byte[] maybePrefix) {
047: int cm_len = checkMe.length;
048: int mp_len = maybePrefix.length;
049: if (cm_len < mp_len)
050: return false;
051: for (int i = 0; i < mp_len; ++i)
052: if (checkMe[i] != maybePrefix[i])
053: return false;
054: return true;
055: }
056:
057: /**
058: * returns a hash-code for an array consistent with Arrays.equals( ... )
059: */
060: public static int hashArray(Object[] oo) {
061: int len = oo.length;
062: int out = len;
063: for (int i = 0; i < len; ++i) {
064: //we rotate the bits of the element hashes
065: //around so that the hash has some loaction
066: //dependency
067: int elem_hash = ObjectUtils.hashOrZero(oo[i]);
068: int rot = i % 32;
069: int rot_hash = elem_hash >>> rot;
070: rot_hash |= elem_hash << (32 - rot);
071: out ^= rot_hash;
072: }
073: return out;
074: }
075:
076: /**
077: * returns a hash-code for an array consistent with Arrays.equals( ... )
078: */
079: public static int hashArray(int[] ii) {
080: int len = ii.length;
081: int out = len;
082: for (int i = 0; i < len; ++i) {
083: //we rotate the bits of the element hashes
084: //around so that the hash has some loaction
085: //dependency
086: int elem_hash = ii[i];
087: int rot = i % 32;
088: int rot_hash = elem_hash >>> rot;
089: rot_hash |= elem_hash << (32 - rot);
090: out ^= rot_hash;
091: }
092: return out;
093: }
094:
095: public static int hashOrZeroArray(Object[] oo) {
096: return (oo == null ? 0 : hashArray(oo));
097: }
098:
099: public static int hashOrZeroArray(int[] ii) {
100: return (ii == null ? 0 : hashArray(ii));
101: }
102:
103: /**
104: * @deprecated use the various toString(T[] methods)
105: */
106: public static String stringifyContents(Object[] array) {
107: StringBuffer sb = new StringBuffer();
108: sb.append("[ ");
109: for (int i = 0, len = array.length; i < len; ++i) {
110: if (i != 0)
111: sb.append(", ");
112: sb.append(array[i].toString());
113: }
114: sb.append(" ]");
115: return sb.toString();
116: }
117:
118: //these methods are obsoleted by Arrays.toString() in jdk1.5, but
119: //for libs that support older VMs...
120: private static String toString(String[] strings, int guessed_len) {
121: StringBuffer sb = new StringBuffer(guessed_len);
122: boolean first = true;
123: sb.append('[');
124: for (int i = 0, len = strings.length; i < len; ++i) {
125: if (first)
126: first = false;
127: else
128: sb.append(',');
129: sb.append(strings[i]);
130: }
131: sb.append(']');
132: return sb.toString();
133: }
134:
135: public static String toString(boolean[] arr) {
136: String[] strings = new String[arr.length];
137: int chars = 0;
138: for (int i = 0, len = arr.length; i < len; ++i) {
139: String str = String.valueOf(arr[i]);
140: chars += str.length();
141: strings[i] = str;
142: }
143: return toString(strings, chars + arr.length + 1);
144: }
145:
146: public static String toString(byte[] arr) {
147: String[] strings = new String[arr.length];
148: int chars = 0;
149: for (int i = 0, len = arr.length; i < len; ++i) {
150: String str = String.valueOf(arr[i]);
151: chars += str.length();
152: strings[i] = str;
153: }
154: return toString(strings, chars + arr.length + 1);
155: }
156:
157: public static String toString(char[] arr) {
158: String[] strings = new String[arr.length];
159: int chars = 0;
160: for (int i = 0, len = arr.length; i < len; ++i) {
161: String str = String.valueOf(arr[i]);
162: chars += str.length();
163: strings[i] = str;
164: }
165: return toString(strings, chars + arr.length + 1);
166: }
167:
168: public static String toString(short[] arr) {
169: String[] strings = new String[arr.length];
170: int chars = 0;
171: for (int i = 0, len = arr.length; i < len; ++i) {
172: String str = String.valueOf(arr[i]);
173: chars += str.length();
174: strings[i] = str;
175: }
176: return toString(strings, chars + arr.length + 1);
177: }
178:
179: public static String toString(int[] arr) {
180: String[] strings = new String[arr.length];
181: int chars = 0;
182: for (int i = 0, len = arr.length; i < len; ++i) {
183: String str = String.valueOf(arr[i]);
184: chars += str.length();
185: strings[i] = str;
186: }
187: return toString(strings, chars + arr.length + 1);
188: }
189:
190: public static String toString(long[] arr) {
191: String[] strings = new String[arr.length];
192: int chars = 0;
193: for (int i = 0, len = arr.length; i < len; ++i) {
194: String str = String.valueOf(arr[i]);
195: chars += str.length();
196: strings[i] = str;
197: }
198: return toString(strings, chars + arr.length + 1);
199: }
200:
201: public static String toString(float[] arr) {
202: String[] strings = new String[arr.length];
203: int chars = 0;
204: for (int i = 0, len = arr.length; i < len; ++i) {
205: String str = String.valueOf(arr[i]);
206: chars += str.length();
207: strings[i] = str;
208: }
209: return toString(strings, chars + arr.length + 1);
210: }
211:
212: public static String toString(double[] arr) {
213: String[] strings = new String[arr.length];
214: int chars = 0;
215: for (int i = 0, len = arr.length; i < len; ++i) {
216: String str = String.valueOf(arr[i]);
217: chars += str.length();
218: strings[i] = str;
219: }
220: return toString(strings, chars + arr.length + 1);
221: }
222:
223: public static String toString(Object[] arr) {
224: String[] strings = new String[arr.length];
225: int chars = 0;
226: for (int i = 0, len = arr.length; i < len; ++i) {
227: String str;
228: Object o = arr[i];
229: if (o instanceof Object[])
230: str = toString((Object[]) o);
231: else if (o instanceof double[])
232: str = toString((double[]) o);
233: else if (o instanceof float[])
234: str = toString((float[]) o);
235: else if (o instanceof long[])
236: str = toString((long[]) o);
237: else if (o instanceof int[])
238: str = toString((int[]) o);
239: else if (o instanceof short[])
240: str = toString((short[]) o);
241: else if (o instanceof char[])
242: str = toString((char[]) o);
243: else if (o instanceof byte[])
244: str = toString((byte[]) o);
245: else if (o instanceof boolean[])
246: str = toString((boolean[]) o);
247: else
248: str = String.valueOf(arr[i]);
249: chars += str.length();
250: strings[i] = str;
251: }
252: return toString(strings, chars + arr.length + 1);
253: }
254:
255: private ArrayUtils() {
256: }
257:
258: /*
259: public static void main(String[] argv)
260: {
261: int[] is = {1,2,3,4};
262: String[] ss = {"Hello", "There"};
263: Object[] os = {"Poop", is, ss, new Thread()};
264:
265: System.out.println( toString(is) );
266: System.out.println( toString(ss) );
267: System.out.println( toString(os) );
268: }
269: */
270: }
|