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