1 64 65 package com.jcorporate.expresso.core.dbobj; 66 67 import com.jcorporate.expresso.core.cache.Cacheable; 68 import com.jcorporate.expresso.core.misc.StringUtil; 69 70 71 99 public class ValidValue implements Cacheable, Cloneable { 100 101 protected String value = ""; 102 protected String description = ""; 103 104 108 public ValidValue() { 109 } 111 112 119 public ValidValue(String newValue, String newDescrip) { 120 setValue(newValue); 121 setDescription(newDescrip); 122 } 123 124 130 public String getValue() { 131 return value; 132 } 133 134 140 public void setValue(String newValue) { 141 this.value = StringUtil.notNull(newValue); 142 } 143 144 150 public String getKey() { 151 return getValue(); 152 } 153 154 159 public int hashCode() { 160 return this.value.hashCode(); 161 } 162 163 164 169 public String getDescription() { 170 return description; 171 } 172 173 179 public void setDescription(String newDescription) { 180 this.description = StringUtil.notNull(newDescription); 181 } 182 183 188 public String toString() { 189 StringBuffer buf = new StringBuffer (); 190 buf.append("ValidValue@" + Integer.toHexString(this.hashCode()) + "{"); 191 buf.append("value:`" + value + "' (`" + description + "')"); 192 buf.append("}"); 193 return buf.toString(); 194 } 195 196 206 protected Object clone() 207 throws CloneNotSupportedException { 208 ValidValue v = (ValidValue) super.clone(); 209 v.setDescription(this.getDescription()); 210 v.setValue(this.getValue()); 211 return v; 212 } 213 214 222 public boolean equals(Object obj) { 223 if (!(obj instanceof ValidValue)) { 224 return false; 225 } 226 227 ValidValue vv = (ValidValue) obj; 228 boolean returnVal = true; 229 230 returnVal &= (this.getValue().equals(vv.getValue())); 231 returnVal &= (this.getDescription().equals(vv.getDescription())); 232 return returnVal; 233 } 234 235 236 237 238 242 246 public static class ValueComparator 247 implements java.util.Comparator { 248 boolean reverse; 249 250 253 public ValueComparator() { 254 this(false); 255 } 256 257 262 public ValueComparator(boolean reverse) { 263 this.reverse = reverse; 264 } 265 266 299 public int compare(Object o1, Object o2) { 300 ValidValue ref1 = (ValidValue) o1; 301 ValidValue ref2 = (ValidValue) o2; 302 int result = ref2.getValue().compareTo(ref1.getValue()); 303 return result * (reverse ? 1 : -1); 304 } 305 306 307 314 public boolean equals(Object o) { 315 if (!(o instanceof ValueComparator)) { 316 return false; 317 } 318 ValueComparator ref = (ValueComparator) o; 319 return reverse == ref.reverse; 320 } 321 322 323 328 public String toString() { 329 StringBuffer buf = new StringBuffer (); 330 buf.append("{ ValueComparator "); 331 buf.append(reverse ? "ascending" : "descending"); 332 buf.append(" sort }"); 333 return buf.toString(); 334 } 335 } 336 337 338 342 public static class DescriptionComparator 343 implements java.util.Comparator { 344 boolean reverse; 345 346 349 public DescriptionComparator() { 350 this(false); 351 } 352 353 358 public DescriptionComparator(boolean reverse) { 359 this.reverse = reverse; 360 } 361 362 395 public int compare(Object o1, Object o2) { 396 ValidValue ref1 = (ValidValue) o1; 397 ValidValue ref2 = (ValidValue) o2; 398 int result = ref2.getDescription().compareTo(ref1.getDescription()); 399 return result * (reverse ? 1 : -1); 400 } 401 402 408 public boolean equals(Object o) { 409 if (!(o instanceof DescriptionComparator)) { 410 return false; 411 } 412 DescriptionComparator ref = (DescriptionComparator) o; 413 return reverse == ref.reverse; 414 } 415 416 421 public String toString() { 422 StringBuffer buf = new StringBuffer (); 423 buf.append("{ DescriptionComparator "); 424 buf.append(reverse ? "ascending" : "descending"); 425 buf.append(" sort }"); 426 return buf.toString(); 427 } 428 } 429 430 431 } 432 433 434 | Popular Tags |