1 11 package org.eclipse.pde.internal.core; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.pde.internal.core.ifeature.IFeature; 18 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 19 20 26 class FeatureTable { 27 public class Idver { 28 private final String fId; 29 30 private final String fVer; 31 32 public Idver(String id, String version) { 33 fId = id; 34 fVer = version; 35 } 36 37 public String getId() { 38 return fId; 39 } 40 41 public String 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 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 id, String 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 toString() { 73 return fId + "_" + fVer; } 75 } 76 77 private static final IFeatureModel[] NO_MODELS = new IFeatureModel[0]; 78 79 82 private Map fModel2idver; 83 84 87 private Map fIdver2models; 88 89 92 private Map fId2idvers; 93 94 public FeatureTable() { 95 96 fModel2idver = new HashMap (); 97 fIdver2models = new HashMap (); 98 fId2idvers = new HashMap (); 99 } 100 101 public synchronized Idver get(IFeatureModel model) { 102 return (Idver) fModel2idver.get(model); 103 } 104 105 110 public synchronized IFeatureModel[] get(String id, String version) { 111 return getImpl(new Idver(id, version)); 112 } 113 114 118 public synchronized IFeatureModel[] get(Idver idver) { 119 return getImpl(idver); 120 } 121 122 126 private IFeatureModel[] getImpl(Idver idver) { 127 ArrayList models = (ArrayList ) 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 id) { 136 ArrayList idvers = (ArrayList ) fId2idvers.get(id); 137 if (idvers == null) 138 return NO_MODELS; 139 ArrayList allModels = new ArrayList (); 140 for (int i = 0; i < idvers.size(); i++) { 141 Idver idver = (Idver) idvers.get(i); 142 ArrayList models = (ArrayList ) 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 159 private IFeatureModel[] getAllImpl() { 160 return (IFeatureModel[]) fModel2idver.keySet().toArray( 161 new IFeatureModel[fModel2idver.size()]); 162 } 163 164 169 public synchronized Idver remove(IFeatureModel model) { 170 return removeImpl(model); 171 } 172 173 177 private Idver removeImpl(IFeatureModel model) { 178 Idver idver = (Idver) fModel2idver.remove(model); 179 if (idver == null) { 180 return null; 181 } 182 ArrayList models = (ArrayList ) 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 idvers = (ArrayList ) 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 211 public synchronized Idver add(IFeatureModel model) { 212 removeImpl(model); 213 214 IFeature feature = model.getFeature(); 215 String id = feature.getId(); 216 String ver = feature.getVersion(); 217 Idver idver = new Idver(id, ver); 218 219 fModel2idver.put(model, idver); 220 221 ArrayList models = (ArrayList ) fIdver2models.get(idver); 222 if (models == null) { 223 models = new ArrayList (1); 224 fIdver2models.put(idver, models); 225 } 226 models.add(model); 227 228 ArrayList idvers = (ArrayList ) fId2idvers.get(id); 229 if (idvers == null) { 230 idvers = new ArrayList (1); 231 fId2idvers.put(id, idvers); 232 } 233 idvers.add(idver); 234 235 return idver; 236 } 237 238 public synchronized String toString() { 239 IFeatureModel[] models = getAllImpl(); 240 StringBuffer buf = new StringBuffer (30 * models.length); 241 buf.append("["); for (int i = 0; i < models.length; i++) { 243 if (i > 0) { 244 buf.append(", "); } 246 buf.append(get(models[i])); 247 buf.append("@"); buf.append(models[i].getFeature().getId()); 249 buf.append("_"); buf.append(models[i].getFeature().getVersion()); 251 } 252 buf.append("]"); return buf.toString(); 254 } 255 256 } 257 | Popular Tags |