1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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.databoard.util;
15 * A generic Pair (2-tuple) structure for containing two object instances of
16 * chosen types. Hash and equals are based on identities.
18 * Element order doesn't matter.
20 * @param <T1> type of first element
21 * @param <T2> type of second element
23 public final class IdentityPair<T1, T2> {
24 public final T1 first;
25 public final T2 second;
26 private final int hash;
28 public static <T1, T2> IdentityPair<T1, T2> make(T1 t1, T2 t2) {
29 return new IdentityPair<T1, T2>(t1, t2);
32 public IdentityPair(T1 first, T2 second) {
33 //assert(first != null);
34 //assert(second != null);
37 this.hash = makeHash();
41 public boolean equals(Object obj) {
44 if (!(obj.getClass().equals(this.getClass())))
46 IdentityPair<?, ?> other = (IdentityPair<?, ?>) obj;
47 return ( (other.first == first) && (other.second == second) ) ||
48 ( (other.first == second) && (other.second == first) );
52 public int hashCode() {
57 public String toString() {
58 return "<"+first+", "+second+">";
61 private int makeHash() {
62 return (first == null ? 0 : System.identityHashCode(first)) +
63 (second == null ? 0 : System.identityHashCode(second));