KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > util > LongHashMap


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.containers.util;
25
26 import java.io.*;
27 import java.util.*;
28 import java.util.ArrayList JavaDoc;
29 import java.security.*;
30
31
32 public class LongHashMap {
33
34     protected static final int DEFAULT_CAPACITY = 128;
35     protected static final float DEFAULT_LOAD_FACTOR = 0.75f;
36     protected static final int MAXIMUM_CAPACITY = 1 << 30;
37
38     protected static boolean debug = false;
39
40     protected transient Entry[] table;
41     protected float loadFactor;
42     protected transient int size;
43     protected int bucketmask;
44     protected int capacity;
45     protected int threshold;
46
47     public LongHashMap() {
48         this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
49     }
50
51     public LongHashMap(int initialCapacity) {
52        this(initialCapacity, DEFAULT_LOAD_FACTOR);
53     }
54
55     public LongHashMap(int initialCapacity, float loadFactor) {
56         if (initialCapacity > MAXIMUM_CAPACITY) {
57             initialCapacity = MAXIMUM_CAPACITY;
58         }
59         if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
60             throw new IllegalArgumentException JavaDoc("Illegal load factor: "
61                                                + loadFactor);
62         }
63
64         // Find a power of 2 >= initialCapacity
65
capacity = 1;
66         
67         while (capacity < initialCapacity) {
68             capacity <<= 1;
69         }
70         this.loadFactor = loadFactor;
71         this.threshold = (int)(capacity * loadFactor);
72         this.table = new Entry[capacity];
73         this.bucketmask = capacity - 1;
74     }
75     
76
77     public boolean contains(long key) {
78         int index = ((int) key) & bucketmask;
79         for (Entry e = table[index]; e != null; e = e.next) {
80             if (e.key == key) {
81                 return true;
82             }
83         }
84         return false;
85     }
86     
87     public Object JavaDoc get(long key) {
88         int index = ((int) key) & bucketmask;
89         for (Entry e = table[index]; e != null; e = e.next) {
90             if (e.key == key) {
91                 return e.value;
92             }
93         }
94         return null;
95     }
96     
97     public Object JavaDoc put(long key, Object JavaDoc value) {
98         int index = ((int) key) & bucketmask;
99         
100         for (Entry e = table[index]; e != null; e = e.next) {
101             if (e.key == key) {
102                 Object JavaDoc oldValue = e.value;
103                 e.value = value;
104                 return oldValue;
105             }
106         }
107         
108         table[index] = new Entry(key, value, table[index]);
109         if (size++ >= threshold) {
110             int newCapacity = 2 * capacity;
111             Entry[] newTable = new Entry[newCapacity];
112             transfer(newTable);
113             table = newTable;
114             capacity = newCapacity;
115             threshold = (int)(newCapacity * loadFactor);
116             bucketmask = capacity-1;
117         }
118         return null;
119     }
120     
121     /**
122      * Transfer all entries from current table to newTable.
123      */

124     private void transfer(Entry[] newTable) {
125         Entry[] src = table;
126         int newCapacity = newTable.length;
127         int bucketmask = newCapacity-1;
128         for (int j = 0; j < src.length; j++) {
129             Entry e = src[j];
130             if (e != null) {
131                 src[j] = null;
132                 do {
133                     Entry next = e.next;
134                     int index = ((int) e.key) & bucketmask;
135                     e.next = newTable[index];
136                     newTable[index] = e;
137                     e = next;
138                 } while (e != null);
139             }
140         }
141     }
142     
143     public Object JavaDoc remove(long key) {
144         int index = ((int) key) & bucketmask;
145         Entry prev = table[index];
146         Entry e = prev;
147         
148         while (e != null) {
149             Entry next = e.next;
150             if (e.key == key) {
151                 size--;
152                 if (prev == e) {
153                     table[index] = next;
154                 } else {
155                     prev.next = next;
156                 }
157                 return e.value;
158             }
159             prev = e;
160             e = next;
161         }
162         
163         return null;
164     }
165     
166     public Enumeration elements() {
167         Vector keyList = new Vector();
168         for (int index=0; index<capacity; index++) {
169             for (Entry e = table[index]; e != null; e = e.next) {
170                 keyList.addElement(e.value);
171             }
172         }
173         
174         return keyList.elements();
175     }
176     
177     public Iterator values() {
178         ArrayList JavaDoc keyList = new ArrayList JavaDoc();
179         for (int index=0; index<capacity; index++) {
180             for (Entry e = table[index]; e != null; e = e.next) {
181                 keyList.add(e.value);
182             }
183         }
184         
185         return keyList.iterator();
186     }
187     
188     public Iterator keys() {
189         ArrayList JavaDoc keyList = new ArrayList JavaDoc();
190         for (int index=0; index<capacity; index++) {
191             for (Entry e = table[index]; e != null; e = e.next) {
192                 keyList.add(new Long JavaDoc(e.key));
193             }
194         }
195         
196         return keyList.iterator();
197     }
198     
199     static class Entry {
200         final long key;
201         Object JavaDoc value;
202         Entry next;
203         
204         /**
205          * Create new entry.
206          */

207         Entry(long k, Object JavaDoc v, Entry n) {
208             key = k;
209             value = v;
210             next = n;
211         }
212         
213         public int hashCode() {
214             return ((int) key);
215         }
216         
217         public String JavaDoc toString() {
218             return key + "=" + value;
219         }
220     }
221 }
222
Popular Tags