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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.datastructures;
\r
14 import org.simantics.utils.strings.AlphanumComparator;
\r
18 * A generic Pair (2-tuple) structure for containing two object instances of
\r
21 * @param <T1> type of first element
\r
22 * @param <T2> type of second element
\r
24 public final class ComparablePair<T1 extends Comparable<T1>, T2 extends Comparable<T2>> implements Comparable<ComparablePair<T1, T2>> {
\r
26 public final T1 first;
\r
27 public final T2 second;
\r
28 private final int hash;
\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
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
38 this.second = second;
\r
39 this.hash = makeHash();
\r
43 public boolean equals(Object obj) {
\r
46 if (!(obj.getClass().equals(this.getClass())))
\r
48 ComparablePair<?, ?> other = (ComparablePair<?, ?>) obj;
\r
49 if (other.first != first && (other.first == null || !other.first.equals(first)))
\r
51 if (other.second != second && (other.second == null || !other.second.equals(second)))
\r
57 public int hashCode() {
\r
62 public String toString() {
\r
63 return "<"+first+", "+second+">";
\r
66 private int makeHash() {
\r
67 return (first == null ? 0 : first.hashCode()) + (second == null ? 0 : second.hashCode())*31;
\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