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