tmf: Rework TmfTraceUtils#resolveIntEventAspectOfClassForEvent
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Mon, 4 Apr 2016 19:49:43 +0000 (15:49 -0400)
committerAlexandre Montplaisir <alexmonthy@efficios.com>
Wed, 20 Apr 2016 17:00:24 +0000 (13:00 -0400)
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 <alexmonthy@efficios.com>
Reviewed-on: https://git.eclipse.org/r/69856
Reviewed-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceUtils.java

index 77ee466be9b9aa989aaf54afd7b04cb7450f6fb4..f2473991ae29d6fa2ec066b8f1b1618c0128a901 100644 (file)
@@ -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 <T extends ITmfEventAspect<?>> Object resolveEventAspectOfClassForEvent(
+    public static <T extends ITmfEventAspect<?>> @Nullable Object resolveEventAspectOfClassForEvent(
             ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) {
-        Iterable<ITmfEventAspect<?>> 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 <T extends ITmfEventAspect<Integer>> Integer resolveIntEventAspectOfClassForEvent(
+    public static <T extends ITmfEventAspect<Integer>> @Nullable Integer resolveIntEventAspectOfClassForEvent(
             ITmfTrace trace, Class<T> 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);
     }
 
     /**
This page took 0.027078 seconds and 5 git commands to generate.