KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > store > ValuePool


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.store;
33
34 import java.math.BigDecimal JavaDoc;
35 import java.sql.Date JavaDoc;
36
37 /**
38   * Supports pooling of Integer, Long, Double, BigDecimal, String and Date
39   * Java Objects. Leads to reduction in memory use when an Object is used more
40   * then twice in the database.
41   *
42   * getXXX methods are used for retrival of values. If a value is not in
43   * the pool, it is added to the pool and returned. When the pool gets
44   * full, half the contents that have been accessed less recently are purged.
45   *
46   * @author fredt@users
47   * @version 1.8.0
48   * @since 1.7.2
49   */

50 public class ValuePool {
51
52     //
53
static ValuePoolHashMap intPool;
54     static ValuePoolHashMap longPool;
55     static ValuePoolHashMap doublePool;
56     static ValuePoolHashMap bigdecimalPool;
57     static ValuePoolHashMap stringPool;
58     static ValuePoolHashMap datePool;
59     static final int DEFAULT_VALUE_POOL_SIZE = 10000;
60     static final int[] defaultPoolLookupSize = new int[] {
61         DEFAULT_VALUE_POOL_SIZE, DEFAULT_VALUE_POOL_SIZE,
62         DEFAULT_VALUE_POOL_SIZE, DEFAULT_VALUE_POOL_SIZE,
63         DEFAULT_VALUE_POOL_SIZE, DEFAULT_VALUE_POOL_SIZE
64     };
65     static final int POOLS_COUNT = defaultPoolLookupSize.length;
66     static final int defaultSizeFactor = 2;
67     static final int defaultMaxStringLength = 16;
68
69     //
70
static ValuePoolHashMap[] poolList;
71
72     //
73
static int maxStringLength;
74
75     //
76
static {
77         initPool();
78     }
79
80     private static void initPool() {
81
82         int[] sizeArray = defaultPoolLookupSize;
83         int sizeFactor = defaultSizeFactor;
84
85         synchronized (ValuePool.class) {
86             maxStringLength = defaultMaxStringLength;
87             poolList = new ValuePoolHashMap[POOLS_COUNT];
88
89             for (int i = 0; i < POOLS_COUNT; i++) {
90                 int size = sizeArray[i];
91
92                 poolList[i] = new ValuePoolHashMap(size, size * sizeFactor,
93                                                    BaseHashMap.PURGE_HALF);
94             }
95
96             intPool = poolList[0];
97             longPool = poolList[1];
98             doublePool = poolList[2];
99             bigdecimalPool = poolList[3];
100             stringPool = poolList[4];
101             datePool = poolList[5];
102         }
103     }
104
105     public static void resetPool(int[] sizeArray, int sizeFactor) {
106
107         synchronized (ValuePool.class) {
108             for (int i = 0; i < POOLS_COUNT; i++) {
109                 poolList[i].resetCapacity(sizeArray[i] * sizeFactor,
110                                           BaseHashMap.PURGE_HALF);
111             }
112         }
113     }
114
115     public static void resetPool() {
116
117         synchronized (ValuePool.class) {
118             resetPool(defaultPoolLookupSize, defaultSizeFactor);
119         }
120     }
121
122     public static void clearPool() {
123
124         synchronized (ValuePool.class) {
125             for (int i = 0; i < POOLS_COUNT; i++) {
126                 poolList[i].clear();
127             }
128         }
129     }
130
131     public static Integer JavaDoc getInt(int val) {
132
133         synchronized (intPool) {
134             return intPool.getOrAddInteger(val);
135         }
136     }
137
138     public static Long JavaDoc getLong(long val) {
139
140         synchronized (longPool) {
141             return longPool.getOrAddLong(val);
142         }
143     }
144
145     public static Double JavaDoc getDouble(long val) {
146
147         synchronized (doublePool) {
148             return doublePool.getOrAddDouble(val);
149         }
150     }
151
152     public static String JavaDoc getString(String JavaDoc val) {
153
154         if (val == null || val.length() > maxStringLength) {
155             return val;
156         }
157
158         synchronized (stringPool) {
159             return stringPool.getOrAddString(val);
160         }
161     }
162
163     public static Date JavaDoc getDate(long val) {
164
165         synchronized (datePool) {
166             return datePool.getOrAddDate(val);
167         }
168     }
169
170     public static BigDecimal JavaDoc getBigDecimal(BigDecimal JavaDoc val) {
171
172         if (val == null) {
173             return val;
174         }
175
176         synchronized (bigdecimalPool) {
177             return (BigDecimal JavaDoc) bigdecimalPool.getOrAddObject(val);
178         }
179     }
180
181     public static Boolean JavaDoc getBoolean(boolean b) {
182         return b ? Boolean.TRUE
183                  : Boolean.FALSE;
184     }
185 }
186
Popular Tags