1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.jasper.util;
19
20 /***
21 * Simple object pool. Based on ThreadPool and few other classes
22 * <p/>
23 * The pool will ignore overflow and return null if empty.
24 *
25 * @author Gal Shachor
26 * @author Costin
27 */
28 public final class SimplePool {
29
30 private static final int DEFAULT_SIZE = 16;
31
32
33
34
35 private Object pool[];
36
37 private int max;
38 private int current = -1;
39
40 private Object lock;
41
42 public SimplePool() {
43 this.max = DEFAULT_SIZE;
44 this.pool = new Object[max];
45 this.lock = new Object();
46 }
47
48 public SimplePool(int max) {
49 this.max = max;
50 this.pool = new Object[max];
51 this.lock = new Object();
52 }
53
54 /***
55 * Adds the given object to the pool, and does nothing if the pool is full
56 */
57 public void put(Object o) {
58 synchronized (lock) {
59 if (current < (max - 1)) {
60 current += 1;
61 pool[current] = o;
62 }
63 }
64 }
65
66 /***
67 * Get an object from the pool, null if the pool is empty.
68 */
69 public Object get() {
70 Object item = null;
71 synchronized (lock) {
72 if (current >= 0) {
73 item = pool[current];
74 current -= 1;
75 }
76 }
77 return item;
78 }
79
80 /***
81 * Return the size of the pool
82 */
83 public int getMax() {
84 return max;
85 }
86 }