[578422] Extend subscriber retrieval through extension points
[EMFCompare2.git] / plugins / org.eclipse.emf.compare.ide.ui / src / org / eclipse / emf / compare / ide / ui / subscriber / SubscriberProviderRegistry.java
blob3545d78941e0a4c78905d97cc1e4e1410b91273f
1 /*******************************************************************************
2 * Copyright (c) 2022 EclipseSource 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 * Martin Fleck - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.emf.compare.ide.ui.subscriber;
13 import java.util.Comparator;
14 import java.util.LinkedHashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.stream.Collectors;
19 import org.eclipse.compare.ICompareContainer;
20 import org.eclipse.compare.ITypedElement;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.emf.compare.ide.ui.internal.subscriber.SubscriberProviderDescriptor;
23 import org.eclipse.team.core.subscribers.Subscriber;
25 /**
26 * The registry managing the registered subscriber provider extension point information.
28 * @author Martin Fleck <mfleck@eclipsesource.com>
29 * @since 4.4.3
31 public class SubscriberProviderRegistry {
33 /** Keeps track of the extensions providing subscriber providers. */
34 private final Map<String, SubscriberProviderDescriptor> registeredDescriptors;
36 /**
37 * Constructs and initialized this registry.
39 public SubscriberProviderRegistry() {
40 registeredDescriptors = new LinkedHashMap<>();
43 /**
44 * Adds the given {@link SubscriberProviderDescriptor} to this registry, using the given {@code className}
45 * as the identifier.
47 * @param className
48 * The identifier for the given {@link SubscriberProviderDescriptor}.
49 * @param descriptor
50 * The {@link SubscriberProviderDescriptor} which is to be added to this registry.
52 public void addProvider(String className, SubscriberProviderDescriptor descriptor) {
53 registeredDescriptors.put(className, descriptor);
56 /**
57 * Removes the {@link SubscriberProviderDescriptor} and its managed {@link ISubscriberProvider} identified
58 * by the given {@code className} from this registry.
60 * @param className
61 * Identifier of the provider we are to remove from this registry.
62 * @return The removed {@link SubscriberProviderDescriptor}, if any.
64 public SubscriberProviderDescriptor removeProvider(String className) {
65 return registeredDescriptors.remove(className);
68 /** Clears out all registered providers from this registry. */
69 public void clear() {
70 registeredDescriptors.clear();
73 /**
74 * Returns the subscriber that provides the synchronization between local resources and remote resources
75 * based on the given comparison input.
77 * @param container
78 * The compare container input.
79 * @param left
80 * Left of the compared elements.
81 * @param right
82 * Right of the compared elements.
83 * @param origin
84 * Common ancestor of the <code>left</code> and <code>right</code> compared elements.
85 * @param monitor
86 * Monitor to report progress on.
87 * @return The subscriber used for the comparison of the container or <code>null</code> if no subscriber
88 * could be determined.
90 public Subscriber getSubscriber(ICompareContainer container, ITypedElement left, ITypedElement right,
91 ITypedElement origin, IProgressMonitor monitor) {
92 List<SubscriberProviderDescriptor> rankedDescriptors = registeredDescriptors.values().stream()
93 .sorted(Comparator.comparingInt(SubscriberProviderDescriptor::getRanking).reversed())
94 .collect(Collectors.toList());
95 for (SubscriberProviderDescriptor descriptor : rankedDescriptors) {
96 ISubscriberProvider provider = descriptor.getSubscriberProvider();
97 if (provider != null) {
98 Subscriber subscriber = provider.getSubscriber(container, left, right, origin, monitor);
99 if (subscriber != null) {
100 return subscriber;
104 return null;