tmf: Add utility method to retrieve field values
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Tue, 5 Jul 2016 19:25:12 +0000 (15:25 -0400)
committerAlexandre Montplaisir <alexmonthy@efficios.com>
Wed, 6 Jul 2016 19:57:34 +0000 (15:57 -0400)
It's very common when handling trace events to expect fields
of a certain type. The common pattern to handle an event field
would be:

- field = content.getField()
- make sure field is not null
- value = field.getValue()
- make sure value is not null and of the expected type

Doing this every time we want to read a field is very
cumbersome, we can add a getFieldValue() utility method to
directly get the value into the expected type. So then we
just need to:

- content.getFieldValue(expectedType, fieldName);
- null-check the result

Change-Id: Icff8de7772d8ba8ed66880668cf14c26a07b2573
Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Reviewed-on: https://git.eclipse.org/r/76646
Reviewed-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/event/TmfEventFieldTest.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/event/ITmfEventField.java

index 10a00c8efb314e0fb0f46fcede2abc55f4a5e015..aef1f8cd2d5c609c7b9b1c294b5eb304e4a48c1b 100644 (file)
@@ -18,6 +18,7 @@ import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
@@ -355,4 +356,48 @@ public class TmfEventFieldTest {
         assertEquals("getFieldNames length", 2, names.size());
         assertArrayEquals(fFieldNames, names.toArray(new String[names.size()]));
     }
+
+    // ------------------------------------------------------------------------
+    // getFieldValue
+    // ------------------------------------------------------------------------
+
+    @Test
+    public void testGetFieldValueExists() {
+        String value = fRootField.getFieldValue(String.class, fFieldName1);
+        assertNotNull(value);
+        assertEquals(fValue1, value);
+    }
+
+    @Test
+    public void testGetFieldValueExistsButWrongType() {
+        Integer value = fRootField.getFieldValue(Integer.class, fFieldName1);
+        assertNull(value);
+    }
+
+    @Test
+    public void testGetFieldValueDoesntExist() {
+        String value = fRootField.getFieldValue(String.class, "no-field");
+        assertNull(value);
+    }
+
+    @Test
+    public void testGetFieldValueNullValue() {
+        ITmfEventField subField = new TmfEventField("subField", null, null);
+        ITmfEventField rootField = new TmfEventField("rootField", null,
+                new ITmfEventField[] { subField });
+
+        String value = rootField.getFieldValue(String.class, "subField");
+        assertNull(value);
+    }
+
+    @Test
+    public void testGetFieldValueAssignableValue() {
+        /*
+         * fValue2 is an Integer, but the method should allow us to use it as a
+         * Number for example.
+         */
+        Number value = fRootField.getFieldValue(Number.class, fFieldName2);
+        assertNotNull(value);
+        assertEquals(fValue2, value);
+    }
 }
index b8046f48af2f0779f6867d10d5af785e479c616d..7a06cf759e3a223edcca412e8eafae56ad92ebbe 100644 (file)
@@ -17,6 +17,7 @@ package org.eclipse.tracecompass.tmf.core.event;
 import java.util.Collection;
 
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 
 /**
  * The generic event payload in TMF. Each field can be either a terminal or
@@ -82,4 +83,35 @@ public interface ITmfEventField {
      */
     ITmfEventField getField(String @NonNull ... path);
 
+    /**
+     * Retrieve the value of a field, given an expected name/path and a value
+     * type.
+     *
+     * This is a utility method that will do the proper null and instanceof
+     * checks on the returned values of {@link #getField} and {@link #getValue}
+     * accordingly.
+     *
+     * @param type
+     *            The expected type of this field, to which the returned value
+     *            will then be cast to.
+     * @param fieldName
+     *            The name or path of the subfield to look for
+     * @return The value if a field with this name exists and the value is of
+     *         the correct type, or 'null' otherwise
+     * @since 2.1
+     */
+    default <T> @Nullable T getFieldValue(@NonNull Class<T> type, @NonNull String @NonNull ... fieldName) {
+        ITmfEventField field = getField(fieldName);
+        if (field == null) {
+            return null;
+        }
+        Object value = field.getValue();
+        if (value == null || !type.isAssignableFrom(value.getClass())) {
+            return null;
+        }
+        @SuppressWarnings("unchecked")
+        T ret = (T) value;
+        return ret;
+    }
+
 }
This page took 0.030226 seconds and 5 git commands to generate.