1 11 package org.eclipse.core.internal.model; 12 13 import java.util.*; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.core.runtime.PluginVersionIdentifier; 16 import org.eclipse.core.runtime.model.PluginModel; 17 18 21 public class PluginMap { 22 private Map map; 23 private boolean preserveOrder; 24 private int size; 25 private boolean replaceDuplicates; 26 27 public PluginMap(Map pluginModels) { 28 this(pluginModels, true, false); 29 } 30 31 public PluginMap(Map pluginModels, boolean preserveOrder, boolean replaceDuplicates) { 32 this.map = pluginModels; 33 this.preserveOrder = preserveOrder; 34 this.replaceDuplicates = replaceDuplicates; 35 } 36 37 public void add(PluginModel pluginModel) { 38 39 String key = pluginModel.getId(); 40 List verList = (List) map.get(key); 41 42 if (verList == null) { 44 verList = new LinkedList(); 45 map.put(key, verList); 46 } 47 48 int i = 0; 49 if (preserveOrder) 51 for (; i < verList.size(); i++) { 52 PluginModel element = (PluginModel) verList.get(i); 53 if (getVersionIdentifier(pluginModel).equals(getVersionIdentifier(element))) { 54 if (replaceDuplicates) 55 verList.set(i, pluginModel); 56 return; } 58 if (getVersionIdentifier(pluginModel).isGreaterThan(getVersionIdentifier(element))) 59 break; 60 } 61 verList.add(i, pluginModel); 62 size++; 63 } 64 65 public PluginModel get(String id, String version) { 66 List versions = (List) map.get(id); 67 if (versions == null || versions.isEmpty()) 68 return null; 69 if (version == null) 70 return (PluginModel) versions.get(0); 72 int versionCount = versions.size(); 73 for (int i = 0; i < versionCount; i++) { 74 PluginModel pluginModel = (PluginModel) versions.get(i); 75 if (pluginModel.getVersion().equals(version)) 76 return pluginModel; 77 } 78 return null; 79 } 80 81 public List getVersions(String id) { 82 return (List) map.get(id); 83 } 84 85 public PluginModel getAny(String id) { 86 List versions = (List) map.get(id); 87 if (versions == null || versions.isEmpty()) 88 return null; 89 return (PluginModel) versions.get(0); 90 } 91 92 private PluginVersionIdentifier getVersionIdentifier(PluginModel model) { 93 if (PluginVersionIdentifier.validateVersion(model.getVersion()).getSeverity() != IStatus.OK) 94 return new PluginVersionIdentifier("0.0.0"); return new PluginVersionIdentifier(model.getVersion()); 96 } 97 98 public int size() { 99 return size; 100 } 101 102 public void markReadOnly() { 103 for (Iterator it = map.values().iterator(); it.hasNext();) { 104 List list = (List) it.next(); 105 int count = list.size(); 106 for (int i = 0; i < count; i++) 107 ((PluginModel) list.get(i)).markReadOnly(); 108 } 109 } 110 111 public PluginModel remove(String pluginId, String version) { 112 List versions = (List) map.get(pluginId); 113 if (versions == null) 114 return null; 115 for (Iterator iter = versions.iterator(); iter.hasNext();) { 116 PluginModel pluginModel = (PluginModel) iter.next(); 117 if (pluginModel.getId().equals(pluginId) && pluginModel.getVersion().equals(version)) { 118 if (versions.size() == 1) 119 map.remove(pluginId); 120 else 121 iter.remove(); 122 size--; 123 return pluginModel; 124 } 125 } 126 return null; 127 } 128 129 public void removeVersions(String pluginId) { 130 List versions = (List) map.remove(pluginId); 131 if (versions != null) 132 size -= versions.size(); 133 } 134 135 public void copyToArray(Object [] array) { 136 int index = 0; 137 for (Iterator mapIter = map.values().iterator(); mapIter.hasNext();) { 138 List versions = (List) mapIter.next(); 139 for (Iterator listIiter = versions.iterator(); listIiter.hasNext();) 140 array[index++] = listIiter.next(); 141 } 142 } 143 } 144 | Popular Tags |