]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/Array.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/Array.java
new file mode 100644 (file)
index 0000000..8b1574e
--- /dev/null
@@ -0,0 +1,67 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ * 18.8.2006\r
+ */\r
+package org.simantics.utils.datastructures;\r
+\r
+import java.util.Arrays;\r
+\r
+/**\r
+ * Deep comparable array object.\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public final class Array<T> {\r
+\r
+    private final T[] array;\r
+    private final int hashCode;    \r
+    \r
+    public Array(T[] array)\r
+    {\r
+        this.array = array.clone();\r
+        this.hashCode = makeHashCode(this.array);\r
+    }\r
+    \r
+    private static <T> int makeHashCode(T[] array) {\r
+        int hash = 1;\r
+        for (int i=0; i<array.length; i++)\r
+        {\r
+            if (array[i]==null) continue;\r
+            hash = (31*hash) ^ array[i].hashCode();\r
+        }\r
+        return hash;\r
+    }\r
+    \r
+    public T[] toArray()\r
+    {\r
+        return array.clone();\r
+    }\r
+    \r
+    @Override\r
+    public int hashCode() {\r
+        return hashCode;\r
+    }\r
+    \r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if (obj==this) return true;\r
+        if (!obj.getClass().equals(getClass()))\r
+            return false;\r
+        return Arrays.equals(array, ((Array<?>)obj).array);\r
+    }\r
+    \r
+    public int size() {\r
+        return array.length;\r
+    }\r
+    \r
+}\r