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: StaticDataRow.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.Collections;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: import org.jfree.report.DataFlags;
037: import org.jfree.report.DataRow;
038: import org.jfree.report.DataSourceException;
039: import org.jfree.report.util.IntegerCache;
040:
041: /**
042: * This is a static datarow holding a value for each name in the datarow. This
043: * datarow does not hold dataflags and thus does not track the changes done to
044: * the data inside.
045: * <p/>
046: * The StaticDataRow is a derived view and is used to provide a safe collection
047: * of the values of the previous datarow.
048: *
049: * @author Thomas Morgner
050: */
051: public class StaticDataRow implements DataRow {
052: private String[] names;
053: private Object[] values;
054: private Map nameCache;
055:
056: protected StaticDataRow() {
057: }
058:
059: protected StaticDataRow(final StaticDataRow dataRow) {
060: if (dataRow == null) {
061: throw new NullPointerException();
062: }
063:
064: this .nameCache = dataRow.nameCache;
065: this .names = dataRow.names;
066: this .values = dataRow.values;
067: }
068:
069: public StaticDataRow(final DataRow dataRow)
070: throws DataSourceException {
071: if (dataRow == null) {
072: throw new NullPointerException();
073: }
074:
075: final HashMap nameCache = new HashMap();
076: synchronized (dataRow) {
077: final int columnCount = dataRow.getColumnCount();
078: this .names = new String[columnCount];
079: this .values = new Object[dataRow.getColumnCount()];
080: for (int i = 0; i < columnCount; i++) {
081: names[i] = dataRow.getColumnName(i);
082: values[i] = dataRow.get(i);
083: if (names[i] != null) {
084: nameCache.put(names[i], IntegerCache.getInteger(i));
085: }
086: }
087: }
088: this .nameCache = Collections.unmodifiableMap(nameCache);
089: }
090:
091: public StaticDataRow(final String[] names, final Object[] values) {
092: setData(names, values);
093: }
094:
095: protected void setData(final String[] names, final Object[] values) {
096: if (names == null) {
097: throw new NullPointerException();
098: }
099: if (values == null) {
100: throw new NullPointerException();
101: }
102: if (names.length != values.length) {
103: throw new IndexOutOfBoundsException();
104: }
105: this .names = (String[]) names.clone();
106: this .values = (Object[]) values.clone();
107:
108: final HashMap nameCache = new HashMap();
109: for (int i = 0; i < names.length; i++) {
110: final String name = names[i];
111: if (name != null) {
112: nameCache.put(name, IntegerCache.getInteger(i));
113: }
114: }
115: this .nameCache = Collections.unmodifiableMap(nameCache);
116: }
117:
118: protected void updateData(final Object[] values) {
119: if (values.length != this .values.length) {
120: throw new IllegalArgumentException(
121: "You should preserve the number of columns.");
122: }
123:
124: this .values = (Object[]) values.clone();
125: }
126:
127: /**
128: * Returns the value of the expression or column in the tablemodel using the
129: * given column number as index. For functions and expressions, the
130: * <code>getValue()</code> method is called and for columns from the
131: * tablemodel the tablemodel method <code>getValueAt(row, column)</code> gets
132: * called.
133: *
134: * @param col the item index.
135: * @return the value.
136: * @throws IllegalStateException if the datarow detected a deadlock.
137: */
138: public Object get(final int col) throws DataSourceException {
139: return values[col];
140: }
141:
142: /**
143: * Returns the value of the function, expression or column using its specific
144: * name. The given name is translated into a valid column number and the the
145: * column is queried. For functions and expressions, the
146: * <code>getValue()</code> method is called and for columns from the
147: * tablemodel the tablemodel method <code>getValueAt(row, column)</code> gets
148: * called.
149: *
150: * @param col the item index.
151: * @return the value.
152: * @throws IllegalStateException if the datarow detected a deadlock.
153: */
154: public Object get(final String col) throws DataSourceException {
155: final Integer idx = (Integer) nameCache.get(col);
156: if (idx == null) {
157: return null;
158: }
159: return values[idx.intValue()];
160: }
161:
162: /**
163: * Returns the name of the column, expression or function. For columns from
164: * the tablemodel, the tablemodels <code>getColumnName</code> method is
165: * called. For functions, expressions and report properties the assigned name
166: * is returned.
167: *
168: * @param col the item index.
169: * @return the name.
170: */
171: public String getColumnName(final int col)
172: throws DataSourceException {
173: return names[col];
174: }
175:
176: /**
177: * Returns the number of columns, expressions and functions and marked
178: * ReportProperties in the report.
179: *
180: * @return the item count.
181: */
182: public int getColumnCount() throws DataSourceException {
183: return values.length;
184: }
185:
186: public DataFlags getFlags(final String col)
187: throws DataSourceException {
188: return new DefaultDataFlags(col, get(col), false);
189: }
190:
191: public DataFlags getFlags(final int col) throws DataSourceException {
192: return new DefaultDataFlags(names[col], values[col], false);
193: }
194: }
|