@Override public int compare(ServiceReference<T> serviceReference1, ServiceReference<T> serviceReference2) { if (serviceReference1 == null) { if (serviceReference2 == null) { return 0; } else { return 1; } } else if (serviceReference2 == null) { return -1; } Object propertyValue1 = serviceReference1.getProperty(_propertyKey); Object propertyValue2 = serviceReference2.getProperty(_propertyKey); if (propertyValue1 == null) { if (propertyValue2 == null) { return 0; } else { return 1; } } else if (propertyValue2 == null) { return -1; } if (!(propertyValue2 instanceof Comparable)) { return serviceReference2.compareTo(serviceReference1); } Comparable<Object> propertyValueComparable2 = (Comparable<Object>) propertyValue2; return propertyValueComparable2.compareTo(propertyValue1); }
private ServiceReference getBestServiceReference(ServiceReference[] refs) { if (refs == null) { return null; } if (refs.length == 1) { return refs[0]; } // Loop through all service references and return // the "best" one according to its rank and ID. ServiceReference bestRef = refs[0]; for (int i = 1; i < refs.length; i++) { if (bestRef.compareTo(refs[i]) < 0) { bestRef = refs[i]; } } return bestRef; }
private List<Bundle> getBundlesToDestroy(List<Bundle> bundles) { List<Bundle> bundlesToDestroy = new ArrayList<Bundle>(); for (Bundle bundle : bundles) { ServiceReference[] references = bundle.getRegisteredServices(); int usage = 0; if (references != null) { for (ServiceReference reference : references) { usage += getServiceUsage(reference, bundles); } } if (usage == 0) { bundlesToDestroy.add(bundle); } } if (!bundlesToDestroy.isEmpty()) { Collections.sort( bundlesToDestroy, new Comparator<Bundle>() { public int compare(Bundle b1, Bundle b2) { return (int) (b2.getLastModified() - b1.getLastModified()); } }); } else { ServiceReference ref = null; for (Bundle bundle : bundles) { ServiceReference[] references = bundle.getRegisteredServices(); for (ServiceReference reference : references) { if (getServiceUsage(reference, bundles) == 0) { continue; } if (ref == null || reference.compareTo(ref) < 0) { ref = reference; } } } if (ref != null) { bundlesToDestroy.add(ref.getBundle()); } } return bundlesToDestroy; }