2 *******************************************************************************
3 * Copyright (C) 1996-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.impl;
12 public class CalendarCache
17 public CalendarCache() {
18 makeArrays(arraySize);
21 private void makeArrays(int newSize) {
22 keys = new long[newSize];
23 values = new long[newSize];
25 for (int i = 0; i < newSize; i++) {
29 threshold = (int)(arraySize * 0.75);
36 public synchronized long get(long key) {
37 return values[findIndex(key)];
43 public synchronized void put(long key, long value)
45 if (size >= threshold) {
48 int index = findIndex(key);
51 values[index] = value;
55 private final int findIndex(long key) {
56 int index = hash(key);
59 while (values[index] != EMPTY && keys[index] != key)
64 index = (index + delta) % arraySize;
71 int oldSize = arraySize;
72 long[] oldKeys = keys;
73 long[] oldValues = values;
75 if (pIndex < primes.length - 1) {
76 arraySize = primes[++pIndex];
78 arraySize = arraySize * 2 + 1;
82 makeArrays(arraySize);
83 for (int i = 0; i < oldSize; i++) {
84 if (oldValues[i] != EMPTY) {
85 put(oldKeys[i], oldValues[i]);
92 * Produce a uniformly-distributed hash value from an integer key.
93 * This is essentially a linear congruential random number generator
94 * that uses the key as its seed value.
96 private final int hash(long key)
98 int h = (int)((key * 15821 + 1) % arraySize);
105 private final int hash2(long key) {
106 return arraySize - 2 - (int)(key % (arraySize-2) );
109 static private final int primes[] = { // 5, 17, 31, 47, // for testing
110 61, 127, 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521,
114 private int pIndex = 0;
115 private int size = 0;
116 private int arraySize = primes[pIndex];
117 private int threshold = (arraySize * 3) / 4;
119 private long[] keys = new long[arraySize];
120 private long[] values = new long[arraySize];
125 static public long EMPTY = Long.MIN_VALUE;