| 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: PrecomputeNodeImpl.java 10756 2009-12-02 15:58:24Z tmorgner $ |
| 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.data; |
| 33 |
|
| 34 |
import java.util.ArrayList; |
| 35 |
|
| 36 |
/** |
| 37 |
* A precompute-node represents a resolved element or section of the report definition. Unlike the structural nodes, |
| 38 |
* these nodes can always have childs. |
| 39 |
* <p/> |
| 40 |
* The resulting tree gets pruned as early as possible - nodes which do not contain precomputed or preserved expressions |
| 41 |
* will not be stored. |
| 42 |
* |
| 43 |
* @author Thomas Morgner |
| 44 |
*/ |
| 45 |
public class PrecomputeNodeImpl implements PrecomputeNode |
| 46 |
{ |
| 47 |
private PrecomputeNodeImpl parent; |
| 48 |
private PrecomputeNodeImpl next; |
| 49 |
private PrecomputeNodeImpl firstChild; |
| 50 |
private PrecomputeNodeImpl lastChild; |
| 51 |
|
| 52 |
private PrecomputeNodeKey key; |
| 53 |
private ArrayList functionResults; |
| 54 |
private ArrayList functionNames; |
| 55 |
|
| 56 |
public PrecomputeNodeImpl(PrecomputeNodeKey key) |
| 57 |
{ |
| 58 |
if (key == null) |
| 59 |
{ |
| 60 |
throw new NullPointerException(); |
| 61 |
} |
| 62 |
this.key = key; |
| 63 |
} |
| 64 |
|
| 65 |
public PrecomputeNodeKey getKey() |
| 66 |
{ |
| 67 |
return key; |
| 68 |
} |
| 69 |
|
| 70 |
public PrecomputeNode getParent() |
| 71 |
{ |
| 72 |
return parent; |
| 73 |
} |
| 74 |
|
| 75 |
protected void setParent(final PrecomputeNodeImpl parent) |
| 76 |
{ |
| 77 |
this.parent = parent; |
| 78 |
} |
| 79 |
|
| 80 |
public PrecomputeNode getNext() |
| 81 |
{ |
| 82 |
return next; |
| 83 |
} |
| 84 |
|
| 85 |
protected void setNext(final PrecomputeNodeImpl next) |
| 86 |
{ |
| 87 |
this.next = next; |
| 88 |
} |
| 89 |
|
| 90 |
public PrecomputeNode getFirstChild() |
| 91 |
{ |
| 92 |
return firstChild; |
| 93 |
} |
| 94 |
|
| 95 |
protected void setFirstChild(final PrecomputeNodeImpl firstChild) |
| 96 |
{ |
| 97 |
this.firstChild = firstChild; |
| 98 |
} |
| 99 |
|
| 100 |
public PrecomputeNode getLastChild() |
| 101 |
{ |
| 102 |
return lastChild; |
| 103 |
} |
| 104 |
|
| 105 |
protected void setLastChild(final PrecomputeNodeImpl lastChild) |
| 106 |
{ |
| 107 |
this.lastChild = lastChild; |
| 108 |
} |
| 109 |
|
| 110 |
public void add(PrecomputeNodeImpl node) |
| 111 |
{ |
| 112 |
if (firstChild == null) |
| 113 |
{ |
| 114 |
firstChild = node; |
| 115 |
firstChild.setParent(this); |
| 116 |
lastChild = node; |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
lastChild.setNext(node); |
| 121 |
lastChild.setParent(this); |
| 122 |
} |
| 123 |
|
| 124 |
public void addFunction(final String name, final Object value) |
| 125 |
{ |
| 126 |
if (this.functionNames == null) |
| 127 |
{ |
| 128 |
functionNames = new ArrayList(); |
| 129 |
functionResults = new ArrayList(); |
| 130 |
} |
| 131 |
|
| 132 |
this.functionNames.add(name); |
| 133 |
this.functionResults.add(value); |
| 134 |
} |
| 135 |
|
| 136 |
public int getFunctionCount() |
| 137 |
{ |
| 138 |
if (functionNames == null) |
| 139 |
{ |
| 140 |
return 0; |
| 141 |
} |
| 142 |
return functionNames.size(); |
| 143 |
} |
| 144 |
|
| 145 |
public String getFunctionName(int idx) |
| 146 |
{ |
| 147 |
if (functionNames == null) |
| 148 |
{ |
| 149 |
throw new IndexOutOfBoundsException(); |
| 150 |
} |
| 151 |
return (String) functionNames.get(idx); |
| 152 |
} |
| 153 |
|
| 154 |
public Object getFunctionResult(int idx) |
| 155 |
{ |
| 156 |
if (functionResults == null) |
| 157 |
{ |
| 158 |
throw new IndexOutOfBoundsException(); |
| 159 |
} |
| 160 |
return functionResults.get(idx); |
| 161 |
} |
| 162 |
|
| 163 |
public void prune() |
| 164 |
{ |
| 165 |
if (parent == null) |
| 166 |
{ |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
if (parent.getLastChild() != this) |
| 171 |
{ |
| 172 |
throw new IllegalStateException("Cannot prune. Not the last child."); |
| 173 |
} |
| 174 |
if (parent.getFirstChild() == this) |
| 175 |
{ |
| 176 |
parent.setFirstChild(null); |
| 177 |
} |
| 178 |
parent.setLastChild(null); |
| 179 |
} |
| 180 |
} |