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 ObjectArrayPool extends Object {
036: private IntHashMap pools = new IntHashMap(23);
037:
038: public static class SizePool extends Object {
039: private List arrays = new ArrayList();
040: private int arraySize;
041: private int size;
042: private int created = 0;
043: private int recovered = 0;
044: private int recycled = 0;
045:
046: public SizePool(int arraySize) {
047: this (arraySize, 0);
048: }
049:
050: public SizePool(int arraySize, int initialSize) {
051: super ();
052: this .arraySize = arraySize;
053: for (int i = 0; i < initialSize; i++) {
054: arrays.add(new Object[arraySize]);
055: }
056: created = size = initialSize;
057: }
058:
059: public int getArraySize() {
060: return arraySize;
061: }
062:
063: public Object[] create() {
064: Object[] result;
065:
066: if (size > 0) {
067: result = (Object[]) arrays.remove(size - 1);
068: size--;
069: recovered++;
070: } else {
071: result = new Object[arraySize];
072: created++;
073: }
074: return result;
075: }
076:
077: public synchronized void recycle(Object[] value) {
078: if (value != null) {
079: if (value.length != arraySize) {
080: throw new IllegalArgumentException(
081: "recycled array size "
082: + value.length
083: + " inappropriate for pool array size "
084: + arraySize);
085: }
086: Arrays.fill(value, null);
087: arrays.add(value);
088: size++;
089: recycled++;
090: } else {
091: throw new IllegalArgumentException(
092: "cannot recycle null object");
093: }
094: }
095:
096: /**
097: Returns the number of items in the pool
098: */
099: public int getSize() {
100: return size;
101: }
102:
103: /**
104: Returns the number of items this pool has created since
105: it's construction.
106: */
107: public int getCreatedCount() {
108: return created;
109: }
110:
111: /**
112: Returns the number of items this pool has recovered from
113: the pool since its construction.
114: */
115: public int getRecoveredCount() {
116: return recovered;
117: }
118:
119: /**
120: Returns the number of items this pool has recycled since
121: it's construction.
122: */
123: public int getRecycledCount() {
124: return recycled;
125: }
126: }
127:
128: public ObjectArrayPool() {
129: super ();
130: }
131:
132: public IntHashMap getSizePools() {
133: return pools;
134: }
135:
136: public synchronized SizePool getSizePool(int arraySize) {
137: SizePool result = (SizePool) pools.get(arraySize);
138:
139: if (result == null) {
140: pools.put(arraySize, result = new SizePool(arraySize));
141: }
142: return result;
143: }
144:
145: public synchronized Object[] create(int arraySize) {
146: return getSizePool(arraySize).create();
147: }
148:
149: public synchronized Object[] create(Object singleton) {
150: Object[] result = create(1);
151:
152: result[0] = singleton;
153: return result;
154: }
155:
156: public synchronized Object[] create(Object object1, Object object2) {
157: Object[] result = create(2);
158:
159: result[0] = object1;
160: result[1] = object2;
161: return result;
162: }
163:
164: public synchronized Object[] create(Object object1, Object object2,
165: Object object3) {
166: Object[] result = create(3);
167:
168: result[0] = object1;
169: result[1] = object2;
170: result[2] = object3;
171: return result;
172: }
173:
174: public synchronized Object[] create(Object object1, Object object2,
175: Object object3, Object object4) {
176: Object[] result = create(4);
177:
178: result[0] = object1;
179: result[1] = object2;
180: result[2] = object3;
181: result[3] = object4;
182: return result;
183: }
184:
185: public synchronized Object[] create(Object object1, Object object2,
186: Object object3, Object object4, Object object5) {
187: Object[] result = create(5);
188:
189: result[0] = object1;
190: result[1] = object2;
191: result[2] = object3;
192: result[3] = object4;
193: result[4] = object5;
194: return result;
195: }
196:
197: public synchronized void recycle(Object[] value) {
198: if (value != null) {
199: getSizePool(value.length).recycle(value);
200: }
201: }
202: }
|