KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > IntMap


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * IntMap.java
21  *
22  * Created on March 29, 2004, 6:40 PM
23  */

24
25 package org.netbeans.core.output2;
26
27 import java.util.Arrays JavaDoc;
28 import org.openide.util.Exceptions;
29
30 /**
31  * Sparse array integer keyed map. Similar to a standard Collections map,
32  * but considerably more efficient for this purpose, it simply an array
33  * if integer indices that have values and an array of objects mapped to
34  * those indices. Entries may be added only in ascending order, enabling
35  * use of Arrays.binarySearch() to quickly locate the relevant entry.
36  * <p>
37  * Used to maintain the mapping between the (relatively few) OutputListeners
38  * and their associated getLine numbers.
39  *
40  * @author Tim Boudreau
41  */

42 final class IntMap {
43     private int[] keys = new int[] { Integer.MAX_VALUE, Integer.MAX_VALUE,
44         Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE};
45         
46     private Object JavaDoc[] vals = new Object JavaDoc[5];
47     private int last = -1;
48     
49     /** Creates a new instance of IntMap */
50     IntMap() {
51     }
52     
53     public int first() {
54         return isEmpty() ? -1 : keys[0];
55     }
56     
57     public int nearest (int line, boolean backward) {
58         if (isEmpty()) {
59             return -1;
60         }
61         if (last == 0) {
62             return keys[last];
63         }
64         if (line < keys[0]) {
65             return backward ? keys[last] : keys[0];
66         }
67         if (line > keys[last]) {
68             return backward ? keys[last] : keys[0];
69         }
70         int idx = Arrays.binarySearch (keys, line);
71         if (idx < 0 && last > 0) {
72             for (int i=1; i <= last; i++) {
73                 if (keys[i-1] < line && keys[i] > line) {
74                     idx = i;
75                     break;
76                 }
77             }
78             return backward ? keys[idx-1] : keys[idx];
79         } else {
80             if (backward) {
81                 idx = idx == 0 ? last : idx - 1;
82             } else {
83                 idx = idx == last ? 0 : idx + 1;
84             }
85             return keys[idx];
86         }
87     }
88
89     public int[] getKeys () {
90         if (last == -1) {
91             return new int[0];
92         }
93         if (last == keys.length -1) {
94             growArrays();
95         }
96         int[] result = new int[last+1];
97         try {
98             System.arraycopy (keys, 0, result, 0, last+1);
99             return result;
100         } catch (ArrayIndexOutOfBoundsException JavaDoc aioobe) { //XXX temp diagnostics
101
ArrayIndexOutOfBoundsException JavaDoc e = new ArrayIndexOutOfBoundsException JavaDoc (
102                 "AIOOBE in IntMap.getKeys() - last = " + last + " keys: " +
103                 i2s(keys) + " vals: " + Arrays.asList(vals) + " result length "
104                 + result.length);
105             Exceptions.printStackTrace(e);
106             return new int[0];
107         }
108     }
109
110     /** Some temporary diagnostics re issue 48608 */
111     private static String JavaDoc i2s (int[] arr) {
112         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(arr.length * 3);
113         sb.append ('[');
114         for (int i=0; i < arr.length; i++) {
115             if (arr[i] != Integer.MAX_VALUE) {
116                 sb.append (arr[i]);
117                 sb.append (',');
118             }
119         }
120         sb.append (']');
121         return sb.toString();
122     }
123     
124     public Object JavaDoc get (int key) {
125         int idx = Arrays.binarySearch (keys, key);
126         if (idx > -1 && idx <= last) {
127             return vals[idx];
128         }
129         return null;
130     }
131     
132     public void put (int key, Object JavaDoc val) {
133         if (last > 0) {
134             assert key > keys[last]: "key=" + key + " last=" + keys[last];
135         }
136         if (last == keys.length - 1) {
137             growArrays();
138         }
139         last++;
140         keys[last] = key;
141         vals[last] = val;
142     }
143     
144     private void growArrays() {
145         int newSize = last * 2;
146         int[] newKeys = new int[newSize];
147         Object JavaDoc[] newVals = new Object JavaDoc[newSize];
148         Arrays.fill (newKeys, Integer.MAX_VALUE); //So binarySearch works
149
System.arraycopy (keys, 0, newKeys, 0, keys.length);
150         System.arraycopy (vals, 0, newVals, 0, vals.length);
151         keys = newKeys;
152         vals = newVals;
153     }
154     
155     /**
156      * Get the key which follows the passed key, or -1. Will wrap around 0.
157      */

158     public int nextEntry (int entry) {
159         int result = -1;
160         if (!isEmpty()) {
161             int idx = Arrays.binarySearch (keys, entry);
162             if (idx >= 0) {
163                 result = idx == keys.length -1 ? keys[0] : keys[idx+1];
164             }
165         }
166         return result;
167     }
168     
169     /**
170      * Get the key which precedes the passed key, or -1. Will wrap around 0.
171      */

172     public int prevEntry (int entry) {
173         int result = -1;
174         if (!isEmpty()) {
175             int idx = Arrays.binarySearch (keys, entry);
176             if (idx >= 0) {
177                 result = idx == 0 -1 ? keys[keys.length-1] : keys[idx-1];
178             }
179         }
180         return result;
181     }
182     
183     
184     public boolean isEmpty() {
185         return last == -1;
186     }
187     
188     public int size() {
189         return last + 1;
190     }
191     
192     public String JavaDoc toString() {
193         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("IntMap@") //NOI18N
194
.append(System.identityHashCode(this));
195         
196         for (int i=0; i < size(); i++) {
197             sb.append ("["); //NOI18N
198
sb.append (keys[i]);
199             sb.append (":"); //NOI18N
200
sb.append (vals[i]);
201             sb.append ("]"); //NOI18N
202
}
203         if (size() == 0) {
204             sb.append ("empty"); //NOI18N
205
}
206         return sb.toString();
207     }
208 }
209
Popular Tags