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: */package org.jfree.report.util;
031:
032: import java.io.Serializable;
033: import java.util.HashMap;
034:
035: /**
036: * The report parameters collection is a map with string keys. The parameters
037: * can be used in a query and will appear as part of the datarow.
038: *
039: * @author Thomas Morgner
040: */
041: public final class ReportParameters implements Serializable, Cloneable {
042: /**
043: * Storage for the properties.
044: */
045: private HashMap properties;
046:
047: /**
048: * Copy constructor.
049: *
050: * @param props an existing ReportProperties instance.
051: */
052: public ReportParameters(final ReportParameters props) {
053: this .properties = new HashMap(props.properties);
054: }
055:
056: /**
057: * Default constructor.
058: */
059: public ReportParameters() {
060: this .properties = new HashMap();
061: }
062:
063: /**
064: * Adds a property to this properties collection. If a property with the given name
065: * exist, the property will be replaced with the new value. If the value is null, the
066: * property will be removed.
067: *
068: * @param key the property key.
069: * @param value the property value.
070: */
071: public void put(final String key, final Object value) {
072: if (key == null) {
073: throw new NullPointerException(
074: "ReportProperties.put (..): Parameter 'key' must not be null");
075: }
076: if (value == null) {
077: this .properties.remove(key);
078: } else {
079: this .properties.put(key, value);
080: }
081: }
082:
083: /**
084: * Retrieves the value stored for a key in this properties collection.
085: *
086: * @param key the property key.
087: * @return The stored value, or <code>null</code> if the key does not exist in this
088: * collection.
089: */
090: public Object get(final String key) {
091: if (key == null) {
092: throw new NullPointerException(
093: "ReportProperties.get (..): Parameter 'key' must not be null");
094: }
095: return this .properties.get(key);
096: }
097:
098: /**
099: * Retrieves the value stored for a key in this properties collection, and returning the
100: * default value if the key was not stored in this properties collection.
101: *
102: * @param key the property key.
103: * @param defaultValue the default value to be returned when the key is not stored in
104: * this properties collection.
105: * @return The stored value, or the default value if the key does not exist in this
106: * collection.
107: */
108: public Object get(final String key, final Object defaultValue) {
109: if (key == null) {
110: throw new NullPointerException(
111: "ReportProperties.get (..): Parameter 'key' must not be null");
112: }
113: final Object o = this .properties.get(key);
114: if (o == null) {
115: return defaultValue;
116: }
117: return o;
118: }
119:
120: /**
121: * Returns all property keys as array.
122: *
123: * @return an enumeration of the property keys.
124: */
125: public String[] keys() {
126: return (String[]) this .properties.keySet().toArray(
127: new String[properties.size()]);
128: }
129:
130: /**
131: * Removes all properties stored in this collection.
132: */
133: public void clear() {
134: this .properties.clear();
135: }
136:
137: /**
138: * Checks whether the given key is stored in this collection of ReportProperties.
139: *
140: * @param key the property key.
141: * @return true, if the given key is known.
142: */
143: public boolean containsKey(final String key) {
144: if (key == null) {
145: throw new NullPointerException(
146: "ReportProperties.containsKey (..): Parameter key must not be null");
147: }
148: return this .properties.containsKey(key);
149: }
150:
151: /**
152: * Clones the properties.
153: *
154: * @return a copy of this ReportProperties object.
155: *
156: * @throws CloneNotSupportedException this should never happen.
157: */
158: public Object clone() throws CloneNotSupportedException {
159: final ReportParameters p = (ReportParameters) super .clone();
160: p.properties = (HashMap) this .properties.clone();
161: return p;
162: }
163:
164: public int size() {
165: return properties.size();
166: }
167: }
|