Clover coverage report - DNA - 1.0
Coverage timestamp: Sun Oct 12 2003 11:23:26 BST
file stats: LOC: 478   Methods: 21
NCLOC: 271   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
DefaultParameters.java 100% 100% 100% 100%
coverage
 1   
 /*
 2   
  * Copyright (C) The JContainer Group. All rights reserved.
 3   
  *
 4   
  * This software is published under the terms of the JContainer
 5   
  * Software License version 1.1, a copy of which has been included
 6   
  * with this distribution in the LICENSE.txt file.
 7   
  */
 8   
 package org.jcontainer.dna.impl;
 9   
 
 10   
 import java.util.HashSet;
 11   
 import java.util.Iterator;
 12   
 import java.util.Properties;
 13   
 import java.util.Set;
 14   
 import org.jcontainer.dna.ParameterException;
 15   
 import org.jcontainer.dna.Parameters;
 16   
 
 17   
 /**
 18   
  * Parameters implementation backed by a Properties object.
 19   
  * The developer should create the DefaultParameters,
 20   
  * associate parameters and then invoke {@link #makeReadOnly()}
 21   
  * before passing the Parameters to the client component.
 22   
  *
 23   
  * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
 24   
  * @version $Revision: 1.17 $ $Date: 2003/09/23 10:15:26 $
 25   
  */
 26   
 public class DefaultParameters
 27   
     extends AbstractFreezable
 28   
     implements Parameters
 29   
 {
 30   
     /**
 31   
      * Constant for separator between parameters
 32   
      * and child parameters.
 33   
      */
 34   
     private static final String SEPARATOR = ".";
 35   
 
 36   
     /**
 37   
      * Constant for empty prefix.
 38   
      */
 39   
     private static final String EMPTY_PREFIX = "";
 40   
 
 41   
     /**
 42   
      * The key-value pairs contained by parameters object.
 43   
      */
 44   
     private final Properties m_parameters = new Properties();
 45   
 
 46   
     /**
 47   
      * The child parameters objects created from with
 48   
      * parameters object.
 49   
      */
 50   
     private final Set m_children = new HashSet();
 51   
 
 52   
     /**
 53   
      * The prefix associated with parameters object.
 54   
      */
 55   
     private final String m_prefix;
 56   
 
 57   
     /**
 58   
      * Create a parameters object with empty prefix.
 59   
      */
 60  52
     public DefaultParameters()
 61   
     {
 62  52
         this( EMPTY_PREFIX );
 63   
     }
 64   
 
 65   
     /**
 66   
      * Create a parameters object with specified prefix.
 67   
      *
 68   
      * @param prefix the prefix
 69   
      */
 70  67
     public DefaultParameters( final String prefix )
 71   
     {
 72  67
         if( null == prefix )
 73   
         {
 74  2
             throw new NullPointerException( "prefix" );
 75   
         }
 76  65
         m_prefix = prefix;
 77   
     }
 78   
 
 79   
     /**
 80   
      * Return the names of all the parameters.
 81   
      *
 82   
      * @return the names of all the parameters.
 83   
      */
 84  8
     public String[] getParameterNames()
 85   
     {
 86  8
         final Set set = getParameters().keySet();
 87  8
         return (String[])set.toArray( new String[ set.size() ] );
 88   
     }
 89   
 
 90   
     /**
 91   
      * Return true of parameter with specified name exists.
 92   
      *
 93   
      * @param name the name
 94   
      * @return true of parameter with specified name exists.
 95   
      */
 96  6
     public boolean isParameter( final String name )
 97   
     {
 98  6
         if( null == name )
 99   
         {
 100  2
             throw new NullPointerException( "name" );
 101   
         }
 102  4
         return getParameters().containsKey( name );
 103   
     }
 104   
 
 105   
     /**
 106   
      * Return value of parameter with specified name.
 107   
      *
 108   
      * @param name the name
 109   
      * @return the value
 110   
      * @throws ParameterException if unable to locate parameter
 111   
      */
 112  20
     public String getParameter( final String name )
 113   
         throws ParameterException
 114   
     {
 115  20
         if( null == name )
 116   
         {
 117  2
             throw new NullPointerException( "name" );
 118   
         }
 119  18
         final String property = getParameters().getProperty( name );
 120  18
         if( null == property )
 121   
         {
 122  2
             final String message =
 123   
                 "Unable to locate parameter named " + name;
 124  2
             throw new ParameterException( message, name );
 125   
         }
 126  16
         return property;
 127   
     }
 128   
 
 129   
     /**
 130   
      * Return value of parameter with specified name.
 131   
      *
 132   
      * @param name the name
 133   
      * @param defaultValue the defaultValue if specified parameter
 134   
      *        does not exist
 135   
      * @return the value
 136   
      */
 137  38
     public String getParameter( final String name,
 138   
                                 final String defaultValue )
 139   
     {
 140  38
         if( null == name )
 141   
         {
 142  2
             throw new NullPointerException( "name" );
 143   
         }
 144  36
         return getParameters().getProperty( name, defaultValue );
 145   
     }
 146   
 
 147   
     /**
 148   
      * Return value of parameter with specified name as a boolean.
 149   
      *
 150   
      * @param name the name
 151   
      * @return the value
 152   
      * @throws ParameterException if unable to locate parameter
 153   
      *         or parameter can not be converted to correct type
 154   
      */
 155  2
     public boolean getParameterAsBoolean( final String name )
 156   
         throws ParameterException
 157   
     {
 158  2
         return getParameter( name ).equals( "true" );
 159   
     }
 160   
 
 161   
     /**
 162   
      * Return value of parameter with specified name as a boolean.
 163   
      *
 164   
      * @param name the name
 165   
      * @param defaultValue the defaultValue if specified parameter
 166   
      *        does not exist or parameter can not be converted to
 167   
      *        the correct type
 168   
      * @return the value
 169   
      */
 170  4
     public boolean getParameterAsBoolean( final String name,
 171   
                                           final boolean defaultValue )
 172   
     {
 173  4
         final String value = getParameter( name, null );
 174  4
         if( null == value )
 175   
         {
 176  2
             return defaultValue;
 177   
         }
 178   
         else
 179   
         {
 180  2
             return value.equals( "true" );
 181   
         }
 182   
     }
 183   
 
 184   
     /**
 185   
      * Return value of parameter with specified name as an integer.
 186   
      *
 187   
      * @param name the name
 188   
      * @return the value
 189   
      * @throws ParameterException if unable to locate parameter
 190   
      *         or parameter can not be converted to correct type
 191   
      */
 192  4
     public int getParameterAsInteger( final String name )
 193   
         throws ParameterException
 194   
     {
 195  4
         final String value = getParameter( name );
 196  4
         try
 197   
         {
 198  4
             return Integer.parseInt( value );
 199   
         }
 200   
         catch( final NumberFormatException nfe )
 201   
         {
 202  2
             final String prefixedName = prefixedName( name );
 203  2
             final String message =
 204   
                 "Unable to parse parameter named " + prefixedName +
 205   
                 " with value '" + value + "'";
 206  2
             throw new ParameterException( message, prefixedName, nfe );
 207   
         }
 208   
     }
 209   
 
 210   
     /**
 211   
      * Return value of parameter with specified name as an integer.
 212   
      *
 213   
      * @param name the name
 214   
      * @param defaultValue the defaultValue if specified parameter
 215   
      *        does not exist or parameter can not be converted to
 216   
      *        the correct type
 217   
      * @return the value
 218   
      */
 219  6
     public int getParameterAsInteger( final String name,
 220   
                                       final int defaultValue )
 221   
     {
 222  6
         final String value = getParameter( name, null );
 223  6
         if( null == value )
 224   
         {
 225  2
             return defaultValue;
 226   
         }
 227   
         else
 228   
         {
 229  4
             try
 230   
             {
 231  4
                 return Integer.parseInt( value );
 232   
             }
 233   
             catch( final NumberFormatException nfe )
 234   
             {
 235  2
                 return defaultValue;
 236   
             }
 237   
         }
 238   
     }
 239   
 
 240   
     /**
 241   
      * Return value of parameter with specified name as a long.
 242   
      *
 243   
      * @param name the name
 244   
      * @return the value
 245   
      * @throws ParameterException if unable to locate parameter
 246   
      *         or parameter can not be converted to correct type
 247   
      */
 248  4
     public long getParameterAsLong( final String name )
 249   
         throws ParameterException
 250   
     {
 251  4
         final String value = getParameter( name );
 252  4
         try
 253   
         {
 254  4
             return Long.parseLong( value );
 255   
         }
 256   
         catch( final NumberFormatException nfe )
 257   
         {
 258  2
             final String prefixedName = prefixedName( name );
 259  2
             final String message =
 260   
                 "Unable to parse parameter named " + prefixedName +
 261   
                 " with value '" + value + "'";
 262  2
             throw new ParameterException( message, prefixedName, nfe );
 263   
         }
 264   
     }
 265   
 
 266   
     /**
 267   
      * Return value of parameter with specified name as a long.
 268   
      *
 269   
      * @param name the name
 270   
      * @param defaultValue the defaultValue if specified parameter
 271   
      *        does not exist or parameter can not be converted to
 272   
      *        the correct type
 273   
      * @return the value
 274   
      */
 275  6
     public long getParameterAsLong( final String name,
 276   
                                     final long defaultValue )
 277   
     {
 278  6
         final String value = getParameter( name, null );
 279  6
         if( null == value )
 280   
         {
 281  2
             return defaultValue;
 282   
         }
 283   
         else
 284   
         {
 285  4
             try
 286   
             {
 287  4
                 return Long.parseLong( value );
 288   
             }
 289   
             catch( final NumberFormatException nfe )
 290   
             {
 291  2
                 return defaultValue;
 292   
             }
 293   
         }
 294   
     }
 295   
 
 296   
     /**
 297   
      * Return value of parameter with specified name as a float.
 298   
      *
 299   
      * @param name the name
 300   
      * @return the value
 301   
      * @throws ParameterException if unable to locate parameter
 302   
      *         or parameter can not be converted to correct type
 303   
      */
 304  4
     public float getParameterAsFloat( final String name )
 305   
         throws ParameterException
 306   
     {
 307  4
         final String value = getParameter( name );
 308  4
         try
 309   
         {
 310  4
             return Float.parseFloat( value );
 311   
         }
 312   
         catch( final NumberFormatException nfe )
 313   
         {
 314  2
             final String prefixedName = prefixedName( name );
 315  2
             final String message =
 316   
                 "Unable to parse parameter named " + name +
 317   
                 " with value '" + value + "'";
 318  2
             throw new ParameterException( message, prefixedName, nfe );
 319   
         }
 320   
     }
 321   
 
 322   
     /**
 323   
      * Return value of parameter with specified name as a float.
 324   
      *
 325   
      * @param name the name
 326   
      * @param defaultValue the defaultValue if specified parameter
 327   
      *        does not exist or parameter can not be converted to
 328   
      *        the correct type
 329   
      * @return the value
 330   
      */
 331  6
     public float getParameterAsFloat( final String name,
 332   
                                       final float defaultValue )
 333   
     {
 334  6
         final String value = getParameter( name, null );
 335  6
         if( null == value )
 336   
         {
 337  2
             return defaultValue;
 338   
         }
 339   
         else
 340   
         {
 341  4
             try
 342   
             {
 343  4
                 return Float.parseFloat( value );
 344   
             }
 345   
             catch( final NumberFormatException nfe )
 346   
             {
 347  2
                 return defaultValue;
 348   
             }
 349   
         }
 350   
     }
 351   
 
 352   
     /**
 353   
      * Return a Parameters object that represents a
 354   
      * subset of parameters with specified prefix. The child
 355   
      * parameters has a prefix with the separator ('.') appended.
 356   
      * ie. if the prefix was "foo" then the parameter
 357   
      * "foo.baz" would be included in child Parameters object
 358   
      * using the key "baz".
 359   
      *
 360   
      * @param prefix the prefix
 361   
      * @return the parameters object
 362   
      */
 363  14
     public Parameters getChildParameters( final String prefix )
 364   
     {
 365  14
         if( null == prefix )
 366   
         {
 367  2
             throw new NullPointerException( "prefix" );
 368   
         }
 369  12
         final String prefixAndSeparator = prefix + SEPARATOR;
 370  12
         final int length = prefix.length() + 1;
 371  12
         final String child = prefixedName( prefix );
 372  12
         final DefaultParameters parameters = new DefaultParameters( child );
 373  12
         final Iterator iterator = getParameters().keySet().iterator();
 374  12
         while( iterator.hasNext() )
 375   
         {
 376  18
             final String key = (String)iterator.next();
 377  18
             if( key.startsWith( prefixAndSeparator ) )
 378   
             {
 379  10
                 final String value = getParameter( key, null );
 380  10
                 final String newKey = key.substring( length );
 381  10
                 parameters.setParameter( newKey, value );
 382   
             }
 383   
         }
 384   
 
 385  12
         parameters.makeReadOnly();
 386  12
         getChildren().add( parameters );
 387  12
         return parameters;
 388   
     }
 389   
 
 390   
     /**
 391   
      * Return name that may be prefixed with full property
 392   
      * name unless prefix is empty.
 393   
      *
 394   
      * @param name the name
 395   
      * @return the name with prefix decorated
 396   
      */
 397  18
     private String prefixedName( final String name )
 398   
     {
 399  18
         if( getPrefix().equals( EMPTY_PREFIX ) )
 400   
         {
 401  14
             return name;
 402   
         }
 403   
         else
 404   
         {
 405  4
             return getPrefix() + SEPARATOR + name;
 406   
         }
 407   
     }
 408   
 
 409   
     /**
 410   
      * Mark the resource and all child parameter
 411   
      * objects as read only.
 412   
      */
 413  20
     public void makeReadOnly()
 414   
     {
 415  20
         super.makeReadOnly();
 416  20
         final Iterator iterator = getChildren().iterator();
 417  20
         while( iterator.hasNext() )
 418   
         {
 419  6
             final Object child = iterator.next();
 420  6
             if( child instanceof Freezable )
 421   
             {
 422  4
                 ( (Freezable)child ).makeReadOnly();
 423   
             }
 424   
         }
 425   
     }
 426   
 
 427   
     /**
 428   
      * Set parameter with specified name to specified value.
 429   
      *
 430   
      * @param name the parameter name
 431   
      * @param value the parameter value
 432   
      */
 433  46
     public void setParameter( final String name,
 434   
                               final String value )
 435   
     {
 436  46
         if( null == name )
 437   
         {
 438  2
             throw new NullPointerException( "name" );
 439   
         }
 440  44
         if( null == value )
 441   
         {
 442  2
             throw new NullPointerException( "value" );
 443   
         }
 444  42
         checkWriteable();
 445  42
         getParameters().setProperty( name, value );
 446   
     }
 447   
 
 448   
     /**
 449   
      * Return the backing properties object associated with parameters.
 450   
      *
 451   
      * @return the backing properties object associated with parameters.
 452   
      */
 453  120
     protected final Properties getParameters()
 454   
     {
 455  120
         return m_parameters;
 456   
     }
 457   
 
 458   
     /**
 459   
      * Return the prefix associated with Parameters object.
 460   
      *
 461   
      * @return the prefix associated with Parameters object.
 462   
      */
 463  26
     protected final String getPrefix()
 464   
     {
 465  26
         return m_prefix;
 466   
     }
 467   
 
 468   
     /**
 469   
      * Return the set of child parameter objects.
 470   
      *
 471   
      * @return the set of child parameter objects
 472   
      */
 473  34
     protected final Set getChildren()
 474   
     {
 475  34
         return m_children;
 476   
     }
 477   
 }
 478