Annotation of java/classes/org/w3c/util/ObservableProperties.java, revision 1.10
1.1 abaird 1: // ObservableProperties.java
1.10 ! ylafon 2: // $Id: ObservableProperties.java,v 1.9 2012/06/16 15:48:50 ylafon Exp $
1.1 abaird 3: // (c) COPYRIGHT MIT and INRIA, 1996.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
1.5 bmahe 6: package org.w3c.util;
1.1 abaird 7:
1.10 ! ylafon 8: import java.io.File;
1.8 ylafon 9: import java.util.Properties;
10: import java.util.StringTokenizer;
11:
1.1 abaird 12: /**
13: * An enhanced property class that provides support to monitor changes.
14: * This class extends the basic properties class of Java, by providing
15: * monitoring support. It also provides more type conversion.
1.9 ylafon 16: *
1.1 abaird 17: * @see PropertyMonitoring
18: */
19:
20: public class ObservableProperties extends Properties {
1.3 abaird 21: private static final boolean debug = false;
1.10 ! ylafon 22: private static final long serialVersionUID = 1527682518013288353L;
1.9 ylafon 23: PropertyMonitoring observers[] = null;
24: int observers_count = 0;
1.1 abaird 25:
26: /**
1.10 ! ylafon 27: * Build an httpdProperties instance from a Properties instance.
! 28: *
! 29: * @param props The Properties instance.
! 30: */
! 31:
! 32: public ObservableProperties(Properties props) {
! 33: super(props);
! 34: this.observers = new PropertyMonitoring[5];
! 35: this.observers_count = 0;
! 36: }
! 37:
! 38: /**
1.1 abaird 39: * Subscribe for property monitoring.
1.9 ylafon 40: *
41: * @param observer The object that handles the PropertyMonitoring
42: * interface.
43: */
44:
1.10 ! ylafon 45: public synchronized void registerObserver(PropertyMonitoring observer) {
1.9 ylafon 46: // Try looking for an empty slot:
47: for (int i = 0; i < observers.length; i++) {
48: if (observers[i] == null) {
1.10 ! ylafon 49: observers[i] = observer;
1.9 ylafon 50: return;
51: }
52: }
53: // Add the observer to the registered oned, resizing array if needed
54: if (observers_count + 1 >= observers.length) {
55: PropertyMonitoring m[] = new PropertyMonitoring[observers.length * 2];
56: System.arraycopy(observers, 0, m, 0, observers.length);
57: observers = m;
58: }
1.10 ! ylafon 59: observers[observers_count++] = observer;
1.1 abaird 60: }
61:
62: /**
63: * Unsubscribe this object from the observers list.
1.9 ylafon 64: *
1.1 abaird 65: * @param observer The observer to unsubscribe.
1.9 ylafon 66: * @return A boolean <strong>true</strong> if object was succesfully
67: * unsubscribed, <strong>false</strong> otherwise.
1.1 abaird 68: */
69:
1.10 ! ylafon 70: public synchronized boolean unregisterObserver(PropertyMonitoring observer) {
1.9 ylafon 71: for (int i = 0; i < observers.length; i++) {
1.10 ! ylafon 72: if (observers[i] == observer) {
1.9 ylafon 73: observers[i] = null;
74: return true;
75: }
76: }
77: return false;
1.1 abaird 78: }
1.9 ylafon 79:
1.1 abaird 80: /**
81: * Update a property value.
82: * Assign a value to a property. If the property value has really changed
83: * notify our observers of the change.
1.9 ylafon 84: *
85: * @param name The name of the property to assign.
1.1 abaird 86: * @param value The new value for this property, or <strong>null</strong>
1.9 ylafon 87: * if the property setting is to be cancelled.
88: * @return A boolean <strong>true</strong> if change was accepted by
89: * our observers, <strong>false</strong> otherwise.
90: */
91:
92: public synchronized boolean putValue(String name, String value) {
93: if (debug) {
94: System.out.println("ObservableProperties: put " + name + "=[" + value
95: + "]");
96: }
97: // If null value, remove the prop definition:
98: if (value == null) {
99: super.remove(name);
100: return true;
101: }
102: // Otherwise, proceed:
103: String old = (String) get(name);
104: if ((old == null) || (!old.equals(value))) {
105: super.put(name, value);
106: for (PropertyMonitoring observer : observers) {
107: if (debug) {
108: if (observer != null) {
109: System.out.println("ObservableProperties: notifies "
110: + observer);
111: }
112: }
113: if (observer != null && !observer.propertyChanged(name)) {
114: if (old != null) {
115: super.put(name, old);
116: }
117: return false;
118: }
119: }
120: }
121: return true;
1.1 abaird 122: }
1.8 ylafon 123:
1.1 abaird 124: /**
125: * Get this property value, as a boolean.
1.9 ylafon 126: *
1.1 abaird 127: * @param name The name of the property to be fetched.
1.9 ylafon 128: * @param def The default value, if the property isn't defined.
1.1 abaird 129: * @return A Boolean instance.
130: */
131:
132: public boolean getBoolean(String name, boolean def) {
1.9 ylafon 133: String v = getProperty(name, null);
1.10 ! ylafon 134: if (v != null) {
1.9 ylafon 135: return Boolean.parseBoolean(v);
136: }
137: return def;
1.1 abaird 138: }
139:
140: /**
141: * Get this property value, as a String.
1.9 ylafon 142: *
1.1 abaird 143: * @param name The name of the property to be fetched.
1.9 ylafon 144: * @param def The default value, if the property isn't defined.
1.1 abaird 145: * @return An instance of String.
146: */
147:
148: public String getString(String name, String def) {
1.9 ylafon 149: String v = getProperty(name, null);
150: return (v == null) ? def : v;
1.1 abaird 151: }
152:
153: /**
154: * Get this property as a String array.
155: * By convention, properties that are get as string arrays should be
156: * encoded as a <strong>|</strong> separated list of Strings.
1.9 ylafon 157: *
1.1 abaird 158: * @param name The property's name.
1.9 ylafon 159: * @param def The default value (if undefined).
1.1 abaird 160: * @return A String array, or <strong>null</strong> if the property
1.9 ylafon 161: * is undefined.
1.1 abaird 162: */
163:
164: public String[] getStringArray(String name, String def[]) {
1.9 ylafon 165: String v = getProperty(name, null);
166: if (v == null) {
167: return def;
168: }
169: // Parse the property value:
170: StringTokenizer st = new StringTokenizer(v, "|");
171: int len = st.countTokens();
172: String ret[] = new String[len];
173: for (int i = 0; i < ret.length; i++) {
174: ret[i] = st.nextToken();
175: }
176: return ret;
1.1 abaird 177: }
178:
179: /**
180: * Get this property value, as an integer.
1.9 ylafon 181: *
1.1 abaird 182: * @param name The name of the property to be fetched.
1.9 ylafon 183: * @param def The default value, if the property isn't defined.
1.2 abaird 184: * @return An integer value.
1.1 abaird 185: */
186:
187: public int getInteger(String name, int def) {
1.9 ylafon 188: String v = getProperty(name, null);
189: if (v != null) {
190: try {
191: if (v.startsWith("0x")) {
192: return Integer.valueOf(v.substring(2), 16).intValue();
193: }
194: if (v.startsWith("#")) {
195: return Integer.valueOf(v.substring(1), 16).intValue();
196: }
197: return Integer.valueOf(v).intValue();
198: } catch (NumberFormatException e) {
199: }
200: }
201: return def;
1.6 bmahe 202: }
203:
204: public long getLong(String name, long def) {
1.9 ylafon 205: String v = getProperty(name, null);
206: if (v != null) {
207: try {
208: return Long.valueOf(v).longValue();
209: } catch (NumberFormatException e) {
210: }
211: }
212: return def;
1.2 abaird 213: }
214:
215: /**
216: * Get this property value, as a double.
1.9 ylafon 217: *
1.2 abaird 218: * @param name The name of the property.
1.9 ylafon 219: * @param def The default value if undefined.
1.2 abaird 220: * @return A double value.
221: */
222:
223: public double getDouble(String name, double def) {
1.9 ylafon 224: String v = getProperty(name, null);
225: if (v != null) {
226: try {
227: return Double.valueOf(v).doubleValue();
228: } catch (NumberFormatException ex) {
229: }
230: }
231: return def;
1.1 abaird 232: }
233:
234: /**
235: * Get this property value, as a File.
1.9 ylafon 236: *
1.1 abaird 237: * @param name The name of the property to be fetched.
1.9 ylafon 238: * @param def The default value, if the property isn't defined.
1.1 abaird 239: * @return An instance of File.
240: */
241:
242: public File getFile(String name, File def) {
1.9 ylafon 243: String v = getProperty(name, null);
244: if (v != null)
245: return new File(v);
246: return def;
1.1 abaird 247: }
248:
249: }
250:
251:
Webmaster