Source for org.jfree.util.ExtendedConfigurationWrapper

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ---------------------------------
  28:  * ExtendedConfigurationWrapper.java
  29:  * ---------------------------------
  30:  * (C)opyright 2002-2005, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: ExtendedConfigurationWrapper.java,v 1.5 2005/11/03 09:55:27 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 20-May-2005 : Initial version.
  40:  */
  41: 
  42: package org.jfree.util;
  43: 
  44: import java.util.Iterator;
  45: import java.util.Enumeration;
  46: 
  47: /**
  48:  * A wrapper for the extended configuration interface around a plain configuration.
  49:  *
  50:  * @author Thomas Morgner
  51:  */
  52: public class ExtendedConfigurationWrapper
  53:         implements ExtendedConfiguration
  54: {
  55:   /** The base configuration. */
  56:   private Configuration parent;
  57: 
  58:   /**
  59:    * Creates a wrapper around the given configuration.
  60:    *
  61:    * @param parent the wrapped up configuration.
  62:    * @throws NullPointerException if the parent is null.
  63:    */
  64:   public ExtendedConfigurationWrapper (final Configuration parent)
  65:   {
  66:     if (parent == null)
  67:     {
  68:       throw new NullPointerException("Parent given must not be null");
  69:     }
  70:     this.parent = parent;
  71:   }
  72: 
  73:   /**
  74:    * Returns the boolean value of a given configuration property. The boolean value true
  75:    * is returned, if the contained string is equal to 'true'.
  76:    *
  77:    * @param name the name of the property
  78:    * @return the boolean value of the property.
  79:    */
  80:   public boolean getBoolProperty (final String name)
  81:   {
  82:     return getBoolProperty(name, false);
  83:   }
  84: 
  85:   /**
  86:    * Returns the boolean value of a given configuration property. The boolean value true
  87:    * is returned, if the contained string is equal to 'true'. If the property is not set,
  88:    * the default value is returned.
  89:    *
  90:    * @param name the name of the property
  91:    * @param defaultValue the default value to be returned if the property is not set
  92:    * @return the boolean value of the property.
  93:    */
  94:   public boolean getBoolProperty (final String name,
  95:                                   final boolean defaultValue)
  96:   {
  97:     return "true".equals(parent.getConfigProperty(name, String.valueOf(defaultValue)));
  98:   }
  99: 
 100:   /**
 101:    * Returns a given property as int value. Zero is returned if the
 102:    * property value is no number or the property is not set.
 103:    *
 104:    * @param name the name of the property
 105:    * @return the parsed number value or zero
 106:    */
 107:   public int getIntProperty (final String name)
 108:   {
 109:     return getIntProperty(name, 0);
 110:   }
 111: 
 112:   /**
 113:    * Returns a given property as int value. The specified default value is returned if the
 114:    * property value is no number or the property is not set.
 115:    *
 116:    * @param name the name of the property
 117:    * @param defaultValue the value to be returned if the property is no integer value
 118:    * @return the parsed number value or the specified default value
 119:    */
 120:   public int getIntProperty (final String name,
 121:                              final int defaultValue)
 122:   {
 123:     final String retval = parent.getConfigProperty(name);
 124:     if (retval == null)
 125:     {
 126:       return defaultValue;
 127:     }
 128:     try
 129:     {
 130:       return Integer.parseInt(retval);
 131:     }
 132:     catch (Exception e)
 133:     {
 134:       return defaultValue;
 135:     }
 136:   }
 137: 
 138:   /**
 139:    * Checks, whether a given property is defined.
 140:    *
 141:    * @param name the name of the property
 142:    * @return true, if the property is defined, false otherwise.
 143:    */
 144:   public boolean isPropertySet (final String name)
 145:   {
 146:     return parent.getConfigProperty(name) != null;
 147:   }
 148: 
 149:   /**
 150:    * Returns all keys with the given prefix.
 151:    *
 152:    * @param prefix the prefix
 153:    * @return the iterator containing all keys with that prefix
 154:    */
 155:   public Iterator findPropertyKeys (final String prefix)
 156:   {
 157:     return parent.findPropertyKeys(prefix);
 158:   }
 159: 
 160:   /**
 161:    * Returns the configuration property with the specified key.
 162:    *
 163:    * @param key the property key.
 164:    * @return the property value.
 165:    */
 166:   public String getConfigProperty (final String key)
 167:   {
 168:     return parent.getConfigProperty(key);
 169:   }
 170: 
 171:   /**
 172:    * Returns the configuration property with the specified key (or the specified default
 173:    * value if there is no such property).
 174:    * <p/>
 175:    * If the property is not defined in this configuration, the code will lookup the
 176:    * property in the parent configuration.
 177:    *
 178:    * @param key          the property key.
 179:    * @param defaultValue the default value.
 180:    * @return the property value.
 181:    */
 182:   public String getConfigProperty (final String key, final String defaultValue)
 183:   {
 184:     return parent.getConfigProperty(key, defaultValue);
 185:   }
 186: 
 187:   public Enumeration getConfigProperties()
 188:   {
 189:     return parent.getConfigProperties();
 190:   }
 191: }