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