/* ********************************************************************** * Copyright (c) 2002-2004, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Author: Mark Davis ********************************************************************** */ package org.unicode.cldr.util; import com.ibm.icu.impl.Utility; import com.ibm.icu.util.Freezable; public final class Pair, U extends Comparable> implements java.lang.Comparable>, Cloneable, Freezable { private T first; private U second; private boolean frozen; public static , U extends Comparable> Pair of(T arg0, U arg1) { return new Pair(arg0, arg1); } public static , U extends Comparable> Pair ofFrozen(T arg0, U arg1) { return of(arg0, arg1).freeze(); } public Pair setFirst(T first) { if (frozen) { throw new UnsupportedOperationException("Attempt to modify frozen object"); } this.first = first; return this; } public T getFirst() { return first; } public Pair setSecond(U second) { if (frozen) { throw new UnsupportedOperationException("Attempt to modify frozen object"); } this.second = second; return this; } public U getSecond() { return second; } public Pair set(Pair name) { setFirst(name.getFirst()); setSecond(name.getSecond()); return this; } public Pair(T first, U second) { this.first = first; this.second = second; } public Pair() { } public int hashCode() { return Utility.checkHash(first) * 37 + Utility.checkHash(second); } public boolean equals(Object other) { try { Pair that = (Pair) other; return Utility.objectEquals(first, that.first) && Utility.objectEquals(second, that.second); } catch (Exception e) { return false; } } public int compareTo(Pair that) { int trial = Utility.checkCompare(first, that.first); if (trial != 0) return trial; return Utility.checkCompare(second, that.second); } public Object clone() { if (frozen) return this; try { return super.clone(); } catch (CloneNotSupportedException e) { return null; } } public String toString() { return '(' + (first == null ? "null" : first.toString()) + ',' + (second == null ? "null" : second.toString()) + ')'; } public boolean isFrozen() { return frozen; } public Pair freeze() { frozen = true; return this; } public Object cloneAsThawed() { try { Pair result = (Pair) super.clone(); result.frozen = false; return result; } catch (CloneNotSupportedException e) { return null; } } }