1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.util.HashSet ; 31 import java.util.LinkedList ; 32 import java.util.Set ; 33 34 import net.sf.jasperreports.engine.JRRuntimeException; 35 36 42 public class JRClonePool 43 { 44 private final JRCloneable original; 45 private final LinkedList availableClones; 46 private final boolean trackLockedClones; 47 private final Set lockedClones; 48 49 50 60 public JRClonePool(JRCloneable original, boolean trackLockedClones, boolean useOriginal) 61 { 62 this.original = original; 63 64 availableClones = new LinkedList (); 65 66 this.trackLockedClones = trackLockedClones; 67 if (trackLockedClones) 68 { 69 lockedClones = new HashSet (); 70 } 71 else 72 { 73 lockedClones = null; 74 } 75 76 if (useOriginal) 77 { 78 availableClones.add(original); 79 } 80 } 81 82 83 91 public Object 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 120 public void releaseClone(Object 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 |