Skip to content
This repository has been archived by the owner on Dec 9, 2018. It is now read-only.

Commit

Permalink
Use org.osgi.util.function.Predicate
Browse files Browse the repository at this point in the history
Also prepare for org.osgi.util.function 1.1 which adds throws
Exception to method signatures.

Signed-off-by: BJ Hargrave <bj@bjhargrave.com>
  • Loading branch information
bjhargrave committed Jul 20, 2016
1 parent e125d5b commit a0f3685
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 41 deletions.
23 changes: 14 additions & 9 deletions bndtools.builder/src/org/bndtools/builder/BuildListeners.java
Expand Up @@ -78,9 +78,9 @@ public void removedService(ServiceReference<BuildListener> reference, BuildListe

public void fireBuildStarting(final IProject project) {
this.project = project;
forEachListener(new Function<BuildListener,Object>() {
forEachListener(new Function<BuildListener,Void>() {
@Override
public Object apply(BuildListener listener) {
public Void apply(BuildListener listener) {
listener.buildStarting(project);
return null;
}
Expand All @@ -90,29 +90,34 @@ public Object apply(BuildListener listener) {
public void fireBuiltBundles(final IProject project, final IPath[] paths) {
this.project = project;
this.paths = paths;
forEachListener(new Function<BuildListener,Object>() {
forEachListener(new Function<BuildListener,Void>() {
@Override
public Object apply(BuildListener listener) {
public Void apply(BuildListener listener) {
listener.builtBundles(project, paths);
return null;
}
});
}

public void fireReleased(final IProject project) {
forEachListener(new Function<BuildListener,Object>() {
forEachListener(new Function<BuildListener,Void>() {
@Override
public Object apply(BuildListener listener) {
public Void apply(BuildListener listener) {
listener.released(project);
return null;
}
});
}

private void forEachListener(Function<BuildListener, ? extends Object> function) {
private void forEachListener(Function<BuildListener,Void> function) {
synchronized (listeners) {
for (BuildListener listener : listeners)
function.apply(listener);
for (BuildListener listener : listeners) {
try {
function.apply(listener);
} catch (Exception e) {
logger.logError("BuildListener error", e);
}
}
}
}

Expand Down
@@ -1,19 +1,19 @@
package org.bndtools.builder;

import org.bndtools.api.BndtoolsConstants;
import org.bndtools.utils.Predicate;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.launching.JavaRuntime;

import org.osgi.util.function.Predicate;

/**
* Filter for {@link IClasspathContainer} instances that removes the Bnd and JRE containers
*
*
* @author Neil Bartlett
*/
class ClasspathContainerFilter implements Predicate<IClasspathContainer> {

public boolean select(IClasspathContainer container) {
@Override
public boolean test(IClasspathContainer container) {
boolean result = true;
if (BndtoolsConstants.BND_CLASSPATH_ID.equals(container.getPath())) {
result = false;
Expand Down
Expand Up @@ -43,7 +43,7 @@
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.ui.IMarkerResolution;
import org.osgi.util.function.Function;
import org.osgi.util.function.Predicate;

import aQute.bnd.build.Project;
import aQute.bnd.differ.Baseline.Info;
Expand Down Expand Up @@ -148,9 +148,9 @@ ISourceRange findPackageInfoJavaVersionLocation(String packageName, ICompilation
parser.setResolveBindings(true);
CompilationUnit ast = (CompilationUnit) parser.createAST(null);

MemberValuePairLocationRetriever mvpRetriever = new MemberValuePairLocationRetriever(annot, new Function<String,Boolean>() {
MemberValuePairLocationRetriever mvpRetriever = new MemberValuePairLocationRetriever(annot, new Predicate<String>() {
@Override
public Boolean apply(String t) {
public boolean test(String t) {
return ANNOTATION_VERSION_BND.equals(t) || ANNOTATION_VERSION_OSGI.equals(t);
}
}, "value");
Expand Down
Expand Up @@ -13,7 +13,7 @@
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.osgi.util.function.Function;
import org.osgi.util.function.Predicate;

/**
* Visitor that will "visit" an ASTNode and its children until it finds the expected MemberValue pair to retain its
Expand All @@ -22,15 +22,15 @@
public class MemberValuePairLocationRetriever extends ASTVisitor {

private final IAnnotation javaAnnotation;
private final Function<String,Boolean> annotationNameMatch;
private final Predicate<String> annotationNameMatch;
private final String memberName;

private ISourceRange locatedSourceRange = null;

/**
* Constructor
*/
public MemberValuePairLocationRetriever(final IAnnotation javaAnnotation, final Function<String,Boolean> annotationNameMatch, final String memberName) {
public MemberValuePairLocationRetriever(final IAnnotation javaAnnotation, final Predicate<String> annotationNameMatch, final String memberName) {
this.javaAnnotation = javaAnnotation;
this.annotationNameMatch = annotationNameMatch;
this.memberName = memberName;
Expand Down Expand Up @@ -104,7 +104,13 @@ public boolean visit(SingleMemberAnnotation node) {
final IAnnotationBinding annotationBinding = node.resolveAnnotationBinding();
if (annotationBinding != null) {
final String nodeName = annotationBinding.getAnnotationType().getQualifiedName();
if (this.annotationNameMatch.apply(nodeName)) {
boolean match;
try {
match = this.annotationNameMatch.test(nodeName);
} catch (Exception e) {
match = false;
}
if (match) {
this.locatedSourceRange = new SourceRange(node.getValue().getStartPosition(), node.getValue().getLength());
}
}
Expand Down Expand Up @@ -137,4 +143,4 @@ public boolean visit(MemberValuePair node) {
return false;
}

}
}
10 changes: 5 additions & 5 deletions bndtools.core/src/bndtools/utils/SelectionUtils.java
Expand Up @@ -16,14 +16,14 @@
import java.util.Iterator;
import java.util.List;

import org.bndtools.utils.Predicate;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.osgi.util.function.Predicate;

public class SelectionUtils {

public static <T> Collection<T> getSelectionMembers(ISelection selection, Class<T> clazz) {
public static <T> Collection<T> getSelectionMembers(ISelection selection, Class<T> clazz) throws Exception {
return getSelectionMembers(selection, clazz, null);
}

Expand All @@ -43,7 +43,7 @@ public static <T> T adaptObject(Object obj, Class<T> clazz) {
return null;
}

public static <T> Collection<T> getSelectionMembers(ISelection selection, Class<T> clazz, Predicate< ? super T> filter) {
public static <T> Collection<T> getSelectionMembers(ISelection selection, Class<T> clazz, Predicate< ? super T> filter) throws Exception {
if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
return Collections.emptyList();
}
Expand All @@ -56,14 +56,14 @@ public static <T> Collection<T> getSelectionMembers(ISelection selection, Class<
if (clazz.isInstance(element)) {
@SuppressWarnings("unchecked")
T casted = (T) element;
if (filter == null || filter.select(casted)) {
if (filter == null || filter.test(casted)) {
result.add(casted);
}
} else if (element instanceof IAdaptable) {
@SuppressWarnings("unchecked")
T adapted = (T) ((IAdaptable) element).getAdapter(clazz);
if (adapted != null) {
if (filter == null || filter.select(adapted)) {
if (filter == null || filter.test(adapted)) {
result.add(adapted);
}
}
Expand Down
15 changes: 0 additions & 15 deletions bndtools.utils/src/org/bndtools/utils/Predicate.java

This file was deleted.

0 comments on commit a0f3685

Please sign in to comment.