KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > model > PluginMap


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.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 /**
19  * A map specialized to manage plugin models (either for plugins or fragments).
20  */

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 JavaDoc key = pluginModel.getId();
40         List verList = (List) map.get(key);
41
42         // create new index entry if one does not exist for plugin
43
if (verList == null) {
44             verList = new LinkedList();
45             map.put(key, verList);
46         }
47
48         int i = 0;
49         // insert plugin into list maintaining version order
50
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; // ignore duplicates
57
}
58                 if (getVersionIdentifier(pluginModel).isGreaterThan(getVersionIdentifier(element)))
59                     break;
60             }
61         verList.add(i, pluginModel);
62         size++;
63     }
64
65     public PluginModel get(String JavaDoc id, String JavaDoc version) {
66         List versions = (List) map.get(id);
67         if (versions == null || versions.isEmpty())
68             return null;
69         if (version == null)
70             // Just return the first one in the list (random)
71
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 JavaDoc id) {
82         return (List) map.get(id);
83     }
84
85     public PluginModel getAny(String JavaDoc 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"); //$NON-NLS-1$
95
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 JavaDoc pluginId, String JavaDoc 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 JavaDoc pluginId) {
130         List versions = (List) map.remove(pluginId);
131         if (versions != null)
132             size -= versions.size();
133     }
134
135     public void copyToArray(Object JavaDoc[] 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