KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > module > VersionHashMap


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.osgi.internal.module;
12
13 import java.util.*;
14 import org.eclipse.osgi.framework.internal.core.Constants;
15
16 public class VersionHashMap extends MappedList implements Comparator {
17     private final String JavaDoc systemBundle = Constants.getInternalSymbolicName();
18     private ResolverImpl resolver;
19
20     public VersionHashMap(ResolverImpl resolver) {
21         this.resolver = resolver;
22     }
23
24     // sorts using the Comparator#compare method to sort
25
protected void sort(Object JavaDoc[] values) {
26         Arrays.sort(values, this);
27     }
28
29     public void put(VersionSupplier[] versionSuppliers) {
30         for (int i = 0; i < versionSuppliers.length; i++)
31             put(versionSuppliers[i].getName(), versionSuppliers[i]);
32     }
33
34     public void put(Object JavaDoc key, Object JavaDoc value) {
35         super.put(key, value);
36         ((VersionSupplier) value).setDropped(false);
37     }
38
39     public boolean contains(VersionSupplier vs) {
40         return contains(vs, false) != null;
41     }
42
43     private VersionSupplier contains(VersionSupplier vs, boolean remove) {
44         Object JavaDoc[] existing = (Object JavaDoc[]) internal.get(vs.getName());
45         if (existing == null)
46             return null;
47         for (int i = 0; i < existing.length; i++)
48             if (existing[i] == vs) {
49                 if (remove) {
50                     vs.setDropped(true);
51                     if (existing.length == 1) {
52                         internal.remove(vs.getName());
53                         return vs;
54                     }
55                     Object JavaDoc[] newExisting = new Object JavaDoc[existing.length - 1];
56                     System.arraycopy(existing, 0, newExisting, 0, i);
57                     if (i + 1 < existing.length)
58                         System.arraycopy(existing, i + 1, newExisting, i, existing.length - i - 1);
59                     internal.put(vs.getName(), newExisting);
60                 }
61                 return vs;
62             }
63         return null;
64     }
65
66     public Object JavaDoc remove(VersionSupplier toBeRemoved) {
67         return contains(toBeRemoved, true);
68     }
69
70     public void remove(VersionSupplier[] versionSuppliers) {
71         for (int i = 0; i < versionSuppliers.length; i++)
72             remove(versionSuppliers[i]);
73     }
74
75     public Object JavaDoc[] remove(Object JavaDoc key) {
76         Object JavaDoc[] results = super.remove(key);
77         for (int i = 0; i < results.length; i++)
78             ((VersionSupplier) results[i]).setDropped(true);
79         return results;
80     }
81
82     // Once we have resolved bundles, we need to make sure that version suppliers
83
// from the resolved bundles are ahead of those from unresolved bundles
84
void reorder() {
85         for (Iterator it = internal.values().iterator(); it.hasNext();) {
86             Object JavaDoc[] existing = (Object JavaDoc[]) it.next();
87             if (existing.length <= 1)
88                 continue;
89             sort(existing);
90         }
91     }
92
93     // Compares two VersionSuppliers for descending ordered sorts.
94
// The VersionSuppliers are sorted by the following priorities
95
// First the resolution status of the supplying bundle.
96
// Second is the supplier version.
97
// Third is the bundle id of the supplying bundle.
98
public int compare(Object JavaDoc o1, Object JavaDoc o2) {
99         if (!(o1 instanceof VersionSupplier) || !(o2 instanceof VersionSupplier))
100             throw new IllegalArgumentException JavaDoc();
101         VersionSupplier vs1 = (VersionSupplier) o1;
102         VersionSupplier vs2 = (VersionSupplier) o2;
103         // if the selection policy is set then use that
104
if (resolver.getSelectionPolicy() != null)
105             return resolver.getSelectionPolicy().compare(vs1.getBaseDescription(), vs2.getBaseDescription());
106         if (systemBundle.equals(vs1.getBundle().getSymbolicName()) && !systemBundle.equals(vs2.getBundle().getSymbolicName()))
107             return -1;
108         else if (!systemBundle.equals(vs1.getBundle().getSymbolicName()) && systemBundle.equals(vs2.getBundle().getSymbolicName()))
109             return 1;
110         if (vs1.getBundle().isResolved() != vs2.getBundle().isResolved())
111             return vs1.getBundle().isResolved() ? -1 : 1;
112         int versionCompare = -(vs1.getVersion().compareTo(vs2.getVersion()));
113         if (versionCompare != 0)
114             return versionCompare;
115         return vs1.getBundle().getBundleId() <= vs2.getBundle().getBundleId() ? -1 : 1;
116     }
117 }
118
Popular Tags