Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 469   Methods: 28
NCLOC: 208   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultCategory.java 70,6% 75% 85,7% 76%
coverage coverage
 1    package org.jconfig;
 2   
 3    import java.util.Properties;
 4    import java.util.StringTokenizer;
 5    import java.util.Vector;
 6   
 7    import javax.swing.event.EventListenerList;
 8   
 9    import org.jconfig.event.CategoryChangedEvent;
 10    import org.jconfig.event.CategoryListener;
 11    import org.jconfig.event.ConfigurationChangedEventImpl;
 12    import org.jconfig.event.PropertyChangedEvent;
 13    import org.jconfig.event.PropertyListener;
 14   
 15    /**
 16    * This class is the result of countless discussions of what
 17    * a Category should be. After determing that it should be
 18    * another class, and that we can do something useful, we
 19    * created this Category.
 20    *
 21    * A Category is a logical extension of the Configuration
 22    * has Categories has Properties has Value frame-of-mind.
 23    * The primary goal is to reduce redundancy in source code
 24    * when using setter and getters that include Category
 25    * information.
 26    *
 27    * @since 2.2
 28    * @author Andreas Mecky <andreas.mecky@xcom.de>
 29    * @author Terry Dye <terry.dye@xcom.de>
 30    *
 31    * Changes: The helper methods for the primitives did not replaced variables
 32    * @author Lubos Pochman lubosp@myway.com
 33    */
 34    public class DefaultCategory implements Category {
 35   
 36    private static final VariableManager vm = VariableManager.getInstance();
 37    private String extendsCategory;
 38    private String name = null;
 39    // The Properties object. Stink normal Properties file.
 40    // Perhaps I'll extend the Properties Object one day
 41    // when I write the generator to make nice type-safe
 42    // getter and setter methods.
 43    private Properties properties = new Properties();
 44    // The List of ConfigurationListeners
 45    private EventListenerList listenerList = new EventListenerList();
 46    // the name of the configuration that this category belongs to
 47    private String configurationName;
 48   
 49    /**
 50    * @since 2.2
 51    * @param categoryName
 52    */
 53  329 public DefaultCategory(String categoryName) {
 54  329 name = categoryName;
 55    }
 56   
 57    /**
 58    * Sets the property with the value given. This will override any
 59    * existing property values if the property name is duplicated.
 60    * This will create a new property if no property exists.
 61    *
 62    * Setting the value to &lt;null&gt; will remove the property
 63    * from the Category;
 64    *
 65    * @since 2.2
 66    * @param propertyName
 67    * @param propertyValue
 68    * @return
 69    */
 70  655 public Category setProperty(String propertyName, String propertyValue) {
 71  655 String temp = getProperty(propertyName);
 72  655 if( propertyValue == null ) {
 73  4 properties.remove(propertyName);
 74  4 firePropertyChangedEvent(
 75    new ConfigurationChangedEventImpl(
 76    PropertyChangedEvent.PROPERTY_REMOVED, this,
 77    propertyName, temp, propertyValue));
 78  4 return this;
 79    }
 80  651 properties.setProperty(propertyName, propertyValue);
 81  651 if(temp == null) {
 82  641 firePropertyChangedEvent(
 83    new ConfigurationChangedEventImpl(
 84    PropertyChangedEvent.PROPERTY_ADDED, this,
 85    propertyName, temp, propertyValue));
 86    }
 87    else {
 88  10 if (!temp.equals(propertyValue)) // thanks to Thorsten Jungbluth for the fix
 89  7 firePropertyChangedEvent(
 90    new ConfigurationChangedEventImpl(
 91    PropertyChangedEvent.PROPERTY_CHANGED, this,
 92    propertyName, temp, propertyValue));
 93    }
 94  651 return this;
 95    }
 96   
 97    /**
 98    * Searches for the property with the specified key in this property list.
 99    * If the key is not found in this property list, the default property list,
 100    * and its defaults, recursively, are then checked. The method returns
 101    * <code>null</code> if the property is not found.
 102    *
 103    * @since 2.2
 104    * @param key the property key.
 105    * @return the value in this property list with the specified key value.
 106    */
 107  825 public String getProperty(String propertyName) {
 108  825 String value = properties.getProperty(propertyName);
 109  825 if (value != null) {
 110  132 return vm.replaceVariables(value,getConfigurationName());
 111    } else {
 112  693 return value;
 113    }
 114    }
 115   
 116    /**
 117    * Searches for the property with the specified key in this property list.
 118    * If the key is not found in this property list, the default property list,
 119    * and its defaults, recursively, are then checked. The method returns the
 120    * default value argument if the property is not found.
 121    *
 122    * @since 2.2
 123    * @param propertyName The property name.
 124    * @param defaultValue The default value.
 125    * @return the value in this property list with the specified key value or
 126    * the defaultValue.
 127    */
 128  3 public String getProperty(String propertyName, String defaultValue) {
 129  3 String temp = properties.getProperty(propertyName, vm.replaceVariables(defaultValue,getConfigurationName()));
 130  3 if(temp != null) {
 131  3 return vm.replaceVariables(temp,getConfigurationName());
 132    }
 133  0 return properties.getProperty(propertyName, vm.replaceVariables(defaultValue,getConfigurationName()));
 134    }
 135   
 136    /**
 137    * Returns the name of this category.
 138    *
 139    * @since 2.2
 140    * @return The name of the category.
 141    */
 142  307 public String getCategoryName() {
 143  307 return name;
 144    }
 145   
 146    /**
 147    * Adds the specified category listener to receive category
 148    * changed events from this category.
 149    *
 150    * @since 2.2
 151    * @param listener The ConfigurationListener
 152    */
 153  264 public void addCategoryListener(CategoryListener listener) {
 154  264 listenerList.add(CategoryListener.class, listener);
 155    }
 156   
 157    /**
 158    * Removes the specified category listener from the category
 159    * change events from this category.
 160    *
 161    * @since 2.2
 162    * @param listener The ConfigurationListener
 163    */
 164  0 public void removeCategoryListener(CategoryListener listener) {
 165  0 listenerList.remove(CategoryListener.class, listener);
 166    }
 167   
 168    /**
 169    * Deliver category changed event to all listeners that are registered
 170    * with our listener list.
 171    *
 172    * @since 2.2
 173    * @param event The ConfigurationChangedEvent
 174    */
 175  917 public void fireCategoryChangedEvent(CategoryChangedEvent event) {
 176    // Guaranteed to return a non-null array
 177  917 Object[] listeners = listenerList.getListenerList();
 178    // Process the listeners last to first, notifying
 179    // those that are interested in this event
 180  917 for (int i = listeners.length - 2; i >= 0; i -= 2) {
 181  603 if (listeners[i] == CategoryListener.class) {
 182    // Lazily create the event:
 183  600 ((CategoryListener) listeners[i + 1]).categoryChanged(event);
 184    }
 185    }
 186   
 187    }
 188   
 189    /**
 190    * Return all properties related to this category
 191    *
 192    * @since 2.2
 193    * @return The Properties
 194    */
 195  37 public Properties getProperties() {
 196  37 return properties;
 197    }
 198   
 199  0 public String[] getArray(String key) {
 200  0 return getArray(key,null);
 201    }
 202   
 203  0 public String[] getArray(String key, String[] defaultValue) {
 204  0 String value = properties.getProperty(key);
 205  0 if ( value == null ) {
 206  0 return defaultValue;
 207    }
 208  0 Vector all = new Vector();
 209  0 StringTokenizer sto = new StringTokenizer(value,",");
 210  0 while ( sto.hasMoreElements() ) {
 211  0 String val = (String)sto.nextElement();
 212  0 val = vm.replaceVariables(val,configurationName);
 213  0 all.add(val);
 214    }
 215  0 return (String[])all.toArray(new String[0]);
 216    }
 217   
 218    /**
 219    * Helper method to allow for strong-binding within Properties.
 220    *
 221    * @since 2.2
 222    * @param name The name of the property
 223    * @param defaultValue The default value
 224    * @return If the String value can be converted to Boolean, the
 225    * value will be return, otherwise the the default value.
 226    */
 227  5 public boolean getBooleanProperty(String name, boolean defaultValue) {
 228  5 String value = properties.getProperty(name);
 229  5 if ( value == null ) {
 230  1 return defaultValue;
 231    } else {
 232  4 try {
 233  4 return Boolean.valueOf(vm.replaceVariables(value,getConfigurationName())).booleanValue();
 234    } catch (Exception e) {
 235  0 return defaultValue;
 236    }
 237    }
 238    }
 239   
 240    /**
 241    * Helper method to allow for strong-binding within Properties.
 242    *
 243    * @since 2.2
 244    * @param name
 245    * @param value
 246    */
 247  4 public void setBooleanProperty(String name, boolean value) {
 248  4 Boolean bool = new Boolean(value);
 249  4 setProperty(name, bool.toString());
 250    }
 251   
 252    /**
 253    * Helper method to allow for strong-binding within Properties.
 254    *
 255    * @since 2.2
 256    * @param name The name of the property
 257    * @param defaultValue The default value
 258    * @return If the String value can be converted to Boolean, the
 259    * value will be return, otherwise the the default value.
 260    */
 261  8 public char getCharProperty(String name, char defaultValue) {
 262  8 String value = properties.getProperty(name);
 263  8 if (value == null) {
 264  0 return defaultValue;
 265    } else {
 266  8 String subValue = vm.replaceVariables(value,getConfigurationName());
 267  8 if (subValue.length() == 1) {
 268  8 return subValue.charAt(0);
 269    } else {
 270  0 return defaultValue;
 271    }
 272    }
 273    }
 274   
 275    /**
 276    * Helper method to allow for strong-binding within Properties.
 277    *
 278    * @since 2.2
 279    * @param string
 280    * @param c
 281    */
 282  3 public void setCharProperty(String key, char value) {
 283  3 Character temp = new Character(value);
 284  3 setProperty(key, temp.toString());
 285    }
 286   
 287    /**
 288    * Helper method to allow for strong-binding within Properties.
 289    *
 290    * @since 2.2
 291    * @param name The name of the property
 292    * @param defaultValue The default value
 293    * @return If the String value can be converted to Boolean, the
 294    * value will be return, otherwise the the default value.
 295    */
 296  3 public double getDoubleProperty(String name, double defaultValue) {
 297  3 String value = properties.getProperty(name);
 298  3 if (value == null) {
 299  0 return defaultValue;
 300    } else {
 301  3 try {
 302  3 return Double.parseDouble(vm.replaceVariables(value,getConfigurationName()));
 303    } catch (Exception e) {
 304  0 return defaultValue;
 305    }
 306    }
 307   
 308    }
 309   
 310    /**
 311    * Helper method to allow for strong-binding within Properties.
 312    * <br/>
 313    * This method does not support any kind of inheritance! Use the
 314    * configuration.getIntProperty method instead
 315    *
 316    * @since 2.2
 317    * @param name The name of the property
 318    * @param defaultValue The default value
 319    * @return If the String value can be converted to Boolean, the
 320    * value will be return, otherwise the the default value.
 321    */
 322  4 public int getIntProperty(String name, int defaultValue) {
 323  4 String value = properties.getProperty(name);
 324  4 if (value == null) {
 325  0 return defaultValue;
 326    } else {
 327  4 try {
 328  4 return Integer.parseInt(vm.replaceVariables(value,getConfigurationName()));
 329    } catch (NumberFormatException nfe) {
 330  0 return defaultValue;
 331    }
 332    }
 333   
 334    }
 335   
 336    /**
 337    * Helper method to allow for strong-binding within Properties.
 338    *
 339    * @since 2.2
 340    * @param string
 341    * @param i
 342    */
 343  3 public void setIntProperty(String key, int value) {
 344  3 Integer temp = new Integer(value);
 345  3 setProperty(key, temp.toString());
 346    }
 347   
 348    /**
 349    * Helper method to allow for strong-binding within Properties.
 350    * <br/>
 351    * This method does not support any kind of inheritance! Use the
 352    * configuration.getIntProperty method instead
 353    *
 354    * @since 2.2
 355    * @param name The name of the property
 356    * @param defaultValue The default value
 357    * @return If the String value can be converted to Boolean, the
 358    * value will be return, otherwise the the default value.
 359    */
 360  3 public long getLongProperty(String name, long defaultValue) {
 361  3 String value = (String)properties.get(name);
 362  3 value = getProperty(name);
 363  3 if (value == null) {
 364  0 return defaultValue;
 365    } else {
 366  3 try {
 367  3 return Long.parseLong(vm.replaceVariables(value,getConfigurationName()));
 368    } catch (Exception e) {
 369  0 return defaultValue;
 370    }
 371    }
 372    }
 373   
 374    /**
 375    * Helper method to allow for strong-binding within Properties.
 376    *
 377    * @since 2.2
 378    * @param string
 379    * @param l
 380    */
 381  3 public void setLongProperty(String key, long value) {
 382  3 Long temp = new Long(value);
 383  3 setProperty(key, temp.toString());
 384    }
 385   
 386    /**
 387    * Getter for property configurationName.
 388    *
 389    * @since 2.2
 390    * @return Value of property configurationName.
 391    *
 392    */
 393  160 public String getConfigurationName() {
 394  160 return configurationName;
 395    }
 396   
 397    /**
 398    * Setter for property configurationName.
 399    *
 400    * @since 2.2
 401    * @param configurationName New value of property configurationName.
 402    *
 403    */
 404  301 public void setConfigurationName(String configurationName) {
 405  301 this.configurationName = configurationName;
 406    }
 407   
 408    /**
 409    * Helper method to allow for strong-binding within Properties.
 410    *
 411    * @since 2.2
 412    * @param key
 413    * @param value
 414    */
 415  3 public void setDoubleProperty(String key, double value) {
 416  3 Double temp = new Double(value);
 417  3 setProperty(key, temp.toString());
 418    }
 419   
 420    /**
 421    * Adds the given listener to the list that wish to receive the
 422    * {@link org.jconfig.event.PropertyChangedEvent PropertyChangedEvent}.
 423    *
 424    * @since 2.2
 425    * @param listener
 426    */
 427  1 public void addPropertyListener( PropertyListener listener ) {
 428  1 listenerList.add( PropertyListener.class, listener );
 429    }
 430   
 431    /**
 432    * Removes the given listener to the list that wish to receive the
 433    * {@link org.jconfig.event.PropertyChangedEvent PropertyChangedEvent}.
 434    *
 435    * @since 2.2
 436    * @param listener
 437    */
 438  0 public void removePropertyListener( PropertyListener listener ) {
 439  0 listenerList.remove( PropertyListener.class, listener );
 440    }
 441   
 442    /**
 443    * Notifies all {@link PropertyListener PropertyListeners} of changes
 444    * to the property (added, removed, changed).
 445    *
 446    * @param event
 447    */
 448  652 public void firePropertyChangedEvent(PropertyChangedEvent event) {
 449    // Guaranteed to return a non-null array
 450  652 Object[] listeners = listenerList.getListenerList();
 451    // Process the listeners last to first, notifying
 452  652 for (int i = listeners.length - 2; i >= 0; i -= 2) {
 453  338 if (listeners[i] == PropertyListener.class) {
 454    // Lazily create the event:
 455  3 ((PropertyListener) listeners[i + 1]).propertyChanged(event);
 456    }
 457    }
 458  652 fireCategoryChangedEvent((CategoryChangedEvent)event);
 459    }
 460   
 461  3 public void setExtendsCategory(String extendsCategory) {
 462  3 this.extendsCategory = extendsCategory;
 463    }
 464   
 465  4 public String getExtendsCategory() {
 466  4 return extendsCategory;
 467    }
 468   
 469    }