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: * TextCache.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.layout;
030:
031: import java.util.HashMap;
032:
033: import org.jfree.report.layout.model.RenderNode;
034: import org.jfree.report.style.StyleSheet;
035: import org.jfree.report.util.InstanceID;
036: import org.jfree.util.ObjectUtilities;
037:
038: /**
039: * Creation-Date: 26.04.2007, 20:23:40
040: *
041: * @author Thomas Morgner
042: */
043: public class TextCache {
044: public static class Result {
045: private RenderNode[] text;
046: private RenderNode[] finish;
047: private StyleSheet styleSheet;
048:
049: public Result(final RenderNode[] text,
050: final RenderNode[] finish, final StyleSheet styleSheet) {
051: this .styleSheet = styleSheet;
052: this .text = (RenderNode[]) text.clone();
053: this .finish = (RenderNode[]) finish.clone();
054: }
055:
056: public StyleSheet getStyleSheet() {
057: return styleSheet;
058: }
059:
060: public RenderNode[] getText() {
061: final RenderNode[] nodes = (RenderNode[]) text.clone();
062: final int nodeCount = nodes.length;
063: for (int i = 0; i < nodeCount; i++) {
064: final RenderNode node = nodes[i];
065: nodes[i] = node.derive(true);
066: }
067: return nodes;
068: }
069:
070: public RenderNode[] getFinish() {
071: final RenderNode[] nodes = (RenderNode[]) finish.clone();
072: final int nodeCount = nodes.length;
073: for (int i = 0; i < nodeCount; i++) {
074: final RenderNode node = nodes[i];
075: nodes[i] = node.derive(true);
076: }
077: return nodes;
078: }
079: }
080:
081: private static class InternalResult extends Result {
082: private long changeTracker;
083: private String originalText;
084:
085: protected InternalResult(final RenderNode[] text,
086: final RenderNode[] finish, final StyleSheet styleSheet,
087: final long changeTracker, final String originalText) {
088: super (text, finish, styleSheet);
089: this .changeTracker = changeTracker;
090: this .originalText = originalText;
091: }
092:
093: public boolean isValid(final long changeTracker,
094: final String text) {
095: if (changeTracker != this .changeTracker) {
096: return false;
097: }
098: return ObjectUtilities.equal(text, originalText);
099: }
100: }
101:
102: private HashMap cache;
103:
104: public TextCache() {
105: cache = new HashMap();
106: }
107:
108: public void store(final InstanceID instanceID,
109: final long changeTracker, final String originalText,
110: final StyleSheet styleSheet, final RenderNode[] text,
111: final RenderNode[] finish) {
112: cache.put(instanceID, new InternalResult(text, finish,
113: styleSheet, changeTracker, originalText));
114: }
115:
116: public Result get(final InstanceID instanceID,
117: final long changeTracker, final String originalText) {
118: final InternalResult o = (InternalResult) cache.get(instanceID);
119: if (o == null) {
120: return null;
121: }
122: if (o.isValid(changeTracker, originalText) == false) {
123: cache.remove(instanceID);
124: return null;
125: }
126: return o;
127: }
128:
129: }
|