Updated Enum handling for a CTFTmfEventField
authorBernd Hufmann <bhufmann@gmail.com>
Tue, 21 Aug 2012 18:49:57 +0000 (14:49 -0400)
committerBernd Hufmann <bhufmann@gmail.com>
Tue, 21 Aug 2012 19:07:33 +0000 (15:07 -0400)
This change provides a way to extract the Enum long value and Enum
value name from the CTF EnumDefinition and store it in a
CtfTmfEventField object. Before this change the CtfTmfEventField
only contained the Enum value name.

For this change, the class Pair<A,B> which was part of the legacy
LTTng core is now integrated in TMF core.

Added JUnit tests for Pair class.

Change-Id: I948d3b9bdde83d5eda7c8397c1b9494e15f7706a
Signed-off-by: Bernd Hufmann <bhufmann@gmail.com>
Reviewed-on: https://git.eclipse.org/r/7326
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
IP-Clean: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Tested-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
IP-Clean: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/util/AllTests.java
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/util/PairTest.java [new file with mode: 0644]
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfEnumPair.java [new file with mode: 0644]
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/util/Pair.java [new file with mode: 0644]

index db1bf9dc935cefa16ad5e67ee6ca15f9f31abbbb..25889bed4a0dffed32f14350e11bf5173a074ae9 100644 (file)
@@ -8,7 +8,6 @@ import org.eclipse.linuxtools.internal.tmf.core.Activator;
 /**
  * <b><u>AllTests</u></b>
  * <p>
- * Implement me. Please.
  * <p>
  */
 @SuppressWarnings({ "nls" })
