KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > orbutil > CacheTable


1 /*
2  * @(#)CacheTable.java 1.16 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.orbutil;
9 import org.omg.CORBA.INTERNAL JavaDoc;
10 import org.omg.CORBA.CompletionStatus JavaDoc;
11
12 import com.sun.corba.se.spi.logging.CORBALogDomains;
13 import com.sun.corba.se.spi.orb.ORB;
14
15 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
16
17 public class CacheTable {
18     class Entry {
19         java.lang.Object JavaDoc key;
20         int val;
21         Entry next; // this chains the collision list of table "map"
22
Entry rnext; // this chains the collision list of table "rmap"
23
public Entry(java.lang.Object JavaDoc k, int v) {
24             key = k;
25             val = v;
26             next = null;
27         rnext = null;
28         }
29     }
30     private boolean noReverseMap;
31     // size must be power of 2
32
static final int INITIAL_SIZE = 16;
33     static final int MAX_SIZE = 1 << 30;
34     int size;
35     int entryCount;
36     private Entry [] map;
37     private Entry [] rmap;
38       
39     private ORB orb;
40     private ORBUtilSystemException wrapper;
41
42     private CacheTable() {}
43     public CacheTable(ORB orb, boolean u) {
44     //System.out.println("using new cache table");
45
this.orb = orb;
46         wrapper = ORBUtilSystemException.get(orb,
47             CORBALogDomains.RPC_ENCODING);
48     noReverseMap = u;
49         size = INITIAL_SIZE;
50     entryCount = 0;
51     initTables();
52     }
53     private void initTables() {
54     map = new Entry[size];
55     rmap = noReverseMap ? null : new Entry[size];
56     }
57     private void grow() {
58     if (size == MAX_SIZE)
59         return;
60     Entry [] oldMap = map;
61         int oldSize = size;
62     size <<= 1;
63     initTables();
64     // now rehash the entries into the new table
65
for (int i = 0; i < oldSize; i++) {
66         for (Entry e = oldMap[i]; e != null; e = e.next)
67             put_table(e.key, e.val);
68     }
69     }
70     private int moduloTableSize(int h) {
71     // these are the "supplemental hash function" copied from
72
// java.util.HashMap, supposed to be "critical"
73
h += ~(h << 9);
74         h ^= (h >>> 14);
75         h += (h << 4);
76         h ^= (h >>> 10);
77     return h & (size - 1);
78     }
79     private int hash(java.lang.Object JavaDoc key) {
80     return moduloTableSize(System.identityHashCode(key));
81     }
82     private int hash(int val) {
83     return moduloTableSize(val);
84     }
85     public final void put(java.lang.Object JavaDoc key, int val) {
86     if (put_table(key, val)) {
87         entryCount++;
88         if (entryCount > size * 3 / 4)
89         grow();
90     }
91     }
92     private boolean put_table(java.lang.Object JavaDoc key, int val) {
93     int index = hash(key);
94     for (Entry e = map[index]; e != null; e = e.next) {
95         if (e.key == key) {
96             if (e.val != val) {
97                     throw wrapper.duplicateIndirectionOffset();
98         }
99         // if we get here we are trying to put in the same key/val pair
100
// this is a no-op, so we just return
101
return false;
102         }
103         }
104     // this means the key is not present in our table
105
// then it shouldnt be present in our reverse table either
106
Entry newEntry = new Entry(key, val);
107     newEntry.next = map[index];
108     map[index] = newEntry;
109     if (!noReverseMap) {
110         int rindex = hash(val);
111         newEntry.rnext = rmap[rindex];
112         rmap[rindex] = newEntry;
113         }
114     return true;
115     }
116     public final boolean containsKey(java.lang.Object JavaDoc key) {
117     return (getVal(key) != -1);
118     }
119     public final int getVal(java.lang.Object JavaDoc key) {
120     int index = hash(key);
121         for (Entry e = map[index]; e != null; e = e.next) {
122             if (e.key == key)
123                 return e.val;
124         }
125         return -1;
126     }
127     public final boolean containsVal(int val) {
128     return (getKey(val) != null);
129     }
130     public final boolean containsOrderedVal(int val) {
131     return containsVal(val);
132     }
133     public final java.lang.Object JavaDoc getKey(int val) {
134     int index = hash(val);
135         for (Entry e = rmap[index]; e != null; e = e.rnext) {
136             if (e.val == val)
137                 return e.key;
138         }
139         return null;
140     }
141     public void done() {
142     map = null;
143         rmap = null;
144     }
145 }
146
Popular Tags