]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/ComparablePair.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / ComparablePair.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.datastructures;\r
13 \r
14 import org.simantics.utils.strings.AlphanumComparator;\r
15 \r
16 \r
17 /**\r
18  * A generic Pair (2-tuple) structure for containing two object instances of\r
19  * chosen types.\r
20  * \r
21  * @param <T1> type of first element\r
22  * @param <T2> type of second element\r
23  */\r
24 public final class ComparablePair<T1 extends Comparable<T1>, T2 extends Comparable<T2>> implements Comparable<ComparablePair<T1, T2>> {\r
25 \r
26         public final T1 first;\r
27     public final T2 second;\r
28     private final int hash;\r
29 \r
30     public static <T1 extends Comparable<T1>, T2 extends Comparable<T2>> ComparablePair<T1, T2> make(T1 t1, T2 t2) {\r
31         return new ComparablePair<T1, T2>(t1, t2);\r
32     }\r
33 \r
34     public ComparablePair(T1 first, T2 second) {\r
35         if(first == null) throw new IllegalArgumentException("ComparablePair does not accept null values");\r
36         if(second == null) throw new IllegalArgumentException("ComparablePair does not accept null values");\r
37         this.first = first;\r
38         this.second = second;\r
39         this.hash = makeHash();\r
40     }\r
41     \r
42     @Override\r
43     public boolean equals(Object obj) {\r
44         if (obj == null)\r
45             return false;\r
46         if (!(obj.getClass().equals(this.getClass())))\r
47             return false;\r
48         ComparablePair<?, ?> other = (ComparablePair<?, ?>) obj;\r
49         if (other.first != first && (other.first == null || !other.first.equals(first)))\r
50             return false;\r
51         if (other.second != second && (other.second == null || !other.second.equals(second)))\r
52             return false;\r
53         return true;\r
54     }\r
55     \r
56     @Override\r
57     public int hashCode() {\r
58         return hash;\r
59     }\r
60     \r
61     @Override\r
62     public String toString() {\r
63         return "<"+first+", "+second+">";\r
64     }\r
65     \r
66     private int makeHash() {\r
67         return (first == null ? 0 : first.hashCode()) + (second == null ? 0 : second.hashCode())*31;\r
68     }\r
69 \r
70         @Override\r
71         public int compareTo(ComparablePair<T1, T2> arg0) {\r
72                 int firstCompare = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(first, arg0.first);\r
73                 if(firstCompare != 0) return firstCompare;\r
74                 else return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(second, arg0.second);\r
75         }\r
76         \r
77 }\r