KickJava   Java API By Example, From Geeks To Geeks.

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


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  * PairMap.java
21  *
22  * Created on May 12, 2004, 10:11 PM
23  */

24
25 package org.netbeans.core.output2;
26
27 import java.lang.ref.WeakReference JavaDoc;
28 import java.util.Arrays JavaDoc;
29
30 /**
31  * A synchronized LIFO map that can contain duplicate keys, and can be
32  * toggled between using weak and strong references for values. Used for
33  * mapping instances of NbIO to the names used in NbIOProvider.getIO().
34  *
35  * @author Tim Boudreau
36  */

37 class PairMap {
38     //XXX weak referencing of objects no longer used - delete?
39

40     private String JavaDoc [] keys = new String JavaDoc[10];
41     /** package private for unit tests */
42     Object JavaDoc [] vals = new Object JavaDoc[10];
43     int last = -1;
44     private boolean weak = false;
45     
46     /** Creates a new instance of PairMap */
47     PairMap() {
48     }
49     
50     public synchronized void clear() {
51         keys = new String JavaDoc[10];
52         vals = new Object JavaDoc[10];
53         last = -1;
54     }
55     
56     public synchronized int size() {
57         if (weak) {
58             return prune();
59         } else {
60             return last+1;
61         }
62     }
63     
64     public synchronized boolean isEmpty() {
65         return size() == 0;
66     }
67     
68     public synchronized void setWeak(boolean val) {
69         if (weak != val) {
70             weak = val;
71             for (int i=0; i <= last; i++) {
72                 if (weak) {
73                     vals[i] = new WeakReference JavaDoc(vals[i]);
74                 } else {
75                     vals[i] = ((WeakReference JavaDoc) vals[i]).get();
76                 }
77             }
78             if (!weak) {
79                 prune();
80             }
81         }
82     }
83     
84     public synchronized void add (String JavaDoc key, NbIO value) {
85         if (last == keys.length -1) {
86             growArrays();
87         }
88         last++;
89         keys[last] = key;
90         vals[last] = value;
91     }
92     
93     public synchronized NbIO get (String JavaDoc key, boolean mustBeClosed) {
94         if (last < 0) {
95             return null;
96         }
97         boolean foundNull = false;
98         for (int i=last; i >= 0; i--) {
99             if (keys[i].equals(key)) {
100                 NbIO io = getValue(i);
101                 foundNull |= io == null;
102                 if (io != null && (!mustBeClosed || (mustBeClosed && io.isStreamClosed()))) {
103                     return io;
104                 }
105             }
106         }
107         if (foundNull) {
108             prune();
109         }
110         return null;
111     }
112     
113     public boolean containsValue(NbIO io) {
114         if (last < 0) {
115             return false;
116         }
117         for (int i=last; i >= 0; i--) {
118             if (getValue(i) == io) {
119                 return true;
120             }
121         }
122         return false;
123     }
124     
125     public synchronized NbIO get (String JavaDoc key) {
126         return get(key, false);
127     }
128     
129     public synchronized String JavaDoc remove (NbIO io) {
130         int idx = indexOfVal (io);
131         if (idx == -1) {
132             return null;
133         }
134         String JavaDoc result = keys[idx];
135         removeIndex(idx);
136         return result;
137     }
138     
139     public synchronized NbIO remove (String JavaDoc key) {
140         int idx = indexOfKey(key);
141         if (idx == -1) {
142             return null;
143         }
144         NbIO result = getValue(idx);
145         removeIndex(idx);
146         return result;
147     }
148     
149     private NbIO getValue(int idx) {
150         if (idx > last) {
151             throw new ArrayIndexOutOfBoundsException JavaDoc ("Tried to fetch item " + //NOI18N
152
idx + " but map only contains " + (last + 1) + " elements"); //NOI18N
153
}
154         NbIO result;
155         Object JavaDoc o = vals[idx];
156         if (weak) {
157             result = (NbIO) ((WeakReference JavaDoc) vals[idx]).get();
158             if (result == null && !pruning) {
159                 removeIndex(idx);
160             }
161
162         } else {
163             result = (NbIO) o;
164         }
165         return result;
166     }
167     
168     public void setValue (int idx, NbIO value) {
169         if (weak) {
170             vals[idx] = new WeakReference JavaDoc(value);
171         } else {
172             vals[idx] = value;
173         }
174     }
175     
176     private void removeIndex (int idx) {
177         if (idx < 0 || idx > last) {
178             throw new ArrayIndexOutOfBoundsException JavaDoc ("Trying to remove " + //NOI18N
179
"element " + idx + " but map only contains " + (last+1) + //NOI18N
180
" elements"); //NOI18N
181
}
182         if (idx == last) {
183             keys[idx] = null;
184             vals[idx] = null;
185         } else {
186             keys[idx] = keys[last];
187             vals[idx] = vals[last];
188             vals[last] = null;
189             keys[last] = null;
190         }
191         last--;
192     }
193     
194     private int indexOfKey (String JavaDoc key) {
195         for (int i=last; i >= 0; i--) {
196             if (keys[i].equals(key)) {
197                 return i;
198             }
199         }
200         return -1;
201     }
202     
203     private int indexOfVal (NbIO val) {
204         for (int i=last; i >= 0; i--) {
205             if (vals[i] == val) {
206                 return i;
207             }
208         }
209         return -1;
210     }
211     
212     private void growArrays() {
213         String JavaDoc[] newKeys = new String JavaDoc[keys.length * 2];
214         NbIO[] newVals = new NbIO[vals.length * 2];
215         System.arraycopy (keys, 0, newKeys, 0, last+1);
216         System.arraycopy (vals, 0, newVals, 0, last+1);
217         keys = newKeys;
218         vals = newVals;
219     }
220     
221     private boolean pruning = false;
222     private int prune() {
223         pruning = true;
224         int oldSize = last + 1;
225         int result = oldSize;
226         int[] removes = new int[oldSize];
227         Arrays.fill (removes, -1);
228         for (int i=last; i >= 0; i--) {
229             if (getValue(i) == null) {
230                 removes[i] = i;
231                 result--;
232             }
233         }
234
235         if (result != oldSize) {
236             for (int i=removes.length-1; i >= 0; i--) {
237                 if (removes[i] != -1) {
238                     removeIndex(removes[i]);
239                 }
240             }
241         }
242         pruning = false;
243         return result;
244     }
245     
246 }
247
Popular Tags