001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * 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,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.fill;
029:
030: import java.util.HashSet;
031: import java.util.LinkedList;
032: import java.util.Set;
033:
034: import net.sf.jasperreports.engine.JRRuntimeException;
035:
036: /**
037: * Working clones pooling utility used at fill time.
038: *
039: * @author Lucian Chirita (lucianc@users.sourceforge.net)
040: * @version $Id: JRClonePool.java 1229 2006-04-19 10:27:35Z teodord $
041: */
042: public class JRClonePool {
043: private final JRCloneable original;
044: private final LinkedList availableClones;
045: private final boolean trackLockedClones;
046: private final Set lockedClones;
047:
048: /**
049: * Creates a clone pool.
050: *
051: * @param original the original element that will be cloned
052: * @param trackLockedClones whether to track clones retrieved from the pool
053: * <p>
054: * If set, the pool will keep a set of in-use clones and the caller will always
055: * have to release the clones back to the pool.
056: * @param useOriginal whether the original object can be used as a working clone
057: */
058: public JRClonePool(JRCloneable original, boolean trackLockedClones,
059: boolean useOriginal) {
060: this .original = original;
061:
062: availableClones = new LinkedList();
063:
064: this .trackLockedClones = trackLockedClones;
065: if (trackLockedClones) {
066: lockedClones = new HashSet();
067: } else {
068: lockedClones = null;
069: }
070:
071: if (useOriginal) {
072: availableClones.add(original);
073: }
074: }
075:
076: /**
077: * Retrieves a clone from the pool.
078: * <p>
079: * The clone is reserved to the caller who will need to call
080: * {@link #releaseClone(Object) releaseClone(Object)} to release it back to the pool.
081: *
082: * @return a clone of the original object
083: */
084: public Object getClone() {
085: JRCloneable clone;
086:
087: if (availableClones.isEmpty()) {
088: JRFillCloneFactory factory = new JRFillCloneFactory();
089: clone = original.createClone(factory);
090: } else {
091: clone = (JRCloneable) availableClones.removeFirst();
092: }
093:
094: if (trackLockedClones) {
095: lockedClones.add(clone);
096: }
097:
098: return clone;
099: }
100:
101: /**
102: * Release the clone back to the pool.
103: * The clone will be available for other clients.
104: *
105: * @param clone the clone to be released
106: */
107: public void releaseClone(Object clone) {
108: if (trackLockedClones) {
109: if (!lockedClones.remove(clone)) {
110: throw new JRRuntimeException("Cannot release clone.");
111: }
112: }
113:
114: availableClones.addLast(clone);
115: }
116: }
|