Skip to content

Commit ef8188e

Browse files
committed
Simplify DirtyFileSearchParticipantServiceTracker
1 parent cc24621 commit ef8188e

File tree

1 file changed

+15
-34
lines changed

1 file changed

+15
-34
lines changed
Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2023 Red Hat Inc and others.
2+
* Copyright (c) 2023, 2024 Red Hat Inc and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -16,57 +16,38 @@
1616
import java.text.MessageFormat;
1717
import java.util.Arrays;
1818
import java.util.Collections;
19-
import java.util.List;
20-
import java.util.Map;
19+
import java.util.Comparator;
20+
import java.util.Optional;
2121

2222
import org.osgi.framework.BundleContext;
2323
import org.osgi.framework.InvalidSyntaxException;
2424
import org.osgi.framework.ServiceReference;
2525
import org.osgi.util.tracker.ServiceTracker;
2626

27-
import org.eclipse.core.resources.IFile;
28-
29-
import org.eclipse.jface.text.IDocument;
30-
3127
import org.eclipse.search.internal.core.text.DirtyFileProvider;
3228

33-
public class DirtyFileSearchParticipantServiceTracker
34-
extends ServiceTracker<DirtyFileProvider, DirtyFileProvider> {
29+
public class DirtyFileSearchParticipantServiceTracker extends ServiceTracker<DirtyFileProvider, DirtyFileProvider> {
3530

3631
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 {
3934
super(context, context.createFilter(MessageFormat.format("(&(objectClass={0}))", //$NON-NLS-1$
4035
DirtyFileProvider.class.getCanonicalName())), null);
4136
}
4237

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+
4343
public DirtyFileProvider checkedGetService() {
4444
ServiceReference<DirtyFileProvider>[] allRefs = getServiceReferences();
4545
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());
5849
}
5950
}
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;
7152
}
7253
}

0 commit comments

Comments
 (0)