1 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 systemBundle = Constants.getInternalSymbolicName(); 18 private ResolverImpl resolver; 19 20 public VersionHashMap(ResolverImpl resolver) { 21 this.resolver = resolver; 22 } 23 24 protected void sort(Object [] 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 key, Object 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 [] existing = (Object []) 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 [] newExisting = new Object [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 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 [] remove(Object key) { 76 Object [] 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 void reorder() { 85 for (Iterator it = internal.values().iterator(); it.hasNext();) { 86 Object [] existing = (Object []) it.next(); 87 if (existing.length <= 1) 88 continue; 89 sort(existing); 90 } 91 } 92 93 public int compare(Object o1, Object o2) { 99 if (!(o1 instanceof VersionSupplier) || !(o2 instanceof VersionSupplier)) 100 throw new IllegalArgumentException (); 101 VersionSupplier vs1 = (VersionSupplier) o1; 102 VersionSupplier vs2 = (VersionSupplier) o2; 103 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 |