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: PrecomputeNodeImpl.java 3525 2007-10-16 11:43:48Z tmorgner $
027: * ------------
028: * (C) Copyright 2000-2005, by Object Refinery Limited.
029: * (C) Copyright 2005-2007, by Pentaho Corporation.
030: */package org.jfree.report.data;
031:
032: import java.util.ArrayList;
033:
034: /**
035: * A precompute-node represents a resolved element or section of the report definition. Unlike the structural nodes,
036: * these nodes can always have childs.
037: * <p/>
038: * The resulting tree gets pruned as early as possible - nodes which do not contain precomputed or preserved expressions
039: * will not be stored.
040: *
041: * @author Thomas Morgner
042: */
043: public class PrecomputeNodeImpl implements PrecomputeNode {
044: private PrecomputeNodeImpl parent;
045: private PrecomputeNodeImpl next;
046: private PrecomputeNodeImpl firstChild;
047: private PrecomputeNodeImpl lastChild;
048:
049: private PrecomputeNodeKey key;
050: private ArrayList functionResults;
051: private ArrayList functionNames;
052:
053: public PrecomputeNodeImpl(final PrecomputeNodeKey key) {
054: if (key == null) {
055: throw new NullPointerException();
056: }
057: this .key = key;
058: }
059:
060: public PrecomputeNodeKey getKey() {
061: return key;
062: }
063:
064: public PrecomputeNode getParent() {
065: return parent;
066: }
067:
068: protected void setParent(final PrecomputeNodeImpl parent) {
069: this .parent = parent;
070: }
071:
072: public PrecomputeNode getNext() {
073: return next;
074: }
075:
076: protected void setNext(final PrecomputeNodeImpl next) {
077: this .next = next;
078: }
079:
080: public PrecomputeNode getFirstChild() {
081: return firstChild;
082: }
083:
084: protected void setFirstChild(final PrecomputeNodeImpl firstChild) {
085: this .firstChild = firstChild;
086: }
087:
088: public PrecomputeNode getLastChild() {
089: return lastChild;
090: }
091:
092: protected void setLastChild(final PrecomputeNodeImpl lastChild) {
093: this .lastChild = lastChild;
094: }
095:
096: public void add(final PrecomputeNodeImpl node) {
097: if (firstChild == null) {
098: firstChild = node;
099: firstChild.setParent(this );
100: lastChild = node;
101: return;
102: }
103:
104: lastChild.setNext(node);
105: lastChild.setParent(this );
106: }
107:
108: public void addFunction(final String name, final Object value) {
109: if (this .functionNames == null) {
110: functionNames = new ArrayList();
111: functionResults = new ArrayList();
112: }
113:
114: this .functionNames.add(name);
115: this .functionResults.add(value);
116: }
117:
118: public int getFunctionCount() {
119: if (functionNames == null) {
120: return 0;
121: }
122: return functionNames.size();
123: }
124:
125: public String getFunctionName(final int idx) {
126: if (functionNames == null) {
127: throw new IndexOutOfBoundsException();
128: }
129: return (String) functionNames.get(idx);
130: }
131:
132: public Object getFunctionResult(final int idx) {
133: if (functionResults == null) {
134: throw new IndexOutOfBoundsException();
135: }
136: return functionResults.get(idx);
137: }
138:
139: public void prune() {
140: if (parent == null) {
141: return;
142: }
143:
144: if (parent.getLastChild() != this ) {
145: throw new IllegalStateException(
146: "Cannot prune. Not the last child.");
147: }
148: if (parent.getFirstChild() == this) {
149: parent.setFirstChild(null);
150: }
151: parent.setLastChild(null);
152: }
153: }
|