1 52 53 package com.go.trove.util; 54 55 64 public class Pair implements Comparable , java.io.Serializable { 65 private final Object mObj1; 66 private final Object mObj2; 67 68 public Pair(Object obj1, Object obj2) { 69 mObj1 = obj1; 70 mObj2 = obj2; 71 } 72 73 public int compareTo(Object obj) { 74 if (this == obj) { 75 return 0; 76 } 77 78 Pair other = (Pair)obj; 79 80 Object a = mObj1; 81 Object b = other.mObj1; 82 83 firstTest: { 84 if (a == null) { 85 if (b != null) { 86 return 1; 87 } 88 break firstTest; 90 } 91 else { 92 if (b == null) { 93 return -1; 94 } 95 } 96 97 int result = ((Comparable )a).compareTo(b); 98 99 if (result != 0) { 100 return result; 101 } 102 } 103 104 a = mObj2; 105 b = other.mObj2; 106 107 if (a == null) { 108 if (b != null) { 109 return 1; 110 } 111 return 0; 113 } 114 else { 115 if (b == null) { 116 return -1; 117 } 118 } 119 120 return ((Comparable )a).compareTo(b); 121 } 122 123 public boolean equals(Object obj) { 124 if (this == obj) { 125 return true; 126 } 127 128 if (!(obj instanceof Pair)) { 129 return false; 130 } 131 132 Pair key = (Pair)obj; 133 134 return 135 (mObj1 == null ? 136 key.mObj1 == null : mObj1.equals(key.mObj1)) && 137 (mObj2 == null ? 138 key.mObj2 == null : mObj2.equals(key.mObj2)); 139 } 140 141 public int hashCode() { 142 return 143 (mObj1 == null ? 0 : mObj1.hashCode()) + 144 (mObj2 == null ? 0 : mObj2.hashCode()); 145 } 146 147 public String toString() { 148 return "[" + mObj1 + ':' + mObj2 + ']'; 149 } 150 } 151 | Popular Tags |