]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/Array.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / Array.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  * 18.8.2006\r
14  */\r
15 package org.simantics.utils.datastructures;\r
16 \r
17 import java.util.Arrays;\r
18 \r
19 /**\r
20  * Deep comparable array object.\r
21  * \r
22  * @author Toni Kalajainen\r
23  */\r
24 public final class Array<T> {\r
25 \r
26     private final T[] array;\r
27     private final int hashCode;    \r
28     \r
29     public Array(T[] array)\r
30     {\r
31         this.array = array.clone();\r
32         this.hashCode = makeHashCode(this.array);\r
33     }\r
34     \r
35     private static <T> int makeHashCode(T[] array) {\r
36         int hash = 1;\r
37         for (int i=0; i<array.length; i++)\r
38         {\r
39             if (array[i]==null) continue;\r
40             hash = (31*hash) ^ array[i].hashCode();\r
41         }\r
42         return hash;\r
43     }\r
44     \r
45     public T[] toArray()\r
46     {\r
47         return array.clone();\r
48     }\r
49     \r
50     @Override\r
51     public int hashCode() {\r
52         return hashCode;\r
53     }\r
54     \r
55     @Override\r
56     public boolean equals(Object obj) {\r
57         if (obj==this) return true;\r
58         if (!obj.getClass().equals(getClass()))\r
59             return false;\r
60         return Arrays.equals(array, ((Array<?>)obj).array);\r
61     }\r
62     \r
63     public int size() {\r
64         return array.length;\r
65     }\r
66     \r
67 }\r