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