1 19 20 package org.openide.util; 21 22 import java.io.Serializable ; 23 24 33 public abstract class Union2<First,Second> implements Cloneable , Serializable { 34 35 private static final long serialVersionUID = 1L; 36 37 Union2() {} 38 39 44 public abstract First first() throws IllegalArgumentException ; 45 46 51 public abstract Second second() throws IllegalArgumentException ; 52 53 57 public abstract boolean hasFirst(); 58 59 63 public abstract boolean hasSecond(); 64 65 @Override 66 public abstract Union2<First,Second> clone(); 67 68 73 public static <First,Second> Union2<First,Second> createFirst(First first) { 74 return new Union2First<First,Second>(first); 75 } 76 77 82 public static <First,Second> Union2<First,Second> createSecond(Second second) { 83 return new Union2Second<First,Second>(second); 84 } 85 86 private static final class Union2First<First,Second> extends Union2<First,Second> { 87 88 private static final long serialVersionUID = 1L; 89 90 private final First first; 91 92 public Union2First(First first) { 93 this.first = first; 94 } 95 96 @Override 97 public First first() throws IllegalArgumentException { 98 return first; 99 } 100 101 @Override 102 public Second second() throws IllegalArgumentException { 103 throw new IllegalArgumentException (); 104 } 105 106 @Override 107 public boolean hasFirst() { 108 return true; 109 } 110 111 @Override 112 public boolean hasSecond() { 113 return false; 114 } 115 116 @Override 117 public String toString() { 118 return String.valueOf(first); 119 } 120 121 @Override 122 public boolean equals(Object obj) { 123 return (obj instanceof Union2First) && first.equals(((Union2First) obj).first); 124 } 125 126 @Override 127 public int hashCode() { 128 return first.hashCode(); 129 } 130 131 @Override 132 public Union2<First,Second> clone() { 133 return createFirst(first); 134 } 135 136 } 137 138 private static final class Union2Second<First,Second> extends Union2<First,Second> { 139 140 private static final long serialVersionUID = 1L; 141 142 private final Second second; 143 144 public Union2Second(Second second) { 145 this.second = second; 146 } 147 148 @Override 149 public First first() throws IllegalArgumentException { 150 throw new IllegalArgumentException (); 151 } 152 153 @Override 154 public Second second() throws IllegalArgumentException { 155 return second; 156 } 157 158 @Override 159 public boolean hasFirst() { 160 return false; 161 } 162 163 @Override 164 public boolean hasSecond() { 165 return true; 166 } 167 168 @Override 169 public String toString() { 170 return String.valueOf(second); 171 } 172 173 @Override 174 public boolean equals(Object obj) { 175 return (obj instanceof Union2Second) && second.equals(((Union2Second) obj).second); 176 } 177 178 @Override 179 public int hashCode() { 180 return second.hashCode(); 181 } 182 183 @Override 184 public Union2<First,Second> clone() { 185 return createSecond(second); 186 } 187 188 } 189 190 } 191 | Popular Tags |