1 /* 2 ********************************************************************** 3 * Copyright (c) 2002-2004, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ********************************************************************** 6 * Author: Mark Davis 7 ********************************************************************** 8 */ 9 10 package org.unicode.cldr.util; 11 12 import java.util.Objects; 13 14 import com.ibm.icu.impl.Utility; 15 import com.ibm.icu.util.Freezable; 16 17 public final class Pair<T extends Comparable<T>, U extends Comparable<U>> implements java.lang.Comparable<Pair<T, U>>, 18 Cloneable, Freezable<Object> { 19 20 private T first; 21 private U second; 22 private boolean frozen; 23 of(T arg0, U arg1)24 public static <T extends Comparable<T>, U extends Comparable<U>> Pair<T, U> of(T arg0, U arg1) { 25 return new Pair<T, U>(arg0, arg1); 26 } 27 ofFrozen(T arg0, U arg1)28 public static <T extends Comparable<T>, U extends Comparable<U>> Pair<T, U> ofFrozen(T arg0, U arg1) { 29 return of(arg0, arg1).freeze(); 30 } 31 setFirst(T first)32 public Pair<T, U> setFirst(T first) { 33 if (frozen) { 34 throw new UnsupportedOperationException("Attempt to modify frozen object"); 35 } 36 this.first = first; 37 return this; 38 } 39 getFirst()40 public T getFirst() { 41 return first; 42 } 43 setSecond(U second)44 public Pair<T, U> setSecond(U second) { 45 if (frozen) { 46 throw new UnsupportedOperationException("Attempt to modify frozen object"); 47 } 48 this.second = second; 49 return this; 50 } 51 getSecond()52 public U getSecond() { 53 return second; 54 } 55 set(Pair<T, U> name)56 public Pair<T, U> set(Pair<T, U> name) { 57 setFirst(name.getFirst()); 58 setSecond(name.getSecond()); 59 return this; 60 } 61 Pair(T first, U second)62 public Pair(T first, U second) { 63 this.first = first; 64 this.second = second; 65 } 66 Pair()67 public Pair() { 68 } 69 hashCode()70 public int hashCode() { 71 return Utility.checkHash(first) * 37 + Utility.checkHash(second); 72 } 73 equals(Object other)74 public boolean equals(Object other) { 75 try { 76 Pair<?, ?> that = (Pair<?, ?>) other; 77 return Objects.equals(first, that.first) && Objects.equals(second, that.second); 78 } catch (Exception e) { 79 return false; 80 } 81 } 82 compareTo(Pair<T, U> that)83 public int compareTo(Pair<T, U> that) { 84 int trial = Utility.checkCompare(first, that.first); 85 if (trial != 0) return trial; 86 return Utility.checkCompare(second, that.second); 87 } 88 clone()89 public Object clone() { 90 if (frozen) return this; 91 try { 92 return super.clone(); 93 } catch (CloneNotSupportedException e) { 94 return null; 95 } 96 } 97 toString()98 public String toString() { 99 return '(' + (first == null ? "null" : first.toString()) 100 + ',' + (second == null ? "null" : second.toString()) + ')'; 101 } 102 isFrozen()103 public boolean isFrozen() { 104 return frozen; 105 } 106 freeze()107 public Pair<T, U> freeze() { 108 frozen = true; 109 return this; 110 } 111 cloneAsThawed()112 public Object cloneAsThawed() { 113 try { 114 Pair<?, ?> result = (Pair<?, ?>) super.clone(); 115 result.frozen = false; 116 return result; 117 } catch (CloneNotSupportedException e) { 118 return null; 119 } 120 } 121 }