|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2023 Red Hat Inc and others. |
| 2 | + * Copyright (c) 2023, 2024 Red Hat Inc and others. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials |
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0 |
|
16 | 16 | import java.text.MessageFormat; |
17 | 17 | import java.util.Arrays; |
18 | 18 | import java.util.Collections; |
19 | | -import java.util.List; |
20 | | -import java.util.Map; |
| 19 | +import java.util.Comparator; |
| 20 | +import java.util.Optional; |
21 | 21 |
|
22 | 22 | import org.osgi.framework.BundleContext; |
23 | 23 | import org.osgi.framework.InvalidSyntaxException; |
24 | 24 | import org.osgi.framework.ServiceReference; |
25 | 25 | import org.osgi.util.tracker.ServiceTracker; |
26 | 26 |
|
27 | | -import org.eclipse.core.resources.IFile; |
28 | | - |
29 | | -import org.eclipse.jface.text.IDocument; |
30 | | - |
31 | 27 | import org.eclipse.search.internal.core.text.DirtyFileProvider; |
32 | 28 |
|
33 | | -public class DirtyFileSearchParticipantServiceTracker |
34 | | - extends ServiceTracker<DirtyFileProvider, DirtyFileProvider> { |
| 29 | +public class DirtyFileSearchParticipantServiceTracker extends ServiceTracker<DirtyFileProvider, DirtyFileProvider> { |
35 | 30 |
|
36 | 31 | private static final String PROPERTY_WEIGHT = "weight"; //$NON-NLS-1$ |
37 | | - public DirtyFileSearchParticipantServiceTracker(BundleContext context) |
38 | | - throws InvalidSyntaxException { |
| 32 | + |
| 33 | + public DirtyFileSearchParticipantServiceTracker(BundleContext context) throws InvalidSyntaxException { |
39 | 34 | super(context, context.createFilter(MessageFormat.format("(&(objectClass={0}))", //$NON-NLS-1$ |
40 | 35 | DirtyFileProvider.class.getCanonicalName())), null); |
41 | 36 | } |
42 | 37 |
|
| 38 | + private final static Comparator<ServiceReference<DirtyFileProvider>> BY_WEIGHT = Comparator.comparing( |
| 39 | + o -> o.getProperty(PROPERTY_WEIGHT), // |
| 40 | + Comparator.nullsFirst(Comparator.comparing(Integer.class::isInstance) // false<true |
| 41 | + .thenComparing(Integer.class::cast))); |
| 42 | + |
43 | 43 | public DirtyFileProvider checkedGetService() { |
44 | 44 | ServiceReference<DirtyFileProvider>[] allRefs = getServiceReferences(); |
45 | 45 | if (allRefs != null && allRefs.length > 0) { |
46 | | - List<ServiceReference<DirtyFileProvider>> l = Arrays.asList(allRefs); |
47 | | - Collections.sort(l, (o1, o2) -> { |
48 | | - Object o1Weight = o1.getProperty(PROPERTY_WEIGHT); |
49 | | - Object o2Weight = o2.getProperty(PROPERTY_WEIGHT); |
50 | | - int o1Val = o1Weight == null ? 0 |
51 | | - : o1Weight instanceof Integer ? ((Integer) o1Weight).intValue() : 0; |
52 | | - int o2Val = o2Weight == null ? 0 |
53 | | - : o2Weight instanceof Integer ? ((Integer) o2Weight).intValue() : 0; |
54 | | - return o2Val - o1Val; |
55 | | - }); |
56 | | - if (l.size() > 0) { |
57 | | - return getService(l.get(0)); |
| 46 | + Optional<ServiceReference<DirtyFileProvider>> reference = Arrays.stream(allRefs).max(BY_WEIGHT); |
| 47 | + if (reference.isPresent()) { |
| 48 | + return getService(reference.get()); |
58 | 49 | } |
59 | 50 | } |
60 | | - return new DirtyFileProvider() { |
61 | | - @SuppressWarnings("unchecked") |
62 | | - @Override |
63 | | - public Map<IFile, IDocument> dirtyFiles() { |
64 | | - return Collections.EMPTY_MAP; |
65 | | - } |
66 | | - }; |
67 | | - } |
68 | | - |
69 | | - public void dispose() { |
70 | | - close(); |
| 51 | + return Collections::emptyMap; |
71 | 52 | } |
72 | 53 | } |
0 commit comments