KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > collection > IntHashMap


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 //NOTE: Tabs are used instead of spaces for indentation.
25
// Make sure that your editor does not replace tabs with spaces.
26
// Set the tab length using your favourite editor to your
27
// visual preference.
28

29 /*
30  * Filename: TooManyTasksException.java
31  *
32  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
33  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
34  * All rights reserved.
35  *
36  * This software is the confidential and proprietary information
37  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
38  * You shall not disclose such Confidential Information and shall
39  * use it only in accordance with the terms of the license
40  * agreement you entered into with iPlanet/Sun Microsystems.
41  */

42  
43 /**
44  * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/collection/IntHashMap.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:12 $
47  */

48  
49 package com.sun.enterprise.util.collection;
50
51 import java.util.HashMap JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.Iterator JavaDoc;
54
55 public class IntHashMap {
56     
57     int maxBuckets = 0;
58     Bucket[] buckets;
59     
60     public IntHashMap() {
61         this(89);
62     }
63     
64     public IntHashMap(int maxBuckets) {
65         this.maxBuckets = maxBuckets;
66         buckets = new Bucket[maxBuckets];
67         for (int i=0; i<maxBuckets; i++) {
68             buckets[i] = new SortedArrayListBucket();
69         }
70     }
71     
72     public IntHashMap(Bucket[] buckets) {
73         this.buckets = buckets;
74         this.maxBuckets = buckets.length;
75     }
76     
77     public void put(int key, Object JavaDoc object) {
78         int index = Math.abs(key % maxBuckets);
79         buckets[index].put(key, object);
80     }
81     
82     public Object JavaDoc get(int key) {
83         int index = Math.abs(key % maxBuckets);
84         return buckets[index].get(key);
85     }
86     
87     public boolean containsKey(int key) {
88         return ( (null!=get(key) ? true: false));
89     }
90     
91     public Object JavaDoc remove(int key) {
92         int index = Math.abs(key % maxBuckets);
93         return buckets[index].remove(key);
94     }
95     
96     public void print() {
97         for (int i=0; i<maxBuckets; i++) {
98             System.out.println("Bucket[" + i + "]: " + buckets[i]);
99         }
100     }
101
102     public IntHashMapIterator iterator() {
103         return new IntHashMapIterator();
104     }
105
106     private class IntHashMapIterator
107         implements Iterator JavaDoc
108     {
109         int bucketIndex = 0;
110         Iterator JavaDoc iter = null;
111
112         IntHashMapIterator() {
113             iter = buckets[0].iterator();
114         }
115
116         public boolean hasNext() {
117             if (iter.hasNext()) {
118                 return true;
119             }
120
121             while (++bucketIndex < maxBuckets) {
122                 iter = buckets[bucketIndex].iterator();
123                 if (iter.hasNext()) {
124                     return true;
125                 }
126             }
127
128             return false;
129         }
130
131         public Object JavaDoc next() {
132             return iter.next();
133         }
134
135         public void remove() {
136         }
137     }
138     
139     public static void main(String JavaDoc[] args) {
140         
141         int count = 20;
142         long time=0, t1=0, t2 = 0;
143         String JavaDoc data = "SomeData_";
144
145         IntHashMap map = new IntHashMap();
146         for (int i=0; i<count; i+= 5) {
147             map.put(i, data + i);
148         }
149         
150         for (int i=1; i<count; i+= 3) {
151             map.put(i, data + i);
152         }
153         
154         for (int i=3; i<count; i+= 4) {
155             map.put(i, data + i);
156         }
157         
158         for (int i=-23; i<count; i+= 4) {
159             map.put(i, data + i);
160         }
161         
162         for (int j=-25; j<25; j++) {
163             System.out.println("Key: " + j + "; val: " + map.get(j));
164         }
165         t2 = System.currentTimeMillis();
166
167
168         Iterator JavaDoc iter = map.iterator();
169         while (iter.hasNext()) {
170             System.out.println("Got: " + iter.next());
171         }
172         
173     }
174
175 }
176
Popular Tags