001: //--------------------------------------------------------------------------
002: // Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package ognl;
032:
033: import java.util.*;
034:
035: public final class EvaluationPool extends Object {
036: private List evaluations = new ArrayList();
037: private int size = 0;
038: private int created = 0;
039: private int recovered = 0;
040: private int recycled = 0;
041:
042: public EvaluationPool() {
043: this (0);
044: }
045:
046: public EvaluationPool(int initialSize) {
047: super ();
048: for (int i = 0; i < initialSize; i++) {
049: evaluations.add(new Evaluation(null, null));
050: }
051: created = size = initialSize;
052: }
053:
054: /**
055: Returns an Evaluation that contains the node, source and whether it
056: is a set operation. If there are no Evaluation objects in the
057: pool one is created and returned.
058: */
059: public Evaluation create(SimpleNode node, Object source) {
060: return create(node, source, false);
061: }
062:
063: /**
064: Returns an Evaluation that contains the node, source and whether it
065: is a set operation. If there are no Evaluation objects in the
066: pool one is created and returned.
067: */
068: public synchronized Evaluation create(SimpleNode node,
069: Object source, boolean setOperation) {
070: Evaluation result;
071:
072: if (size > 0) {
073: result = (Evaluation) evaluations.remove(size - 1);
074: result.init(node, source, setOperation);
075: size--;
076: recovered++;
077: } else {
078: result = new Evaluation(node, source, setOperation);
079: created++;
080: }
081: return result;
082: }
083:
084: /**
085: Recycles an Evaluation
086: */
087: public synchronized void recycle(Evaluation value) {
088: if (value != null) {
089: value.reset();
090: evaluations.add(value);
091: size++;
092: recycled++;
093: }
094: }
095:
096: /**
097: Recycles an of Evaluation and all of it's siblings
098: and children.
099: */
100: public void recycleAll(Evaluation value) {
101: if (value != null) {
102: recycleAll(value.getNext());
103: recycleAll(value.getFirstChild());
104: recycle(value);
105: }
106: }
107:
108: /**
109: Recycles a List of Evaluation objects
110: */
111: public void recycleAll(List value) {
112: if (value != null) {
113: for (int i = 0, icount = value.size(); i < icount; i++) {
114: recycle((Evaluation) value.get(i));
115: }
116: }
117: }
118:
119: /**
120: Returns the number of items in the pool
121: */
122: public int getSize() {
123: return size;
124: }
125:
126: /**
127: Returns the number of items this pool has created since
128: it's construction.
129: */
130: public int getCreatedCount() {
131: return created;
132: }
133:
134: /**
135: Returns the number of items this pool has recovered from
136: the pool since its construction.
137: */
138: public int getRecoveredCount() {
139: return recovered;
140: }
141:
142: /**
143: Returns the number of items this pool has recycled since
144: it's construction.
145: */
146: public int getRecycledCount() {
147: return recycled;
148: }
149: }
|