]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/FirstIdentityPair.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / FirstIdentityPair.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 package org.simantics.utils.datastructures;\r
13 \r
14 \r
15 /**\r
16  * A generic Pair (2-tuple) structure for containing two object instances of\r
17  * chosen types.\r
18  * \r
19  * @param <T1> type of first element\r
20  * @param <T2> type of second element\r
21  */\r
22 public final class FirstIdentityPair<T1, T2> implements Comparable<FirstIdentityPair<T1, T2>> {\r
23     public final T1 first;\r
24     public final T2 second;\r
25     private final int hash;\r
26 \r
27     public static <T1, T2> FirstIdentityPair<T1, T2> make(T1 t1, T2 t2) {\r
28         return new FirstIdentityPair<T1, T2>(t1, t2);\r
29     }\r
30 \r
31     public FirstIdentityPair(T1 first, T2 second) {\r
32         //assert(first != null);\r
33         //assert(second != null);\r
34         \r
35         this.first = first;\r
36         this.second = second;\r
37         this.hash = makeHash();\r
38     }\r
39     \r
40     @Override\r
41     public boolean equals(Object obj) {\r
42         if (obj == null)\r
43             return false;\r
44         if (!(obj.getClass().equals(this.getClass())))\r
45             return false;\r
46         FirstIdentityPair<?, ?> other = (FirstIdentityPair<?, ?>) obj;\r
47         if (other.first != first && (other.first == null || !other.first.equals(first)))\r
48             return false;\r
49         return true;\r
50     }\r
51     \r
52     @Override\r
53     public int hashCode() {\r
54         return hash;\r
55     }\r
56     \r
57     @Override\r
58     public String toString() {\r
59         return "<"+first+", "+second+">";\r
60     }\r
61     \r
62     private int makeHash() {\r
63         return (first == null ? 0 : first.hashCode());\r
64     }\r
65 \r
66         @Override\r
67         public int compareTo(FirstIdentityPair<T1, T2> arg0) {\r
68                 return hash - arg0.hash;\r
69         }\r
70         \r
71 }\r