From: Alexandre Montplaisir Date: Mon, 4 Apr 2016 19:49:43 +0000 (-0400) Subject: tmf: Rework TmfTraceUtils#resolveIntEventAspectOfClassForEvent X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=c15e897db268c29b97ad20c108bbb219d542728b;p=deliverable%2Ftracecompass.git tmf: Rework TmfTraceUtils#resolveIntEventAspectOfClassForEvent The generic type bound now ensures the aspect class passsed to resolveIntEventAspectOfClassForEvent() really resolves to an Integer, which means we can skip the check. Also rework both aspect-finding methods to use flat Stream operations. Change-Id: I8a341021699a69ac21b7a147c9ea91d58619fa99 Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/69856 Reviewed-by: Hudson CI Reviewed-by: Patrick Tasse Tested-by: Patrick Tasse --- diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceUtils.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceUtils.java index 77ee466be9..f2473991ae 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceUtils.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceUtils.java @@ -24,6 +24,7 @@ import java.util.Set; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.tracecompass.common.core.StreamUtils; import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; @@ -101,24 +102,19 @@ public final class TmfTraceUtils { * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null * for the event or {@code null} otherwise */ - public static @Nullable > Object resolveEventAspectOfClassForEvent( + public static > @Nullable Object resolveEventAspectOfClassForEvent( ITmfTrace trace, Class aspectClass, ITmfEvent event) { - Iterable> aspects = trace.getEventAspects(); - for (ITmfEventAspect aspect : aspects) { - if (aspectClass.isAssignableFrom(aspect.getClass())) { - Object obj = aspect.resolve(event); - if (obj != null) { - return obj; - } - } - } - return null; + return StreamUtils.getStream(trace.getEventAspects()) + .filter(aspect -> aspectClass.isAssignableFrom(aspect.getClass())) + .map(aspect -> aspect.resolve(event)) + .filter(obj -> obj != null) + .findFirst().orElse(null); } /** - * Return the first result of the first aspect that resolves as non null for - * the event received in parameter. The result is cast to an Integer if - * possible, otherwise null is returned. + * Return the first result of the first aspect that resolves as a non-null + * Integer for the event received in parameter. If no matching aspects are + * found then null is returned. * * @param trace * The trace for which you want the event aspects @@ -131,13 +127,14 @@ public final class TmfTraceUtils { * for the event or {@code null} otherwise * @since 2.0 */ - public static @Nullable > Integer resolveIntEventAspectOfClassForEvent( + public static > @Nullable Integer resolveIntEventAspectOfClassForEvent( ITmfTrace trace, Class aspectClass, ITmfEvent event) { - Object result = resolveEventAspectOfClassForEvent(trace, aspectClass, event); - if (result instanceof Integer) { - return (Integer) result; - } - return null; + return StreamUtils.getStream(trace.getEventAspects()) + .filter(aspect -> aspectClass.isAssignableFrom(aspect.getClass())) + /* Enforced by the T parameter bounding */ + .map(aspect -> (Integer) aspect.resolve(event)) + .filter(obj -> obj != null) + .findFirst().orElse(null); } /**