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