@@ -20,6 +19,7 @@ public class AllTests {
        public static Test suite() {
                TestSuite suite = new TestSuite("Test suite for " + Activator.PLUGIN_ID + ".util"); //$NON-NLS-1$);
                //$JUnit-BEGIN$
+               suite.addTestSuite(PairTest.class);
                suite.addTestSuite(TmfFixedArrayTest.class);
                //$JUnit-END$
                return suite;
diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/util/PairTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/util/PairTest.java
new file mode 100644 (file)
index 0000000..23ec715
--- /dev/null
@@ -0,0 +1,129 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *  Bernd Hufmann - Initial design and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.tmf.core.tests.util;
+
+import junit.framework.TestCase;
+
+import org.eclipse.linuxtools.tmf.core.util.Pair;
+
+/**
+ * Test case for Pair class.
+ *
+ * @author Bernd Hufmann
+ */
+@SuppressWarnings({ "nls", "javadoc" })
+public class PairTest extends TestCase {
+
+    // ------------------------------------------------------------------------
+    // Field(s)
+    // ------------------------------------------------------------------------
+    Pair<String, Long> fPair1 = new Pair<String, Long>("String 1", 1L);
+    Pair<String, Long> fPair2 = new Pair<String, Long>("String 2", 2L);
+
+    // ------------------------------------------------------------------------
+    // Housekeeping
+    // ------------------------------------------------------------------------
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    // ------------------------------------------------------------------------
+    // to String
+    // ------------------------------------------------------------------------
+
+    public void testToString() {
+        String result = fPair1.toString();
+        assertEquals("(String 1, 1)", result);
+    }
+
+    // ------------------------------------------------------------------------
+    // Setters/Getters
+    // ------------------------------------------------------------------------
+
+    public void testAccessors() {
+        Pair<String, Long> myPair = new Pair<String, Long>("String 1", 1L);
+        assertEquals("String 1", myPair.getFirst());
+        assertEquals(Long.valueOf(1L), myPair.getSecond());
+
+        myPair.setFirst("Hello");
+        assertEquals("Hello", myPair.getFirst());
+
+        myPair.setSecond(123L);
+        assertEquals(Long.valueOf(123L), myPair.getSecond());
+    }
+
+    // ------------------------------------------------------------------------
+    // equals
+    // ------------------------------------------------------------------------
+
+    public void testEqualsReflexivity() {
+        assertTrue("equals", fPair1.equals(fPair1));
+        assertTrue("equals", fPair2.equals(fPair2));
+
+        assertTrue("equals", !fPair1.equals(fPair2));
+        assertTrue("equals", !fPair2.equals(fPair1));
+    }
+
+    public void testEqualsSymmetry() {
+        Pair<String, Long> info1 = new Pair<String, Long>(fPair1.getFirst(), fPair1.getSecond().longValue());
+        Pair<String, Long> info2 = new Pair<String, Long>(fPair2.getFirst(), fPair2.getSecond().longValue());
+
+        assertTrue("equals", info1.equals(fPair1));
+        assertTrue("equals", fPair1.equals(info1));
+
+        assertTrue("equals", info2.equals(fPair2));
+        assertTrue("equals", fPair2.equals(info2));
+    }
+
+    public void testEqualsTransivity() {
+        Pair<String, Long> info1 = new Pair<String, Long>(fPair1.getFirst(), fPair1.getSecond().longValue());
+        Pair<String, Long> info2 = new Pair<String, Long>(fPair1.getFirst(), fPair1.getSecond().longValue());
+        Pair<String, Long> info3 = new Pair<String, Long>(fPair1.getFirst(), fPair1.getSecond().longValue());
+
+        assertTrue("equals", info1.equals(info2));
+        assertTrue("equals", info2.equals(info3));
+        assertTrue("equals", info1.equals(info3));
+    }
+
+    public void testEqualsNull() {
+        assertTrue("equals", !fPair1.equals(null));
+        assertTrue("equals", !fPair2.equals(null));
+    }
+
+    public void testEqualsDifferentObj() {
+        Pair<Long, String> info = new Pair<Long, String>(1L, "String1");
+        assertTrue("equals", !fPair1.equals(info));
+    }
+
+    // ------------------------------------------------------------------------
+    // hashCode
+    // ------------------------------------------------------------------------
+
+    public void testHashCode() {
+        Pair<String, Long> info1 = new Pair<String, Long>(fPair1.getFirst(), fPair1.getSecond().longValue());
+        Pair<String, Long> info2 = new Pair<String, Long>(fPair2.getFirst(), fPair2.getSecond().longValue());
+
+        assertTrue("hashCode", fPair1.hashCode() == info1.hashCode());
+        assertTrue("hashCode", fPair2.hashCode() == info2.hashCode());
+
+        assertTrue("hashCode", fPair1.hashCode() != info2.hashCode());
+        assertTrue("hashCode", fPair2.hashCode() != info1.hashCode());
+    }
+}
diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfEnumPair.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfEnumPair.java
new file mode 100644 (file)
index 0000000..38bc148
--- /dev/null
@@ -0,0 +1,63 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Bernd Hufmann - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.tmf.core.ctfadaptor;
+
+import org.eclipse.linuxtools.tmf.core.util.Pair;
+
+/**
+ * Pair of Enum value name and its long value.
+ *
+ * @author Bernd Hufmann
+ * @since 2.0
+ */
+public class CtfEnumPair extends Pair<String, Long> {
+
+    /**
+     * Constructs a CtfEnumPair
+     *
+     * @param strValue
+     *                  The first parameter of the pair (String)
+     * @param longValue
+     *                  The second parameter of the pair (Long)
+     */
+    public CtfEnumPair(String strValue, Long longValue) {
+        super(strValue, longValue);
+    }
+
+    /**
+     * Returns the String value of the Enum.
+     *
+     * @return the string value
+     */
+    public String getStringValue() {
+        return getFirst();
+    }
+
+    /**
+     * Returns the long value of the Enum.
+     *
+     * @return the Long value
+     */
+    public Long getLongValue() {
+        return getSecond();
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.linuxtools.tmf.core.util.Pair#toString()
+     */
+    @Override
+    public String toString() {
+        return getFirst();
+    }
+}
index 347765414915ebb0dcb8c2dded48c3632b1f5503..177654b049530ba9fefbaf0b44415f200382f027 100644 (file)
@@ -6,8 +6,11 @@
  * accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
  *
- * Contributors: Matthew Khouzam - Initial API and implementation
- * Contributors: Alexendre Montplaisir - Initial API and implementation
+ * Contributors:
+ *  Matthew Khouzam - Initial API and implementation
+ *  Alexendre Montplaisir - Initial API and implementation
+ *  Bernd Hufmann - Add Enum field handling
+ *
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
@@ -27,7 +30,7 @@ import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
 /**
  * The CTF implementation of the TMF event field model
  *
- * @version 1.0
+ * @version 2.0
  * @author Matthew Khouzam
  * @author Alexandre Montplaisir
  */
@@ -49,6 +52,9 @@ public abstract class CtfTmfEventField implements ITmfEventField {
     /** @since 2.0 */
     protected static final int FIELDTYPE_FLOAT = 3;
 
+    /** @since 2.0 */
+    protected static final int FIELDTYPE_ENUM = 4;
+
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
@@ -109,7 +115,7 @@ public abstract class CtfTmfEventField implements ITmfEventField {
 
         } else if (fieldDef instanceof EnumDefinition) {
             EnumDefinition enumDef = (EnumDefinition) fieldDef;
-            field = new CTFStringField(enumDef.getValue(), fieldName);
+            field = new CTFEnumField(new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue()), fieldName);
 
         } else if (fieldDef instanceof StringDefinition) {
             field = new CTFStringField(
@@ -186,6 +192,8 @@ public abstract class CtfTmfEventField implements ITmfEventField {
         case FIELDTYPE_FLOAT:
             return new CTFFloatField(((CTFFloatField) other).getValue(),
                     other.name);
+        case FIELDTYPE_ENUM:
+            return new CTFEnumField(((CTFEnumField) other).getValue(), other.name);
         default:
             return null;
         }
@@ -445,4 +453,42 @@ final class CTFFloatField extends CtfTmfEventField {
     }
 }
 
+/**
+ * The CTF field implementation for Enum fields
+ *
+ * @author Bernd Hufmann
+ */
+final class CTFEnumField extends CtfTmfEventField {
+
+    private final CtfEnumPair value;
+
+    /**
+     * Constructor for CTFEnumField.
+     *
+     * @param enumValue
+     *            The Enum value consisting of a pair of Enum value name and its long value
+     * @param name
+     *            The name of this field
+     */
+    CTFEnumField(CtfEnumPair enumValue, String name) {
+        super(name);
+        this.value = new CtfEnumPair(enumValue.getFirst(), enumValue.getSecond().longValue());
+    }
+
+    @Override
+    public int getFieldType() {
+        return FIELDTYPE_ENUM;
+    }
+
+    @Override
+    public  CtfEnumPair getValue() {
+        return this.value;
+    }
+
+    @Override
+    public String toString() {
+        return name + '=' + value.toString();
+    }
+}
+
 /* Implement other possible fields types here... */
diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/util/Pair.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/util/Pair.java
new file mode 100644 (file)
index 0000000..6bde879
--- /dev/null
@@ -0,0 +1,146 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Philippe Sawicki (INF4990.A2010@gmail.com)   - Initial API and implementation
+ *   Mathieu Denis    (mathieu.denis55@gmail.com) - Refactored code
+ *   Bernd Hufmann - Integrated to TMF, fixed hashCode() and equals() methods
+ *******************************************************************************/
+package org.eclipse.linuxtools.tmf.core.util;
+
+/**
+ * Pair utility class, encapsulates a pair of objects.
+ *
+ * @param <A>
+ *            The type of the first object.
+ * @param <B>
+ *            The type of the second object.
+ *
+ * @author Philippe Sawicki
+ * @since 2.0
+ */
+public class Pair<A, B> {
+
+    /**
+     * A reference to the first object.
+     */
+    protected A fFirst;
+    /**
+     * A reference to the second object.
+     */
+    protected B fSecond;
+
+    /**
+     * Constructor.
+     * @param first
+     *            The pair's first object.
+     * @param second
+     *            The pair's second object.
+     */
+    public Pair(A first, B second) {
+        fFirst = first;
+        fSecond = second;
+    }
+
+    /**
+     * Constructor.
+     */
+    public Pair() {
+        this(null, null);
+    }
+
+    /**
+     * Pair hash code.
+     */
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((fFirst == null) ? 0 : fFirst.hashCode());
+        result = prime * result + ((fSecond == null) ? 0 : fSecond.hashCode());
+        return result;
+    }
+
+    /**
+     * Object comparison.
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj == null) {
+            return false;
+        }
+
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+
+        Pair<?, ?> other = (Pair<?, ?>) obj;
+
+        if (fFirst == null) {
+            if (other.fFirst != null) {
+                return false;
+            }
+        } else if (!fFirst.equals(other.fFirst)) {
+            return false;
+        }
+        if (fSecond == null) {
+            if (other.fSecond != null) {
+                return false;
+            }
+        } else if (!fSecond.equals(other.fSecond)) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Object to string.
+     */
+    @Override
+    public String toString() {
+        return "(" + fFirst + ", " + fSecond + ")";  //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
+    }
+
+    /**
+     * Returns a reference to the pair's first object.
+     * @return A reference to the pair's first object.
+     */
+    public A getFirst() {
+        return fFirst;
+    }
+
+    /**
+     * Sets the pair's first object.
+     * @param first
+     *            The pair's first object.
+     */
+    public void setFirst(A first) {
+        fFirst = first;
+    }
+
+    /**
+     * Returns a reference to the pair's second object.
+     * @return A reference to the pair's second object.
+     */
+    public B getSecond() {
+        return fSecond;
+    }
+
+    /**
+     * Sets the pair's second object.
+     * @param second
+     *            The pair's second object.
+     */
+    public void setSecond(B second) {
+        fSecond = second;
+    }
+}
\ No newline at end of file
This page took 0.034669 seconds and 5 git commands to generate.