1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.internal.reflect;
19
20class ProxyObjectCache {
21    Object keyTable[];
22
23    int valueTable[];
24
25    int elementSize;
26
27    int threshold;
28
29    ProxyObjectCache(int initialCapacity) {
30        if (initialCapacity < 13) {
31            initialCapacity = 13;
32        }
33        this.elementSize = 0;
34        this.threshold = (int) (initialCapacity * 0.66f);
35        this.keyTable = new Object[initialCapacity];
36        this.valueTable = new int[initialCapacity];
37    }
38
39    int get(Object key) {
40        int index = hashCode(key);
41        while (keyTable[index] != null) {
42            if (keyTable[index].equals(key)) {
43                return valueTable[index];
44            }
45            index = (index + 1) % keyTable.length;
46        }
47        return -1;
48    }
49
50    int hashCode(Object key) {
51        return (key.hashCode() & 0x7FFFFFFF) % keyTable.length;
52    }
53
54    int put(Object key, int value) {
55        int index = hashCode(key);
56        while (keyTable[index] != null) {
57            if (keyTable[index].equals(key)) {
58                return valueTable[index] = value;
59            }
60            index = (index + 1) % keyTable.length;
61        }
62        keyTable[index] = key;
63        valueTable[index] = value;
64
65        // assumes the threshold is never equal to the size of the table
66        if (++elementSize > threshold) {
67            rehash();
68        }
69        return value;
70    }
71
72    private void rehash() {
73        ProxyObjectCache newHashtable = new ProxyObjectCache(
74                keyTable.length * 2);
75        for (int i = keyTable.length; --i >= 0;) {
76            if (keyTable[i] != null) {
77                newHashtable.put(keyTable[i], valueTable[i]);
78            }
79        }
80
81        this.keyTable = newHashtable.keyTable;
82        this.valueTable = newHashtable.valueTable;
83        this.threshold = newHashtable.threshold;
84    }
85
86    int size() {
87        return elementSize;
88    }
89
90    @Override
91    public String toString() {
92        int max = size();
93        StringBuilder buf = new StringBuilder();
94        buf.append("{"); //$NON-NLS-1$
95        for (int i = 0; i < max; ++i) {
96            if (keyTable[i] != null) {
97                buf.append(keyTable[i]).append("->").append(valueTable[i]); //$NON-NLS-1$
98            }
99            if (i < max) {
100                buf.append(", "); //$NON-NLS-1$
101            }
102        }
103        buf.append("}"); //$NON-NLS-1$
104        return buf.toString();
105    }
106}
107