aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Christian Kouamé2013-05-14 18:03:57 (EDT)
committerAlexandre Montplaisir2013-07-04 18:43:13 (EDT)
commit46201da19d01a67e96e2f235816bf02aa9d381bb (patch)
treec68efe5274d3c9a1d0bb88b6a8f23500a5a70455
parentdc57f341fa010f1c08cee5f1a38c6ab8e72a869c (diff)
downloadorg.eclipse.linuxtools-46201da19d01a67e96e2f235816bf02aa9d381bb.zip
org.eclipse.linuxtools-46201da19d01a67e96e2f235816bf02aa9d381bb.tar.gz
org.eclipse.linuxtools-46201da19d01a67e96e2f235816bf02aa9d381bb.tar.bz2
ctf: Move integer pretty-printing to IntegerDefinitionrefs/changes/89/14089/2
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>
-rw-r--r--lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java41
-rw-r--r--lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java72
-rw-r--r--lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java41
-rw-r--r--lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java56
4 files changed, 106 insertions, 104 deletions
diff --git a/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java b/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java
index 1ce81fd..74db10e 100644
--- a/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java
+++ b/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java
@@ -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);
+ }
}
diff --git a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java
index e0718b2..f9680ee 100644
--- a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java
+++ b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java
@@ -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;
}
}
diff --git a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java
index d322e15..31d1d86 100644
--- a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java
+++ b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java
@@ -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);
- }
}
diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java
index 3c4de5d..4b82324 100644
--- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java
+++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java
@@ -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();
}