1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.settings;
21
22 import java.beans.PropertyChangeListener;
23 import java.util.Arrays;
24
25 /***
26 * Provides a default implementation of a setting.
27 */
28 public abstract class AbstractSetting<T> implements Setting<T> {
29
30 public static final String ARRAY_SEPARATOR = ";";
31
32 protected SettingResource backstore;
33 private String key;
34 private Validator validator;
35 private T defaultValue;
36
37 public AbstractSetting(SettingResource backstore, String key, T defaultValue, Validator validator)
38 {
39 this.backstore = backstore;
40 this.key = key;
41 this.defaultValue = defaultValue;
42 this.validator = validator;
43 }
44
45 /***
46 * Adds a preferences listener.
47 */
48 public synchronized void addPropertyChangeListener(PropertyChangeListener l)
49 {
50 backstore.addPropertyChangeListener(getKey(), l);
51 }
52
53 /***
54 * Determine if 2 objects are equal, or both point to null.
55 */
56 public static boolean areObjectsEqual(Object obj1, Object obj2)
57 {
58 return (obj1 != null) ? obj1.equals(obj2) : (obj1 == obj2);
59 }
60
61 /***
62 * Determine if 2 arrays are equal, or both point to null.
63 */
64 public static boolean areObjectsEqual(Object[] obj1, Object[] obj2)
65 {
66 return (obj1 != null) ? obj2 == null : Arrays.equals(obj1, obj2);
67 }
68
69 /***
70 * Sets the value of the setting from s.
71 */
72 protected abstract T fromString(String s);
73
74 public String getKey()
75 {
76 return key;
77 }
78
79 public T getDefaultValue()
80 {
81 return defaultValue;
82 }
83
84 public SettingResource getProperties()
85 {
86 return backstore;
87 }
88
89 public T getValue()
90 {
91 T value = null;
92 String s = backstore.get(getKey(), null);
93 if (s != null) {
94 value = fromString(s);
95 }
96 return (value != null) ? value : defaultValue;
97 }
98
99 public Validator getValidator()
100 {
101 return validator;
102 }
103
104 public void removePropertyChangeListener(PropertyChangeListener l)
105 {
106 backstore.removePropertyChangeListener(getKey(), l);
107 }
108
109 public void revert()
110 {
111 setValue(defaultValue);
112 }
113
114 protected void setDefaultValue(T defaultValue)
115 {
116 this.defaultValue = defaultValue;
117 }
118
119 public void setProperties(PropertyResource properties)
120 {
121 this.backstore = properties;
122 }
123
124 public void setValue(T newValue)
125 {
126 T oldValue = getValue();
127 if (!areObjectsEqual(newValue, oldValue)) {
128 if (newValue == null || areObjectsEqual(newValue, defaultValue)) {
129
130 backstore.remove(getKey());
131 }
132 else {
133 String stringValue = toString(newValue);
134 if (validator != null) {
135 validator.validate(stringValue);
136 }
137 backstore.put(getKey(), stringValue);
138 }
139
140
141 backstore.firePropertyChange(getKey(), oldValue, newValue);
142 }
143 }
144
145 public void setValidator(Validator validator)
146 {
147 this.validator = validator;
148 }
149
150 /***
151 * Returns a string representation of the setting. The string is
152 * written to the settings file.
153 *
154 * @see #fromString(String)
155 */
156 protected abstract String toString(T object);
157
158 }