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