KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRClonePool


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * 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,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.fill;
29
30 import java.util.HashSet JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import net.sf.jasperreports.engine.JRRuntimeException;
35
36 /**
37  * Working clones pooling utility used at fill time.
38  *
39  * @author Lucian Chirita (lucianc@users.sourceforge.net)
40  * @version $Id: JRClonePool.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
41  */

42 public class JRClonePool
43 {
44     private final JRCloneable original;
45     private final LinkedList JavaDoc availableClones;
46     private final boolean trackLockedClones;
47     private final Set JavaDoc lockedClones;
48
49     
50     /**
51      * Creates a clone pool.
52      *
53      * @param original the original element that will be cloned
54      * @param trackLockedClones whether to track clones retrieved from the pool
55      * <p>
56      * If set, the pool will keep a set of in-use clones and the caller will always
57      * have to release the clones back to the pool.
58      * @param useOriginal whether the original object can be used as a working clone
59      */

60     public JRClonePool(JRCloneable original, boolean trackLockedClones, boolean useOriginal)
61     {
62         this.original = original;
63         
64         availableClones = new LinkedList JavaDoc();
65         
66         this.trackLockedClones = trackLockedClones;
67         if (trackLockedClones)
68         {
69             lockedClones = new HashSet JavaDoc();
70         }
71         else
72         {
73             lockedClones = null;
74         }
75         
76         if (useOriginal)
77         {
78             availableClones.add(original);
79         }
80     }
81     
82     
83     /**
84      * Retrieves a clone from the pool.
85      * <p>
86      * The clone is reserved to the caller who will need to call
87      * {@link #releaseClone(Object) releaseClone(Object)} to release it back to the pool.
88      *
89      * @return a clone of the original object
90      */

91     public Object JavaDoc getClone()
92     {
93         JRCloneable clone;
94         
95         if (availableClones.isEmpty())
96         {
97             JRFillCloneFactory factory = new JRFillCloneFactory();
98             clone = original.createClone(factory);
99         }
100         else
101         {
102             clone = (JRCloneable) availableClones.removeFirst();
103         }
104         
105         if (trackLockedClones)
106         {
107             lockedClones.add(clone);
108         }
109         
110         return clone;
111     }
112     
113     
114     /**
115      * Release the clone back to the pool.
116      * The clone will be available for other clients.
117      *
118      * @param clone the clone to be released
119      */

120     public void releaseClone(Object JavaDoc clone)
121     {
122         if (trackLockedClones)
123         {
124             if (!lockedClones.remove(clone))
125             {
126                 throw new JRRuntimeException("Cannot release clone.");
127             }
128         }
129         
130         availableClones.addLast(clone);
131     }
132 }
133
Popular Tags