001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: ReportParameters.java 3048 2007-07-28 18:02:42Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    
032    package org.jfree.report.util;
033    
034    import java.io.Serializable;
035    import java.util.HashMap;
036    
037    /**
038     * The report parameters collection is a map with string keys. The parameters
039     * can be used in a query and will appear as part of the datarow.
040     *
041     * @author Thomas Morgner
042     */
043    public final class ReportParameters implements Serializable, Cloneable
044    {
045      /**
046       * Storage for the properties.
047       */
048      private HashMap properties;
049    
050      /**
051       * Copy constructor.
052       *
053       * @param props an existing ReportProperties instance.
054       */
055      public ReportParameters (final ReportParameters props)
056      {
057        this.properties = new HashMap(props.properties);
058      }
059    
060      /**
061       * Default constructor.
062       */
063      public ReportParameters ()
064      {
065        this.properties = new HashMap();
066      }
067    
068      /**
069       * Adds a property to this properties collection. If a property with the given name
070       * exist, the property will be replaced with the new value. If the value is null, the
071       * property will be removed.
072       *
073       * @param key   the property key.
074       * @param value the property value.
075       */
076      public void put (final String key, final Object value)
077      {
078        if (key == null)
079        {
080          throw new NullPointerException
081                  ("ReportProperties.put (..): Parameter 'key' must not be null");
082        }
083        if (value == null)
084        {
085          this.properties.remove(key);
086        }
087        else
088        {
089          this.properties.put(key, value);
090        }
091      }
092    
093      /**
094       * Retrieves the value stored for a key in this properties collection.
095       *
096       * @param key the property key.
097       * @return The stored value, or <code>null</code> if the key does not exist in this
098       *         collection.
099       */
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    }