Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/repository/tools/comparator/ArtifactComparatorFactory.java57
1 files changed, 34 insertions, 23 deletions
diff --git a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/repository/tools/comparator/ArtifactComparatorFactory.java b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/repository/tools/comparator/ArtifactComparatorFactory.java
index a4beebc4d..49bdf7302 100644
--- a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/repository/tools/comparator/ArtifactComparatorFactory.java
+++ b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/repository/tools/comparator/ArtifactComparatorFactory.java
@@ -7,10 +7,12 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
- * Compeople AG (Stefan Liebig) - various ongoing maintenance
+ * Compeople AG (Stefan Liebig) - various ongoing maintenance
+ * Mykola Nikishov - maintenance and documentation
*******************************************************************************/
package org.eclipse.equinox.p2.repository.tools.comparator;
+import java.util.*;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.RegistryFactory;
import org.eclipse.equinox.internal.p2.artifact.repository.Messages;
@@ -24,32 +26,41 @@ public class ArtifactComparatorFactory {
private static final String ATTR_ID = "id"; //$NON-NLS-1$
private static final String ATTR_CLASS = "class"; //$NON-NLS-1$
+ /**
+ * Find artifact comparator by comparator's extension id.
+ *
+ * @param comparatorID id of the extension contributed to <code>org.eclipse.equinox.p2.artifact.repository.artifactComparators</code> extension point
+ * @return if found, instance of artifact comparator
+ * @throws {@link IllegalArgumentException} otherwise
+ */
public static IArtifactComparator getArtifactComparator(String comparatorID) {
- IConfigurationElement[] extensions = RegistryFactory.getRegistry().getConfigurationElementsFor(COMPARATOR_POINT);
-
- IConfigurationElement element = null;
- if (comparatorID == null && extensions.length > 0) {
- element = extensions[0]; //just take the first one
- } else {
- for (IConfigurationElement extension : extensions) {
- if (extension.getAttribute(ATTR_ID).equals(comparatorID)) {
- element = extension;
- break;
- }
- }
- }
- if (element != null) {
- try {
- Object execExt = element.createExecutableExtension(ATTR_CLASS);
- if (execExt instanceof IArtifactComparator)
- return (IArtifactComparator) execExt;
- } catch (Exception e) {
- //fall through
- }
- }
+ List<IConfigurationElement> extensions = Arrays.asList(RegistryFactory.getRegistry().getConfigurationElementsFor(COMPARATOR_POINT));
+
+ Optional<IArtifactComparator> artifactComparator = findComparatorConfiguration(comparatorID, extensions).map(ArtifactComparatorFactory::createArtifactComparator);
+ if (artifactComparator.isPresent())
+ return artifactComparator.get();
if (comparatorID != null)
throw new IllegalArgumentException(NLS.bind(Messages.exception_comparatorNotFound, comparatorID));
throw new IllegalArgumentException(Messages.exception_noComparators);
}
+
+ private static Optional<IConfigurationElement> findComparatorConfiguration(String comparatorID, List<IConfigurationElement> extensions) {
+ if (comparatorID == null && extensions.size() > 0) {
+ return Optional.ofNullable(extensions.get(0)); //just take the first one
+ }
+
+ return extensions.stream().filter(extension -> extension.getAttribute(ATTR_ID).equals(comparatorID)).findAny();
+ }
+
+ private static IArtifactComparator createArtifactComparator(IConfigurationElement element) {
+ try {
+ Object execExt = element.createExecutableExtension(ATTR_CLASS);
+ if (execExt instanceof IArtifactComparator)
+ return (IArtifactComparator) execExt;
+ } catch (Exception e) {
+ //fall through
+ }
+ return null;
+ }
}

Back to the top