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