1 30 31 32 package org.hsqldb.store; 33 34 import java.math.BigDecimal ; 35 import java.sql.Date ; 36 37 50 public class ValuePool { 51 52 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 static ValuePoolHashMap[] poolList; 71 72 static int maxStringLength; 74 75 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 getInt(int val) { 132 133 synchronized (intPool) { 134 return intPool.getOrAddInteger(val); 135 } 136 } 137 138 public static Long getLong(long val) { 139 140 synchronized (longPool) { 141 return longPool.getOrAddLong(val); 142 } 143 } 144 145 public static Double getDouble(long val) { 146 147 synchronized (doublePool) { 148 return doublePool.getOrAddDouble(val); 149 } 150 } 151 152 public static String getString(String 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 getDate(long val) { 164 165 synchronized (datePool) { 166 return datePool.getOrAddDate(val); 167 } 168 } 169 170 public static BigDecimal getBigDecimal(BigDecimal val) { 171 172 if (val == null) { 173 return val; 174 } 175 176 synchronized (bigdecimalPool) { 177 return (BigDecimal ) bigdecimalPool.getOrAddObject(val); 178 } 179 } 180 181 public static Boolean getBoolean(boolean b) { 182 return b ? Boolean.TRUE 183 : Boolean.FALSE; 184 } 185 } 186 | Popular Tags |