tmf: Retrieve aspects by name instead of class
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Thu, 11 May 2017 20:50:51 +0000 (16:50 -0400)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Fri, 12 May 2017 14:34:59 +0000 (10:34 -0400)
Change-Id: Iedf77bb7fbf97c643e384ffa6a47df32fa92f903
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/96901
Reviewed-by: Hudson CI
Reviewed-by: Jean-Christian Kouame <jean-christian.kouame@ericsson.com>
Tested-by: Jean-Christian Kouame <jean-christian.kouame@ericsson.com>
tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/TmfTraceUtilsTest.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceUtils.java

index cf89ffd4e43960d8ae07ecc1fb980afb756262de..5638bb0670b73da2f4bbc9a95cec63c9ca58ca4e 100644 (file)
@@ -85,15 +85,16 @@ public class TmfTraceUtilsTest {
     private static class TestEventAspect implements ITmfEventAspect<Integer> {
 
         public static final Integer RESOLVED_VALUE = 2;
+        public static final @NonNull String ASPECT_NAME = "test";
 
         @Override
         public @NonNull String getName() {
-            return "test";
+            return ASPECT_NAME;
         }
 
         @Override
         public @NonNull String getHelpText() {
-            return "test";
+            return ASPECT_NAME;
         }
 
         @Override
@@ -187,7 +188,29 @@ public class TmfTraceUtilsTest {
     }
 
     /**
-     * Test the {@link TmfTraceUtils#registerEventAspect(ITmfEventAspect)} method
+     * Test the
+     * {@link TmfTraceUtils#resolveAspectOfNameForEvent(ITmfTrace, String, ITmfEvent)}
+     * method.
+     */
+    @Test
+    public void testResolveEventAspectsOfNameForEvent() {
+        TmfTrace trace = fTrace;
+        assertNotNull(trace);
+
+        ITmfContext context = trace.seekEvent(0L);
+        ITmfEvent event = trace.getNext(context);
+        assertNotNull(event);
+
+        /* Make sure the CPU aspect returns the expected value */
+        Object cpuObj = TmfTraceUtils.resolveAspectOfNameForEvent(trace, "cpu", event);
+        assertNotNull(cpuObj);
+        assertEquals(1, cpuObj);
+
+    }
+
+    /**
+     * Test the {@link TmfTraceUtils#registerEventAspect(ITmfEventAspect)}
+     * method
      */
     @Test
     public void testAdditionalAspects() {
@@ -199,14 +222,23 @@ public class TmfTraceUtilsTest {
         ITmfEvent event = trace.getNext(context);
         assertNotNull(event);
 
+        // Make sure the aspect is not resolved
         Object obj = TmfTraceUtils.resolveEventAspectOfClassForEvent(trace, TestEventAspect.class, event);
         assertNull(obj);
 
+        obj = TmfTraceUtils.resolveAspectOfNameForEvent(trace, TestEventAspect.ASPECT_NAME, event);
+        assertNull(obj);
+
         // Register the aspect
         TmfTraceUtils.registerEventAspect(new TestEventAspect());
         // See that the aspect is resolved now
         obj = TmfTraceUtils.resolveEventAspectOfClassForEvent(trace, TestEventAspect.class, event);
         assertNotNull(obj);
         assertEquals(TestEventAspect.RESOLVED_VALUE, obj);
+
+        // See if it is resolved by name as well
+        obj = TmfTraceUtils.resolveAspectOfNameForEvent(trace, TestEventAspect.ASPECT_NAME, event);
+        assertNotNull(obj);
+        assertEquals(TestEventAspect.RESOLVED_VALUE, obj);
     }
 }
index 948611986faa70afe424b392ddcf596a06863206..b38a3ce7c04955fb1a70aa15687071daafd67d6f 100644 (file)
@@ -155,6 +155,44 @@ public final class TmfTraceUtils {
                 .findFirst().orElse(null);
     }
 
+    /**
+     * Return the first result of the first aspect that resolves as non null for
+     * the event received in parameter. If the returned value is not null, it
+     * can be safely cast to the aspect's class proper return type.
+     *
+     * @param trace
+     *            The trace for which you want the event aspects
+     * @param aspectName
+     *            The class of the aspect(s) to resolve
+     * @param event
+     *            The event for which to get the aspect
+     * @return The first result of the
+     *         {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null
+     *         for the event or {@code null} otherwise
+     * @since 2.4
+     */
+    public static @Nullable Object resolveAspectOfNameForEvent(ITmfTrace trace, String aspectName, ITmfEvent event) {
+        // First look in the trace aspects
+        Object value = StreamUtils.getStream(trace.getEventAspects())
+                .filter(aspect -> aspectName.equalsIgnoreCase(aspect.getName()))
+                .map(aspect -> aspect.resolve(event))
+                .filter(obj -> obj != null)
+                .findFirst().orElse(null);
+        if (value != null) {
+            return value;
+        }
+        // If the value is not found, look at the global aspects
+        for (ITmfEventAspect<?> aspect : EXTRA_ASPECTS) {
+            if (aspectName.equalsIgnoreCase(aspect.getName())) {
+                value = aspect.resolve(event);
+                if (value != null) {
+                    return value;
+                }
+            }
+        }
+        return null;
+    }
+
     /**
      * 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
This page took 0.028326 seconds and 5 git commands to generate.