ctf: Move integer pretty-printing to IntegerDefinition
authorJean-Christian Kouamé <kadjo.gwandy.jean-christian.kouame@ericsson.com>
Tue, 14 May 2013 22:03:57 +0000 (18:03 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Thu, 4 Jul 2013 22:43:13 +0000 (18:43 -0400)
Fixes bug 408046.

Change-Id: I4836f2026c70969a1d479cea2c66ef846cc95752
Signed-off-by: Jean-Christian Kouame
Reviewed-on: https://git.eclipse.org/r/14089
IP-Clean: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Tested-by: Hudson CI
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java

index 1ce81fda243948b341c6d1787e5641a33e40231d..74db10edcf9ec3cd8855a465e072d1b3e37fe3fe 100644 (file)
@@ -105,4 +105,45 @@ public class IntegerDefinitionTest {
         String result = fixture.toString();
         assertNotNull(result);
     }
+
+    /**
+     * Run the IntegerDefinition formatNumber(Long, int, boolean) method test
+     * for unsigned values.
+     */
+    @Test
+    public void testFormatNumber_unsignedLong() {
+
+        long unsignedLongValue = -64;
+        String result = IntegerDefinition.formatNumber(unsignedLongValue, 10, false);
+        // -64 + 2^64 = 18446744073709551552
+        assertEquals("18446744073709551552", result);
+
+        unsignedLongValue = -131940199973272L;
+        result = IntegerDefinition.formatNumber(unsignedLongValue, 10, false);
+        // -131940199973272l + 2^64 = 18446612133509578344
+        assertEquals("18446612133509578344", result);
+
+        unsignedLongValue = 123456789L;
+        result = IntegerDefinition.formatNumber(unsignedLongValue, 10, false);
+        assertEquals("123456789", result);
+    }
+
+    /**
+     * Run the IntegerDefinition formatNumber(Long, int, boolean) method test
+     * for signed values.
+     */
+    @Test
+    public void testFormatNumber_signedLong() {
+        long signedValue = -64L;
+        String result = IntegerDefinition.formatNumber(signedValue, 10, true);
+        assertEquals("-64", result);
+
+        signedValue = -131940199973272L;
+        result = IntegerDefinition.formatNumber(signedValue, 10, true);
+        assertEquals("-131940199973272", result);
+
+        signedValue = 123456789L;
+        result = IntegerDefinition.formatNumber(signedValue, 10, true);
+        assertEquals("123456789", result);
+    }
 }
