Skip to content

Commit

Permalink
Simplify DirtyFileSearchParticipantServiceTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Oct 2, 2024
1 parent cc24621 commit ef8188e
Showing 1 changed file with 15 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat Inc and others.
* Copyright (c) 2023, 2024 Red Hat Inc and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -16,57 +16,38 @@
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Comparator;
import java.util.Optional;

import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

import org.eclipse.core.resources.IFile;

import org.eclipse.jface.text.IDocument;

import org.eclipse.search.internal.core.text.DirtyFileProvider;

public class DirtyFileSearchParticipantServiceTracker
extends ServiceTracker<DirtyFileProvider, DirtyFileProvider> {
public class DirtyFileSearchParticipantServiceTracker extends ServiceTracker<DirtyFileProvider, DirtyFileProvider> {

private static final String PROPERTY_WEIGHT = "weight"; //$NON-NLS-1$
public DirtyFileSearchParticipantServiceTracker(BundleContext context)
throws InvalidSyntaxException {

public DirtyFileSearchParticipantServiceTracker(BundleContext context) throws InvalidSyntaxException {
super(context, context.createFilter(MessageFormat.format("(&(objectClass={0}))", //$NON-NLS-1$
DirtyFileProvider.class.getCanonicalName())), null);
}

private final static Comparator<ServiceReference<DirtyFileProvider>> BY_WEIGHT = Comparator.comparing(
o -> o.getProperty(PROPERTY_WEIGHT), //
Comparator.nullsFirst(Comparator.comparing(Integer.class::isInstance) // false<true
.thenComparing(Integer.class::cast)));

public DirtyFileProvider checkedGetService() {
ServiceReference<DirtyFileProvider>[] allRefs = getServiceReferences();
if (allRefs != null && allRefs.length > 0) {
List<ServiceReference<DirtyFileProvider>> l = Arrays.asList(allRefs);
Collections.sort(l, (o1, o2) -> {
Object o1Weight = o1.getProperty(PROPERTY_WEIGHT);
Object o2Weight = o2.getProperty(PROPERTY_WEIGHT);
int o1Val = o1Weight == null ? 0
: o1Weight instanceof Integer ? ((Integer) o1Weight).intValue() : 0;
int o2Val = o2Weight == null ? 0
: o2Weight instanceof Integer ? ((Integer) o2Weight).intValue() : 0;
return o2Val - o1Val;
});
if (l.size() > 0) {
return getService(l.get(0));
Optional<ServiceReference<DirtyFileProvider>> reference = Arrays.stream(allRefs).max(BY_WEIGHT);
if (reference.isPresent()) {
return getService(reference.get());
}
}
return new DirtyFileProvider() {
@SuppressWarnings("unchecked")
@Override
public Map<IFile, IDocument> dirtyFiles() {
return Collections.EMPTY_MAP;
}
};
}

public void dispose() {
close();
return Collections::emptyMap;
}
}

0 comments on commit ef8188e

Please sign in to comment.