001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.internal.reflect;
019:
020: class ProxyObjectCache {
021: Object keyTable[];
022:
023: int valueTable[];
024:
025: int elementSize;
026:
027: int threshold;
028:
029: ProxyObjectCache(int initialCapacity) {
030: if (initialCapacity < 13) {
031: initialCapacity = 13;
032: }
033: this .elementSize = 0;
034: this .threshold = (int) (initialCapacity * 0.66f);
035: this .keyTable = new Object[initialCapacity];
036: this .valueTable = new int[initialCapacity];
037: }
038:
039: int get(Object key) {
040: int index = hashCode(key);
041: while (keyTable[index] != null) {
042: if (keyTable[index].equals(key)) {
043: return valueTable[index];
044: }
045: index = (index + 1) % keyTable.length;
046: }
047: return -1;
048: }
049:
050: int hashCode(Object key) {
051: return (key.hashCode() & 0x7FFFFFFF) % keyTable.length;
052: }
053:
054: int put(Object key, int value) {
055: int index = hashCode(key);
056: while (keyTable[index] != null) {
057: if (keyTable[index].equals(key)) {
058: return valueTable[index] = value;
059: }
060: index = (index + 1) % keyTable.length;
061: }
062: keyTable[index] = key;
063: valueTable[index] = value;
064:
065: // assumes the threshold is never equal to the size of the table
066: if (++elementSize > threshold) {
067: rehash();
068: }
069: return value;
070: }
071:
072: private void rehash() {
073: ProxyObjectCache newHashtable = new ProxyObjectCache(
074: keyTable.length * 2);
075: for (int i = keyTable.length; --i >= 0;) {
076: if (keyTable[i] != null) {
077: newHashtable.put(keyTable[i], valueTable[i]);
078: }
079: }
080:
081: this .keyTable = newHashtable.keyTable;
082: this .valueTable = newHashtable.valueTable;
083: this .threshold = newHashtable.threshold;
084: }
085:
086: int size() {
087: return elementSize;
088: }
089:
090: @Override
091: public String toString() {
092: int max = size();
093: StringBuilder buf = new StringBuilder();
094: buf.append("{"); //$NON-NLS-1$
095: for (int i = 0; i < max; ++i) {
096: if (keyTable[i] != null) {
097: buf.append(keyTable[i])
098: .append("->").append(valueTable[i]); //$NON-NLS-1$
099: }
100: if (i < max) {
101: buf.append(", "); //$NON-NLS-1$
102: }
103: }
104: buf.append("}"); //$NON-NLS-1$
105: return buf.toString();
106: }
107: }
|