KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > dtd > util > SortedMap


1 /*******************************************************************************
2  * Copyright (c) 2002, 2005 Object Factory Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Object Factory Inc. - Initial implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.dtd.util;
12
13 import com.ibm.icu.text.MessageFormat;
14 import java.util.Comparator JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 /**
18  * @author Bob Foster
19  */

20 public class SortedMap implements FactoryObject {
21
22     private SortedSet fSet;
23     private IMapHolder fHolder;
24     private SortedMap fNext;
25     
26     public SortedMap(IMapHolder holder, Comparator JavaDoc comp) {
27         fHolder = holder;
28         fSet = new SortedSet(holder, comp);
29     }
30     
31     public SortedMap(Comparator JavaDoc comp) {
32         fSet = new SortedSet(comp);
33     }
34     
35     public SortedMap(IMapHolder holder) {
36         fHolder = holder;
37         fSet = new SortedSet(holder);
38     }
39     
40     public SortedMap() {
41         fSet = new SortedSet();
42     }
43     
44     public void setMapHolder(IMapHolder holder) {
45         fHolder = holder;
46         fSet.setKeyHolder(holder);
47     }
48     
49     public void setComparator(Comparator JavaDoc comp) {
50         fSet.setComparator(comp);
51     }
52     
53     public boolean containsKey(Object JavaDoc key) {
54         return fSet.contains(key);
55     }
56     
57     public boolean containsKeyIdentity(Object JavaDoc key) {
58         return fSet.containsIdentity(key);
59     }
60     
61     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc val) {
62         Object JavaDoc[] values = fHolder.getValues();
63         int index = fSet.indexOf(key);
64         Object JavaDoc result = index >= 0 && values != null ? values[index] : null;
65         
66         int i = fSet.internalAdd(key, false);
67         if (i >= 0)
68             internalPut(i, val);
69         return result;
70     }
71     
72     protected void internalPut(int i, Object JavaDoc val) {
73         Object JavaDoc[] values = fHolder.getValues();
74         if (values == null) {
75             values = new Object JavaDoc[1];
76             values[0] = val;
77             return;
78         }
79     
80         Object JavaDoc[] tmp = new Object JavaDoc[values.length+1];
81         System.arraycopy(values,0,tmp,0,i);
82         tmp[i] = val;
83         System.arraycopy(values,i,tmp,i+1,values.length-i);
84         fHolder.setValues(tmp);
85     }
86     
87     public int putAlways(Object JavaDoc key, Object JavaDoc val) {
88         int i = fSet.internalAdd(key, true);
89         internalPut(i, val);
90         return i;
91     }
92     
93     public void append(Object JavaDoc key, Object JavaDoc val) {
94         Object JavaDoc[] values = fHolder.getValues();
95         int len = values != null ? values.length : 0;
96         fSet.internalAdd(len, key);
97         internalPut(len, val);
98     }
99     
100     public Object JavaDoc get(Object JavaDoc key) {
101         Object JavaDoc[] values = fHolder.getValues();
102         if (values == null)
103             return null;
104         int i = fSet.indexOf(key);
105         if (i >= 0)
106             return values[i];
107         return null;
108     }
109     
110     public Object JavaDoc getIdentity(Object JavaDoc key) {
111         Object JavaDoc[] values = fHolder.getValues();
112         if (values == null)
113             return null;
114         int i = fSet.indexOfIdentity(key);
115         if (i >= 0)
116             return values[i];
117         return null;
118     }
119     
120     public Object JavaDoc[] keys() {
121         return fSet.members();
122     }
123     
124     public Object JavaDoc[] values() {
125         Object JavaDoc[] values = fHolder.getValues();
126         if (values == null)
127             return new Object JavaDoc[0];
128         return values;
129     }
130     
131     public Iterator JavaDoc keyIterator() {
132         return new ArrayIterator();
133     }
134
135     public Iterator JavaDoc valueIterator() {
136         return new ArrayIterator();
137     }
138     
139     private class ArrayIterator implements Iterator JavaDoc {
140         private int fIndex;
141         
142         public ArrayIterator() {
143             fIndex = -1;
144         }
145         /**
146          * @see java.util.Iterator#hasNext()
147          */

148         public boolean hasNext() {
149             Object JavaDoc[] array = SortedMap.this.fHolder.getKeys();
150             if (array == null)
151                 return false;
152             return fIndex + 1 < array.length;
153         }
154
155         /**
156          * @see java.util.Iterator#next()
157          */

158         public Object JavaDoc next() {
159             Object JavaDoc[] array = SortedMap.this.fHolder.getKeys();
160             if (array == null)
161                 throw new IllegalStateException JavaDoc(AntDTDUtilMessages.SortedMap_next___called_for_empty_array_1);
162             return array[++fIndex];
163         }
164
165         /**
166          * @see java.util.Iterator#remove()
167          */

168         public void remove() {
169             SortedMap.this.remove(fIndex);
170             --fIndex;
171         }
172
173     }
174     
175     public void remove(int i) {
176         Object JavaDoc[] values = fHolder.getValues();
177         if (values == null) {
178             throw new IllegalArgumentException JavaDoc(MessageFormat.format(AntDTDUtilMessages.SortedMap_remove__0___in_empty_map_2, new String JavaDoc[]{Integer.toString(i)}));
179         }
180         fSet.remove(i);
181         Object JavaDoc[] tmp = new Object JavaDoc[values.length-1];
182         System.arraycopy(values,0,tmp,0,i);
183         System.arraycopy(values,i+1,tmp,i,values.length-i-1);
184         fHolder.setValues(tmp);
185     }
186     
187     public Object JavaDoc remove(Object JavaDoc obj) {
188         Object JavaDoc[] values = fHolder.getValues();
189         if (values == null)
190             return null;
191         int i = fSet.indexOf(obj);
192         if (i >= 0) {
193             Object JavaDoc tmp = values[i];
194             fSet.remove(i);
195             remove(i);
196             return tmp;
197         }
198         return null;
199     }
200     public Object JavaDoc removeIdentity(Object JavaDoc obj) {
201         Object JavaDoc[] values = fHolder.getValues();
202         if (values == null)
203             return null;
204         int i = fSet.indexOfIdentity(obj);
205         if (i >= 0) {
206             Object JavaDoc tmp = values[i];
207             fSet.remove(i);
208             remove(i);
209             return tmp;
210         }
211         return null;
212     }
213
214     public int size() {
215         return fSet.size();
216     }
217     
218     public int keyIndex(Object JavaDoc key) {
219         return fSet.indexOf(key);
220     }
221     
222     public void merge(SortedMap other) {
223         Object JavaDoc[] values = fHolder.getValues();
224         Object JavaDoc[] keys = fHolder.getKeys();
225         Object JavaDoc[] othervalues = other.fHolder.getValues();
226         Object JavaDoc[] otherkeys = other.fHolder.getKeys();
227         if (otherkeys == null)
228             return;
229         if (keys == null) {
230             fHolder.setKeys(otherkeys);
231             fHolder.setValues(othervalues);
232             return;
233         }
234         int ithis = 0, iother = 0, i = 0;
235         int mthis = keys.length, mother = otherkeys.length;
236         Object JavaDoc[] ktmp = new Object JavaDoc[mthis+mother];
237         Object JavaDoc[] vtmp = new Object JavaDoc[mthis+mother];
238         while (ithis < mthis && iother < mother) {
239             int comp = fSet.fComp.compare(keys[ithis], otherkeys[iother]);
240             if (comp <= 0) {
241                 vtmp[i] = values[ithis];
242                 ktmp[i++] = keys[ithis++];
243             }
244             else {
245                 vtmp[i] = othervalues[iother];
246                 ktmp[i++] = otherkeys[iother++];
247             }
248         }
249         while (ithis < mthis) {
250             vtmp[i] = values[ithis];
251             ktmp[i++] = keys[ithis++];
252         }
253         while (iother < mother) {
254             vtmp[i] = othervalues[iother];
255             ktmp[i++] = otherkeys[iother++];
256         }
257         fHolder.setKeys(ktmp);
258         fHolder.setValues(vtmp);
259     }
260     
261     public FactoryObject next() {
262         return fNext;
263     }
264     
265     public void next(FactoryObject next) {
266         fNext = (SortedMap) next;
267     }
268 }
269
Popular Tags