Source for org.jfree.report.util.ReportParameters

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: ReportParameters.java 2725 2007-04-01 18:49:29Z taqua $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.util;
  33: 
  34: import java.io.Serializable;
  35: import java.util.HashMap;
  36: 
  37: /**
  38:  * The report parameters collection is a map with string keys. The parameters
  39:  * can be used in a query and will appear as part of the datarow.
  40:  *
  41:  * @author Thomas Morgner
  42:  */
  43: public final class ReportParameters implements Serializable, Cloneable
  44: {
  45:   /**
  46:    * Storage for the properties.
  47:    */
  48:   private HashMap properties;
  49: 
  50:   /**
  51:    * Copy constructor.
  52:    *
  53:    * @param props an existing ReportProperties instance.
  54:    */
  55:   public ReportParameters (final ReportParameters props)
  56:   {
  57:     this.properties = new HashMap(props.properties);
  58:   }
  59: 
  60:   /**
  61:    * Default constructor.
  62:    */
  63:   public ReportParameters ()
  64:   {
  65:     this.properties = new HashMap();
  66:   }
  67: 
  68:   /**
  69:    * Adds a property to this properties collection. If a property with the given name
  70:    * exist, the property will be replaced with the new value. If the value is null, the
  71:    * property will be removed.
  72:    *
  73:    * @param key   the property key.
  74:    * @param value the property value.
  75:    */
  76:   public void put (final String key, final Object value)
  77:   {
  78:     if (key == null)
  79:     {
  80:       throw new NullPointerException
  81:               ("ReportProperties.put (..): Parameter 'key' must not be null");
  82:     }
  83:     if (value == null)
  84:     {
  85:       this.properties.remove(key);
  86:     }
  87:     else
  88:     {
  89:       this.properties.put(key, value);
  90:     }
  91:   }
  92: 
  93:   /**
  94:    * Retrieves the value stored for a key in this properties collection.
  95:    *
  96:    * @param key the property key.
  97:    * @return The stored value, or <code>null</code> if the key does not exist in this
  98:    *         collection.
  99:    */
 100:   public Object get (final String key)
 101:   {
 102:     if (key == null)
 103:     {
 104:       throw new NullPointerException
 105:               ("ReportProperties.get (..): Parameter 'key' must not be null");
 106:     }
 107:     return this.properties.get(key);
 108:   }
 109: 
 110:   /**
 111:    * Retrieves the value stored for a key in this properties collection, and returning the
 112:    * default value if the key was not stored in this properties collection.
 113:    *
 114:    * @param key          the property key.
 115:    * @param defaultValue the default value to be returned when the key is not stored in
 116:    *                     this properties collection.
 117:    * @return The stored value, or the default value if the key does not exist in this
 118:    *         collection.
 119:    */
 120:   public Object get (final String key, final Object defaultValue)
 121:   {
 122:     if (key == null)
 123:     {
 124:       throw new NullPointerException
 125:               ("ReportProperties.get (..): Parameter 'key' must not be null");
 126:     }
 127:     final Object o = this.properties.get(key);
 128:     if (o == null)
 129:     {
 130:       return defaultValue;
 131:     }
 132:     return o;
 133:   }
 134: 
 135:   /**
 136:    * Returns all property keys as array.
 137:    *
 138:    * @return an enumeration of the property keys.
 139:    */
 140:   public String[] keys ()
 141:   {
 142:     return (String[]) this.properties.keySet().toArray(new String[properties.size()]);
 143:   }
 144: 
 145:   /**
 146:    * Removes all properties stored in this collection.
 147:    */
 148:   public void clear ()
 149:   {
 150:     this.properties.clear();
 151:   }
 152: 
 153:   /**
 154:    * Checks whether the given key is stored in this collection of ReportProperties.
 155:    *
 156:    * @param key the property key.
 157:    * @return true, if the given key is known.
 158:    */
 159:   public boolean containsKey (final String key)
 160:   {
 161:     if (key == null)
 162:     {
 163:       throw new NullPointerException
 164:               ("ReportProperties.containsKey (..): Parameter key must not be null");
 165:     }
 166:     return this.properties.containsKey(key);
 167:   }
 168: 
 169:   /**
 170:    * Clones the properties.
 171:    *
 172:    * @return a copy of this ReportProperties object.
 173:    *
 174:    * @throws CloneNotSupportedException this should never happen.
 175:    */
 176:   public Object clone ()
 177:           throws CloneNotSupportedException
 178:   {
 179:     final ReportParameters p = (ReportParameters) super.clone();
 180:     p.properties = (HashMap) this.properties.clone();
 181:     return p;
 182:   }
 183: 
 184:   public int size()
 185:   {
 186:     return properties.size();
 187:   }
 188: }