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
}
/**
- * 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() {
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);
}
}
.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