index e0718b2c240bb3e6a3c04f9902ce293c1b64c37f..f9680ee107687d3f1e82be00073673a379b91f93 100644 (file)
@@ -12,6 +12,7 @@
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
+import java.math.BigInteger;
 import java.nio.ByteOrder;
 
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
@@ -19,8 +20,8 @@ import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 /**
  * A CTF integer definition.
  *
- * The definition of a integer basic data type. It will take the data
- * from a trace and store it (and make it fit) as a long.
+ * The definition of a integer basic data type. It will take the data from a
+ * trace and store it (and make it fit) as a long.
  *
  * @version 1.0
  * @author Matthew Khouzam
@@ -41,9 +42,13 @@ public class IntegerDefinition extends SimpleDatatypeDefinition {
 
     /**
      * Constructor
-     * @param declaration the parent declaration
-     * @param definitionScope the parent scope
-     * @param fieldName the field name
+     *
+     * @param declaration
+     *            the parent declaration
+     * @param definitionScope
+     *            the parent scope
+     * @param fieldName
+     *            the field name
      */
     public IntegerDefinition(IntegerDeclaration declaration,
             IDefinitionScope definitionScope, String fieldName) {
@@ -57,6 +62,7 @@ public class IntegerDefinition extends SimpleDatatypeDefinition {
 
     /**
      * Gets the value of the integer
+     *
      * @return the value of the integer (in long)
      */
     public long getValue() {
@@ -65,7 +71,9 @@ public class IntegerDefinition extends SimpleDatatypeDefinition {
 
     /**
      * Sets the value of an integer
-     * @param val the value
+     *
+     * @param val
+     *            the value
      */
     public void setValue(long val) {
         value = val;
@@ -76,8 +84,6 @@ public class IntegerDefinition extends SimpleDatatypeDefinition {
         return declaration;
     }
 
-
-
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
@@ -152,6 +158,54 @@ public class IntegerDefinition extends SimpleDatatypeDefinition {
             char c = (char) value;
             return Character.toString(c);
         }
-        return String.valueOf(value);
+        return formatNumber(value, declaration.getBase(), declaration.isSigned());
+    }
+
+    /**
+     * Print a numeric value as a string in a given base
+     *
+     * @param value
+     *            The value to print as string
+     * @param base
+     *            The base for this value
+     * @param signed
+     *            Is the value signed or not
+     * @return formatted number string
+     * @since 3.0
+     */
+    public static final String formatNumber(long value, int base, boolean signed) {
+        String s;
+        /* Format the number correctly according to the integer's base */
+        switch (base) {
+        case 2:
+            s = "0b" + Long.toBinaryString(value); //$NON-NLS-1$
+            break;
+        case 8:
+            s = "0" + Long.toOctalString(value); //$NON-NLS-1$
+            break;
+        case 16:
+            s = "0x" + Long.toHexString(value); //$NON-NLS-1$
+            break;
+        case 10:
+        default:
+            /* For non-standard base, we'll just print it as a decimal number */
+            if (!signed && value < 0) {
+                /*
+                 * Since there are no 'unsigned long', handle this case with
+                 * BigInteger
+                 */
+                BigInteger bigInteger = BigInteger.valueOf(value);
+                /*
+                 * we add 2^64 to the negative number to get the real unsigned
+                 * value
+                 */
+                bigInteger = bigInteger.add(BigInteger.valueOf(1).shiftLeft(64));
+                s = bigInteger.toString();
+            } else {
+                s = Long.toString(value);
+            }
+            break;
+        }
+        return s;
     }
 }
index d322e15e69685c37e34b7f7fac486571051eca1b..31d1d86d9af74bedae72e8bb936d9395027fd611 100644 (file)
@@ -194,45 +194,4 @@ public class CtfTmfEventFieldTest {
         CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
         assertEquals("test=float=9.551467814359616E-38", result.toString());
     }
-
-    /**
-     * Run the CtfTmfEventField formatNumber(Long, int, boolean) method test for
-     * unsigned values.
-     */
-    @Test
-    public void testFormatNumber_unsignedLong() {
-
-        long unsignedLongValue = -64;
-        String result = CtfTmfEventField.formatNumber(unsignedLongValue, 10, false);
-        // -64 + 2^64 = 18446744073709551552
-        assertEquals("18446744073709551552", result);
-
-        unsignedLongValue = -131940199973272L;
-        result = CtfTmfEventField.formatNumber(unsignedLongValue, 10, false);
-        // -131940199973272l + 2^64 = 18446612133509578344
-        assertEquals("18446612133509578344", result);
-
-        unsignedLongValue = 123456789L;
-        result = CtfTmfEventField.formatNumber(unsignedLongValue, 10, false);
-        assertEquals("123456789", result);
-    }
-
-    /**
-     * Run the CtfTmfEventField formatNumber(Long, int, boolean) method test for
-     * signed values.
-     */
-    @Test
-    public void testFormatNumber_signedLong() {
-        long signedValue = -64L;
-        String result = CtfTmfEventField.formatNumber(signedValue, 10, true);
-        assertEquals("-64", result);
-
-        signedValue = -131940199973272L;
-        result = CtfTmfEventField.formatNumber(signedValue, 10, true);
-        assertEquals("-131940199973272", result);
-
-        signedValue = 123456789L;
-        result = CtfTmfEventField.formatNumber(signedValue, 10, true);
-        assertEquals("123456789", result);
-    }
 }
index 3c4de5ddc53e5234499d29ac65ef9b68685c8b0b..4b82324fd3fa0168c6e5b0ab57103df029810a23 100644 (file)
@@ -16,7 +16,6 @@
 
 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
 
-import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -187,57 +186,6 @@ public abstract class CtfTmfEventField extends TmfEventField {
     public String toString() {
         return getName() + '=' + getFormattedValue();
     }
-
-    /**
-     * Print a numeric value as a string in a given base
-     *
-     * @param value
-     *            The value to print as string
-     * @param base
-     *            The base for this value
-     * @param signed
-     *            Is the value signed or not
-     * @return formatted number string
-     * @since 2.0
-     */
-    public final static String formatNumber(long value, int base, boolean signed) {
-        String s;
-
-        /* Format the number correctly according to the integer's base */
-        switch (base) {
-        case 2:
-            s = "0b" + Long.toBinaryString(value); //$NON-NLS-1$
-            break;
-        case 8:
-            s = "0" + Long.toOctalString(value); //$NON-NLS-1$
-
-            break;
-        case 16:
-            s = "0x" + Long.toHexString(value); //$NON-NLS-1$
-            break;
-        case 10:
-        default:
-            /* For non-standard base, we'll just print it as a decimal number */
-            if (!signed && value < 0) {
-                /* Since there are no 'unsigned long', handle this case with BigInteger */
-                BigInteger bigInteger = BigInteger.valueOf(value);
-                /*
-                 * powerOfTwo = 2^64 we add 2^64 to the negative number to get
-                 * the real unsigned value
-                 */
-                BigInteger powerOfTwo = (BigInteger.valueOf(Long.MAX_VALUE)).add(BigInteger.valueOf(1));
-                powerOfTwo = powerOfTwo.multiply(BigInteger.valueOf(2));
-                bigInteger = bigInteger.add(powerOfTwo);
-
-                s = bigInteger.toString();
-            } else {
-                s = Long.toString(value);
-            }
-            break;
-        }
-        return s;
-    }
-
 }
 
 /**
@@ -274,7 +222,7 @@ final class CTFIntegerField extends CtfTmfEventField {
 
     @Override
     public String getFormattedValue() {
-        return formatNumber(getValue(), base, signed);
+        return IntegerDefinition.formatNumber(getValue(), base, signed);
     }
 
 }
@@ -342,7 +290,7 @@ final class CTFIntegerArrayField extends CtfTmfEventField {
         if (formattedValue == null) {
             List<String> strings = new ArrayList<String>();
             for (Long value : getValue()) {
-                strings.add(formatNumber(value, base, signed));
+                strings.add(IntegerDefinition.formatNumber(value, base, signed));
             }
             formattedValue = strings.toString();
         }
This page took 0.03282 seconds and 5 git commands to generate.