KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > FeatureTable


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.pde.internal.core.ifeature.IFeature;
18 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
19
20 /**
21  * Stores IFeatureModels. Models are indexed by id and id with version for fast
22  * retrieval. Given id or version may have more than one corresponding model. A
23  * model has only one id and version that can be null. When models changes, its
24  * Idver stays unchanged until the models reinserted.
25  */

26 class FeatureTable {
27     public class Idver {
28         private final String JavaDoc fId;
29
30         private final String JavaDoc fVer;
31
32         public Idver(String JavaDoc id, String JavaDoc version) {
33             fId = id;
34             fVer = version;
35         }
36
37         public String JavaDoc getId() {
38             return fId;
39         }
40
41         public String JavaDoc getVer() {
42             return fVer;
43         }
44
45         public int hashCode() {
46             int code = 0;
47             if (fId != null)
48                 code += fId.hashCode();
49             if (fVer != null)
50                 code += fVer.hashCode();
51
52             return code;
53         }
54
55         public boolean equals(Object JavaDoc obj) {
56             if (this == obj)
57                 return true;
58             if (!(obj instanceof Idver))
59                 return false;
60             return equals(((Idver) obj).getId(), ((Idver) obj).getVer());
61         }
62
63         public boolean equals(String JavaDoc id, String JavaDoc version) {
64             boolean sameId = fId == null && id == null || fId != null
65                     && fId.equals(id);
66             boolean sameVer = fVer == null && version == null || fVer != null
67                     && fVer.equals(version);
68             return sameId && sameVer;
69
70         }
71
72         public String JavaDoc toString() {
73             return fId + "_" + fVer; //$NON-NLS-1$
74
}
75     }
76
77     private static final IFeatureModel[] NO_MODELS = new IFeatureModel[0];
78
79     /**
80      * Map of IFeatureModel to Idver
81      */

82     private Map JavaDoc fModel2idver;
83
84     /**
85      * Map of Idver to ArrayList of IFeatureModel
86      */

87     private Map JavaDoc fIdver2models;
88
89     /**
90      * Map of Id to ArrayList of Idver
91      */

92     private Map JavaDoc fId2idvers;
93
94     public FeatureTable() {
95
96         fModel2idver = new HashMap JavaDoc();
97         fIdver2models = new HashMap JavaDoc();
98         fId2idvers = new HashMap JavaDoc();
99     }
100
101     public synchronized Idver get(IFeatureModel model) {
102         return (Idver) fModel2idver.get(model);
103     }
104
105     /**
106      * @param id
107      * @param version
108      * @return IFeatureModel[]
109      */

110     public synchronized IFeatureModel[] get(String JavaDoc id, String JavaDoc version) {
111         return getImpl(new Idver(id, version));
112     }
113
114     /**
115      * @param idver
116      * @return IFeatureModel[]
117      */

118     public synchronized IFeatureModel[] get(Idver idver) {
119         return getImpl(idver);
120     }
121
122     /**
123      * @param idver
124      * @return
125      */

126     private IFeatureModel[] getImpl(Idver idver) {
127         ArrayList JavaDoc models = (ArrayList JavaDoc) fIdver2models.get(idver);
128         if (models == null) {
129             return NO_MODELS;
130         }
131         return (IFeatureModel[]) models
132                 .toArray(new IFeatureModel[models.size()]);
133     }
134
135     public synchronized IFeatureModel[] get(String JavaDoc id) {
136         ArrayList JavaDoc idvers = (ArrayList JavaDoc) fId2idvers.get(id);
137         if (idvers == null)
138             return NO_MODELS;
139         ArrayList JavaDoc allModels = new ArrayList JavaDoc();
140         for (int i = 0; i < idvers.size(); i++) {
141             Idver idver = (Idver) idvers.get(i);
142             ArrayList JavaDoc models = (ArrayList JavaDoc) fIdver2models.get(idver);
143             if (models == null) {
144                 continue;
145             }
146             allModels.addAll(models);
147         }
148         return (IFeatureModel[]) allModels.toArray(new IFeatureModel[allModels
149                 .size()]);
150     }
151
152     public synchronized IFeatureModel[] getAll() {
153         return getAllImpl();
154     }
155
156     /**
157      * @return
158      */

159     private IFeatureModel[] getAllImpl() {
160         return (IFeatureModel[]) fModel2idver.keySet().toArray(
161                 new IFeatureModel[fModel2idver.size()]);
162     }
163
164     /**
165      * Removes the model.
166      *
167      * @return Idver if model existed and was removed, null otherwise
168      */

169     public synchronized Idver remove(IFeatureModel model) {
170         return removeImpl(model);
171     }
172
173     /**
174      * @param model
175      * @return
176      */

177     private Idver removeImpl(IFeatureModel model) {
178         Idver idver = (Idver) fModel2idver.remove(model);
179         if (idver == null) {
180             return null;
181         }
182         ArrayList JavaDoc models = (ArrayList JavaDoc) fIdver2models.get(idver);
183         for (int i = 0; i < models.size(); i++) {
184             if (models.get(i) == model) {
185                 models.remove(i);
186                 break;
187             }
188         }
189         if (models.size() <= 0) {
190             fIdver2models.remove(idver);
191
192             ArrayList JavaDoc idvers = (ArrayList JavaDoc) fId2idvers.get(idver.getId());
193             for (int i = 0; i < idvers.size(); i++) {
194                 if (idvers.get(i).equals(idver)) {
195                     idvers.remove(i);
196                     break;
197                 }
198             }
199             if (idvers.size() <= 0) {
200                 fId2idvers.remove(idver.getId());
201             }
202         }
203         return idver;
204     }
205
206     /**
207      * Adds the model. Updates the position of the model if already exist.
208      *
209      * @return Idver used during insertion
210      */

211     public synchronized Idver add(IFeatureModel model) {
212         removeImpl(model);
213
214         IFeature feature = model.getFeature();
215         String JavaDoc id = feature.getId();
216         String JavaDoc ver = feature.getVersion();
217         Idver idver = new Idver(id, ver);
218
219         fModel2idver.put(model, idver);
220
221         ArrayList JavaDoc models = (ArrayList JavaDoc) fIdver2models.get(idver);
222         if (models == null) {
223             models = new ArrayList JavaDoc(1);
224             fIdver2models.put(idver, models);
225         }
226         models.add(model);
227
228         ArrayList JavaDoc idvers = (ArrayList JavaDoc) fId2idvers.get(id);
229         if (idvers == null) {
230             idvers = new ArrayList JavaDoc(1);
231             fId2idvers.put(id, idvers);
232         }
233         idvers.add(idver);
234
235         return idver;
236     }
237
238     public synchronized String JavaDoc toString() {
239         IFeatureModel[] models = getAllImpl();
240         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(30 * models.length);
241         buf.append("["); //$NON-NLS-1$
242
for (int i = 0; i < models.length; i++) {
243             if (i > 0) {
244                 buf.append(", "); //$NON-NLS-1$
245
}
246             buf.append(get(models[i]));
247             buf.append("@"); //$NON-NLS-1$
248
buf.append(models[i].getFeature().getId());
249             buf.append("_"); //$NON-NLS-1$
250
buf.append(models[i].getFeature().getVersion());
251         }
252         buf.append("]"); //$NON-NLS-1$
253
return buf.toString();
254     }
255
256 }
257
Popular Tags