Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMykola Nikishov2018-02-09 18:02:20 +0000
committerAlexander Kurtakov2018-02-12 13:29:50 +0000
commitd30f1ec58f55cfd47c174ba72d673d9e37c8573e (patch)
tree861d948484f4d421cef50c345c288f18df002022
parente819e1b82f257d1fa94d397482004cf6852320b4 (diff)
downloadrt.equinox.p2-d30f1ec58f55cfd47c174ba72d673d9e37c8573e.tar.gz
rt.equinox.p2-d30f1ec58f55cfd47c174ba72d673d9e37c8573e.tar.xz
rt.equinox.p2-d30f1ec58f55cfd47c174ba72d673d9e37c8573e.zip
Bug 423715 - Extract methods in ArtifactComparatorFactoryI20180214-2000I20180213-2000I20180213-0125I20180212-2000
...to isolate independent parts of code and make it easier to understand. Also, add javadoc to the single public method. Change-Id: Ie61a36eca7b98fb33af2f267ad58ce4724053f17 Signed-off-by: Mykola Nikishov <mn@mn.com.ua>
-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