01: /*
02: * Copyright 1999,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.jasper.util;
18:
19: /**
20: * Simple object pool. Based on ThreadPool and few other classes
21: *
22: * The pool will ignore overflow and return null if empty.
23: *
24: * @author Gal Shachor
25: * @author Costin
26: */
27: public final class SimplePool {
28:
29: private static final int DEFAULT_SIZE = 16;
30:
31: /*
32: * Where the threads are held.
33: */
34: private Object pool[];
35:
36: private int max;
37: private int current = -1;
38:
39: private Object lock;
40:
41: public SimplePool() {
42: this .max = DEFAULT_SIZE;
43: this .pool = new Object[max];
44: this .lock = new Object();
45: }
46:
47: public SimplePool(int max) {
48: this .max = max;
49: this .pool = new Object[max];
50: this .lock = new Object();
51: }
52:
53: /**
54: * Adds the given object to the pool, and does nothing if the pool is full
55: */
56: public void put(Object o) {
57: synchronized (lock) {
58: if (current < (max - 1)) {
59: current += 1;
60: pool[current] = o;
61: }
62: }
63: }
64:
65: /**
66: * Get an object from the pool, null if the pool is empty.
67: */
68: public Object get() {
69: Object item = null;
70: synchronized (lock) {
71: if (current >= 0) {
72: item = pool[current];
73: current -= 1;
74: }
75: }
76: return item;
77: }
78:
79: /**
80: * Return the size of the pool
81: */
82: public int getMax() {
83: return max;
84: }
85: }
|