1 package org.shiftone.cache.util; 2 3 4 5 import java.util.Arrays ; 6 import java.util.Hashtable ; 7 import java.util.Map ; 8 9 10 14 public class MapFactory 15 { 16 17 private static final Log LOG = new Log(MapFactory.class); 18 public static final float DEFAULT_LOAD_FACTOR = 0.5f; 19 public static final int[] PRIMES = 20 { 21 22 167, 191, 223, 251, 281, 313, 349, 389, 433, 487, 541, 601, 673, 751, 839, 937, 1049, 1171, 1301, 1447, 1607, 1787, 1987, 2207, 2459, 2731, 3037, 3373, 3761, 4177, 4637, 5153, 5737, 6373, 7079, 7867, 8737, 9719, 10789, 11981, 13309, 14779, 16411, 18217, 20231, 22469, 24943, 27689, 30757, 34141, 37897, 42071, 46703, 51853, 57557, 63901, 70937, 78779, 87473, 97103, 107791, 119653, 132817, 147449, 163673, 181693, 201683, 223903, 248533, 275881, 306239, 339943, 377339, 418849, 464923, 516077, 572867, 635891, 705841, 783487, 869683, 965357, 1071563, 1189453, 1320301, 1465547, 1626763, 1805729, 2004377, 2224861, 2469629, 2741303, 3042857, 3377579, 3749117, 4161527, 4619309, 5127433, 5691457, 6317527, 7012469, 7783843, 8640109, 9590531, 10645507, 11816521, 13116343, 14559151, 16160663, 17938357, 19911581, 22101889, 24533099, 27231751, 30227287, 33552293, 37243051, 41339843, Integer.MAX_VALUE 34 }; 35 36 public static int getNearPrime(int number) 37 { 38 39 int value = Arrays.binarySearch(PRIMES, number); 40 41 if (value < 0) 42 { 43 value = -value - 1; 44 } 45 46 return PRIMES[value]; 47 } 48 49 50 public static Map createMap(int initialCapacity) 51 { 52 return new Hashtable (getNearPrime(initialCapacity), DEFAULT_LOAD_FACTOR); 53 } 54 55 56 private static void test(int a) 57 { 58 59 int b = getNearPrime(a); 60 61 LOG.info(a + " -> " + b); 62 } 63 64 65 public static void main(String args[]) 66 { 67 68 test(0); 69 test(965356); 70 test(965357); 71 test(965358); 72 test(Integer.MAX_VALUE); 73 } 74 } 75 | Popular Tags |