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