1 4 package com.tc.util; 5 6 import org.apache.commons.lang.builder.HashCodeBuilder; 7 8 import java.io.Serializable ; 9 10 20 public class SettableValue implements Serializable { 21 22 private Object value; 23 private boolean isSet; 24 25 public SettableValue() { 26 this.value = null; 27 this.isSet = false; 28 } 29 30 public void set(Object value) { 31 this.value = value; 32 this.isSet = true; 33 } 34 35 public void unset() { 36 this.value = null; 37 this.isSet = false; 38 } 39 40 public boolean isSet() { 41 return this.isSet; 42 } 43 44 public Object value() { 45 return this.value; 46 } 47 48 51 public Object value(Object defaultValue) { 52 if (this.isSet) { 53 return this.value; 54 } else { 55 return defaultValue; 56 } 57 } 58 59 public boolean equals(Object that) { 60 if (that == this) return true; 61 if (that == null) return false; 62 if (! (that instanceof SettableValue)) return false; 63 64 SettableValue valueThat = (SettableValue) that; 65 if (this.isSet != valueThat.isSet) return false; 66 if ((this.value == null) != (valueThat.value == null)) return false; 67 if (this.value != null) return this.value.equals(valueThat.value); 68 else return true; 69 } 70 71 public int hashCode() { 72 return new HashCodeBuilder().append(isSet).append(value).toHashCode(); 73 } 74 75 public Object clone() { 76 SettableValue out = new SettableValue(); 77 if (this.isSet) out.set(this.value); 78 return out; 79 } 80 81 public String toString() { 82 if (! isSet) return "<unset>"; 83 if (value == null) return "<null>"; 84 return value.toString(); 85 } 86 87 } | Popular Tags |