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