KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v1 > util > ArrayUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software 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
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v1.util;
25
26 import com.mchange.v2.lang.ObjectUtils;
27
28 public final class ArrayUtils
29 {
30     /**
31      * returns a hash-code for an array consistent with Arrays.equals( ... )
32      */

33     public static int hashArray(Object JavaDoc[] oo)
34     {
35     int len = oo.length;
36     int out = len;
37     for (int i = 0; i < len; ++i)
38         {
39         //we rotate the bits of the element hashes
40
//around so that the hash has some loaction
41
//dependency
42
int elem_hash = ObjectUtils.hashOrZero(oo[i]);
43         int rot = i % 32;
44         int rot_hash = elem_hash >>> rot;
45         rot_hash |= elem_hash << (32 - rot);
46         out ^= rot_hash;
47         }
48     return out;
49     }
50
51     /**
52      * returns a hash-code for an array consistent with Arrays.equals( ... )
53      */

54     public static int hashArray(int[] ii)
55     {
56     int len = ii.length;
57     int out = len;
58     for (int i = 0; i < len; ++i)
59         {
60         //we rotate the bits of the element hashes
61
//around so that the hash has some loaction
62
//dependency
63
int elem_hash = ii[i];
64         int rot = i % 32;
65         int rot_hash = elem_hash >>> rot;
66         rot_hash |= elem_hash << (32 - rot);
67         out ^= rot_hash;
68         }
69     return out;
70     }
71
72     public static int hashOrZeroArray(Object JavaDoc[] oo)
73     { return (oo == null ? 0 : hashArray(oo)); }
74
75     public static int hashOrZeroArray(int[] ii)
76     { return (ii == null ? 0 : hashArray(ii)); }
77
78     public static String JavaDoc stringifyContents(Object JavaDoc[] array)
79     {
80     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
81     sb.append("[ ");
82     for (int i = 0, len = array.length; i < len; ++i)
83         {
84         if (i != 0)
85             sb.append(", ");
86         sb.append( array[i].toString() );
87         }
88     sb.append(" ]");
89     return sb.toString();
90     }
91 }
92
93
Popular Tags