]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/IdentityPair.java
Re-implement URIStringUtils escape and unescape using Unicode
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / IdentityPair.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  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.databoard.util;
13
14 /**
15  * A generic Pair (2-tuple) structure for containing two object instances of
16  * chosen types. Hash and equals are based on identities.
17  * 
18  * Element order doesn't matter.
19  * 
20  * @param <T1> type of first element
21  * @param <T2> type of second element
22  */
23 public final class IdentityPair<T1, T2> {
24     public final T1 first;
25     public final T2 second;
26     private final int hash;
27
28     public static <T1, T2> IdentityPair<T1, T2> make(T1 t1, T2 t2) {
29         return new IdentityPair<T1, T2>(t1, t2);
30     }
31
32     public IdentityPair(T1 first, T2 second) {
33         //assert(first != null);
34         //assert(second != null);
35         this.first = first;
36         this.second = second;
37         this.hash = makeHash();
38     }
39     
40     @Override
41     public boolean equals(Object obj) {         
42         if (obj == null)
43             return false;
44         if (!(obj.getClass().equals(this.getClass())))
45             return false;
46         IdentityPair<?, ?> other = (IdentityPair<?, ?>) obj;
47         return ( (other.first == first) && (other.second == second) ) ||
48         ( (other.first == second) && (other.second == first) );
49     }
50     
51     @Override
52     public int hashCode() {
53         return hash;
54     }
55     
56     @Override
57     public String toString() {
58         return "<"+first+", "+second+">";
59     }
60     
61     private int makeHash() {
62         return (first == null ? 0 : System.identityHashCode(first)) + 
63                 (second == null ? 0 : System.identityHashCode(second));
64     }
65 }