001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, 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: * ExpressionCollection.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function;
030:
031: import java.io.Serializable;
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Collections;
035: import java.util.Iterator;
036: import java.util.List;
037:
038: /**
039: * Collects all expressions used in the report. There exist 2 states of the ExpressionCollection. In the first,
040: * modifiable state, expressions can be added to the collection. During the adding the expressions get initialized. An
041: * ExpressionCollection in this state is not able to connect to an DataRow.
042: * <p/>
043: * The second state is an immutable state of this collection, no expressions can be added or removed. This
044: * ReadOnlyExpressionCollection can be created by calling getCopy() on the first-state expression collection. The
045: * ReadOnlyExpressionCollection is able to connect to a DataRow.
046: *
047: * @author Thomas Morgner
048: */
049: public class ExpressionCollection implements Cloneable, Serializable {
050: /**
051: * Ordered storage for the Expressions.
052: */
053: private ArrayList expressionList;
054:
055: /**
056: * Creates a new expression collection (initially empty).
057: */
058: public ExpressionCollection() {
059: expressionList = new ArrayList();
060: }
061:
062: /**
063: * Creates a new expression collection, populated with the supplied expressions.
064: *
065: * @param expressions a collection of expressions.
066: * @throws ClassCastException if the collection does not contain Expressions
067: */
068: public ExpressionCollection(final Collection expressions) {
069: this ();
070: addAll(expressions);
071: }
072:
073: /**
074: * Adds all expressions contained in the given collection to this expression collection. The expressions get
075: * initialized during the adding process.
076: *
077: * @param expressions the expressions to be added.
078: * @throws ClassCastException if the collection does not contain expressions
079: */
080: public void addAll(final Collection expressions) {
081: if (expressions != null) {
082: final Iterator iterator = expressions.iterator();
083: while (iterator.hasNext()) {
084: final Expression f = (Expression) iterator.next();
085: add(f);
086: }
087: }
088: }
089:
090: /**
091: * Returns the {@link Expression} with the specified name (or <code>null</code>).
092: *
093: * @param name the expression name (<code>null</code> not permitted).
094: * @return The expression.
095: * @throws NullPointerException if the name given is <code>null</code>.
096: */
097: public Expression get(final String name) {
098: if (name == null) {
099: throw new NullPointerException();
100: }
101:
102: final int position = findExpressionByName(name);
103: if (position == -1) {
104: return null;
105: }
106: return getExpression(position);
107: }
108:
109: /**
110: * Searches the list of expressions for an expression with the given name.
111: *
112: * @param name the name, never null.
113: * @return the position of the expression with that name or -1 if no expression contains that name.
114: */
115: private int findExpressionByName(final String name) {
116: for (int i = 0; i < expressionList.size(); i++) {
117: final Expression expression = (Expression) expressionList
118: .get(i);
119: if (name.equals(expression.getName())) {
120: return i;
121: }
122: }
123: return -1;
124: }
125:
126: /**
127: * Adds an expression to the collection. The expression is initialized before it is added to this collection.
128: *
129: * @param e the expression.
130: */
131: public void add(final Expression e) {
132: if (e == null) {
133: throw new NullPointerException("Expression is null");
134: }
135:
136: final int position = findExpressionByName(e.getName());
137: if (position == -1) {
138: privateAdd(e);
139: return;
140: }
141:
142: removeExpression(e);
143: privateAdd(e);
144: }
145:
146: /**
147: * Adds an expression to the collection.
148: *
149: * @param e the expression.
150: * @throws NullPointerException if the given Expression is null.
151: */
152: protected void privateAdd(final Expression e) {
153: expressionList.add(e);
154: }
155:
156: /**
157: * Removes an expression from the collection.
158: *
159: * @param e the expression.
160: * @throws NullPointerException if the given Expression is null.
161: */
162: public void removeExpression(final Expression e) {
163: final int position = findExpressionByName(e.getName());
164: if (position == -1) {
165: return;
166: }
167: expressionList.remove(position);
168: }
169:
170: /**
171: * Returns the number of active expressions in this collection.
172: *
173: * @return the number of expressions in this collection
174: */
175: public int size() {
176: return expressionList.size();
177: }
178:
179: /**
180: * Returns the expression on the given position in the list.
181: *
182: * @param pos the position in the list.
183: * @return the expression.
184: * @throws IndexOutOfBoundsException if the given position is invalid
185: */
186: public Expression getExpression(final int pos) {
187: return (Expression) expressionList.get(pos);
188: }
189:
190: /**
191: * Clones this expression collection and all expressions contained in the collection.
192: *
193: * @return The clone.
194: * @throws CloneNotSupportedException should never happen.
195: */
196: public Object clone() throws CloneNotSupportedException {
197: final ExpressionCollection col = (ExpressionCollection) super
198: .clone();
199: col.expressionList = (ArrayList) expressionList.clone();
200: col.expressionList.clear();
201:
202: try {
203: final Iterator it = expressionList.iterator();
204: while (it.hasNext()) {
205: final Expression ex = (Expression) it.next();
206: col.privateAdd(ex.getInstance());
207: }
208: } catch (Exception e) {
209: throw new CloneNotSupportedException(
210: "Unable to clone an expression");
211: }
212: return col;
213: }
214:
215: /**
216: * Returns the expressions contained in this collection as unmodifiable list.
217: *
218: * @return the expressions as list.
219: * @deprecated This functionality will be removed. Use the array-version instead.
220: */
221: public List getExpressionList() {
222: return Collections.unmodifiableList(expressionList);
223: }
224:
225: /**
226: * Return all expressions contained in this collection as array.
227: *
228: * @return the expressions as array.
229: */
230: public Expression[] getExpressions() {
231: return (Expression[]) expressionList
232: .toArray(new Expression[expressionList.size()]);
233: }
234: }
|