01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */package com.tc.util;
04:
05: import org.apache.commons.lang.builder.HashCodeBuilder;
06:
07: import java.io.Serializable;
08:
09: /**
10: * A simple value (of any Object type) that can be in three states, not just two:
11: * <ul>
12: * <li>Unset.</li>
13: * <li>Set, but null.</li>
14: * <li>Set, with a value.</li>
15: * </ul>
16: * This lets us handle a myriad of cases in the config where we need to distinguish between something having never
17: * been set and something having been set explicitly to null.
18: */
19: public class SettableValue implements Serializable {
20:
21: private Object value;
22: private boolean isSet;
23:
24: public SettableValue() {
25: this .value = null;
26: this .isSet = false;
27: }
28:
29: public void set(Object value) {
30: this .value = value;
31: this .isSet = true;
32: }
33:
34: public void unset() {
35: this .value = null;
36: this .isSet = false;
37: }
38:
39: public boolean isSet() {
40: return this .isSet;
41: }
42:
43: public Object value() {
44: return this .value;
45: }
46:
47: /**
48: * @returns defaultValue if value has not been set
49: */
50: public Object value(Object defaultValue) {
51: if (this .isSet) {
52: return this .value;
53: } else {
54: return defaultValue;
55: }
56: }
57:
58: public boolean equals(Object that) {
59: if (that == this )
60: return true;
61: if (that == null)
62: return false;
63: if (!(that instanceof SettableValue))
64: return false;
65:
66: SettableValue valueThat = (SettableValue) that;
67: if (this .isSet != valueThat.isSet)
68: return false;
69: if ((this .value == null) != (valueThat.value == null))
70: return false;
71: if (this .value != null)
72: return this .value.equals(valueThat.value);
73: else
74: return true;
75: }
76:
77: public int hashCode() {
78: return new HashCodeBuilder().append(isSet).append(value)
79: .toHashCode();
80: }
81:
82: public Object clone() {
83: SettableValue out = new SettableValue();
84: if (this .isSet)
85: out.set(this .value);
86: return out;
87: }
88:
89: public String toString() {
90: if (!isSet)
91: return "<unset>";
92: if (value == null)
93: return "<null>";
94: return value.toString();
95: }
96:
97: }
|