Annotation of java/classes/org/w3c/util/ObservableProperties.java, revision 1.9

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

Webmaster