KickJava   Java API By Example, From Geeks To Geeks.

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


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/LongSortedArrayListBucket.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:13 $
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 /*
56  * A simple test program populated an ArrayListBucket with 20 entries. Then it performed
57  * 50,000,000 get(int key) took 32.968 seconds!! 60% of the time the key that was searched
58  * was not in the bucket and hence had to search the maximum.
59  */

60 public class LongSortedArrayListBucket
61     implements Bucket
62 {
63
64     protected ArrayList JavaDoc entries;
65     
66     LongSortedArrayListBucket() {
67         entries = new ArrayList JavaDoc();
68     }
69     
70     public Object JavaDoc put(int searchKey, Object JavaDoc object) {
71         return put((long) searchKey, object);
72     }
73     
74     public Object JavaDoc put(long searchKey, Object JavaDoc object) {
75         int low = 0, high = entries.size()-1, mid;
76         int entryKey = 0;
77         IntEntry entry = null;
78         while (low <= high) {
79             mid = (low + high) / 2;
80             entry = (IntEntry) entries.get(mid);
81             entryKey = entry.key;
82             if (entryKey == searchKey) {
83                 Object JavaDoc oldObject = entry.object;
84                 entry.object = object;
85                 return oldObject;
86             } else if (searchKey < entryKey) {
87                 high = mid - 1;
88             } else {
89                 low = mid + 1;
90             }
91         }
92             
93         //totalEntries++;
94
//System.out.println("**Inserting key: " + searchKey + " at Lo: " + low);
95
entries.add(low, new LongEntry(searchKey, object));
96         return null;
97     }
98         
99     public Object JavaDoc get(int searchKey) {
100         return get((long) searchKey);
101     }
102     
103     public Object JavaDoc get(long searchKey) {
104         int low = 0, high = entries.size()-1, mid;
105         long entryKey = 0;
106         LongEntry entry = null;
107         while (low <= high) {
108             mid = (low + high) / 2;
109             entry = (LongEntry) entries.get(mid);
110             entryKey = entry.key;
111             if (entryKey == searchKey) {
112                 return entry.object;
113             } else if (searchKey < entryKey) {
114                 high = mid - 1;
115             } else {
116                 low = mid + 1;
117             }
118         }
119         return null;
120     }
121         
122     public Object JavaDoc remove(int searchKey) {
123         return remove((long) searchKey);
124     }
125     
126     public Object JavaDoc remove(long searchKey) {
127         int low = 0, high = entries.size()-1, mid;
128         int entryKey = 0;
129         IntEntry entry = null;
130
131         while (low <= high) {
132             mid = (low + high) / 2;
133             entry = (IntEntry) entries.get(mid);
134             entryKey = entry.key;
135             if (entryKey == searchKey) {
136                 entries.remove(mid);
137                 //totalEntries--;
138
return entry.object;
139             } else if (searchKey < entryKey) {
140                 high = mid - 1;
141             } else {
142                 low = mid + 1;
143             }
144         }
145         return null;
146     }
147     
148     public boolean containsKey(int searchKey) {
149         return (get((long) searchKey) != null);
150     }
151
152     public boolean containsKey(long searchKey) {
153         return (get(searchKey) != null);
154     }
155     
156     public int size() {
157         return entries.size();
158     }
159
160     public Iterator JavaDoc iterator() {
161         return new BucketIterator(entries, false);
162     }
163
164     public Iterator JavaDoc entryIterator() {
165         return new BucketIterator(entries, true);
166     }
167
168     private class BucketIterator
169         implements java.util.Iterator JavaDoc
170     {
171         ArrayList JavaDoc entries;
172         int index = 0;
173         boolean iterateEntry;
174         
175         BucketIterator(ArrayList JavaDoc entries, boolean iterateEntry) {
176             this.entries = entries;
177             this.index = 0;
178             this.iterateEntry = iterateEntry;
179         }
180         
181         public boolean hasNext() {
182             return (index < entries.size());
183         }
184         
185         public Object JavaDoc next() {
186             return (iterateEntry ? entries.get(index++) : ((IntEntry) entries.get(index++)).object);
187         }
188         
189         public void remove() {
190             
191         }
192         
193     }
194         
195     
196         
197 }
198
199
Popular Tags