From 68b18f2f1c642faf7ecd98c287d4897e9440116a Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Mon, 21 Jan 2013 15:34:26 -0500 Subject: [PATCH] tmf: Make CtfTmfEventField extend TmfEventField Part of bug 387929. Also swapped the (name, value) parameter order in the constructors to match the superclass. Change-Id: I71de4d41e6d22cb31262e8b0ccb63a8a910b4dbf Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/10054 Tested-by: Hudson CI Reviewed-by: Patrick Tasse IP-Clean: Patrick Tasse --- .../ctfadaptor/CtfTmfEventFieldTest.java | 55 ++-- .../tmf/core/ctfadaptor/CtfTmfEventField.java | 257 ++++-------------- 2 files changed, 76 insertions(+), 236 deletions(-) diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java index 1dbc2a9c94..6a512694e3 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java @@ -14,7 +14,6 @@ package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import java.nio.ByteOrder; @@ -41,16 +40,17 @@ import org.junit.Test; * @author ematkho * @version 1.0 */ +@SuppressWarnings("nls") public class CtfTmfEventFieldTest { - private static final String ROOT = "root"; //$NON-NLS-1$ - private static final String SEQ = "seq"; //$NON-NLS-1$ - private static final String ARRAY = "array"; //$NON-NLS-1$ - private static final String STR = "str"; //$NON-NLS-1$ - private static final String FLOAT = "float"; //$NON-NLS-1$ - private static final String LEN = "len"; //$NON-NLS-1$ - private static final String INT = "int"; //$NON-NLS-1$ - private static final String NAME = "test"; //$NON-NLS-1$ + private static final String ROOT = "root"; + private static final String SEQ = "seq"; + private static final String ARRAY = "array"; + private static final String STR = "str"; + private static final String FLOAT = "float"; + private static final String LEN = "len"; + private static final String INT = "int"; + private static final String NAME = "test"; private StructDefinition fixture; @@ -108,11 +108,9 @@ public class CtfTmfEventFieldTest { */ @Test public void testParseField_float() { - FloatDefinition fieldDef; - fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT); - CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, "_"+NAME); //$NON-NLS-1$ - String result2 = CtfTmfEventField.copyFrom(result).toString(); - assertEquals( result2, "test=9.551467814359616E-38"); //$NON-NLS-1$ + FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT); + CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, "_" + NAME); + assertEquals("test=9.551467814359616E-38", result.toString()); } /** @@ -120,10 +118,9 @@ public class CtfTmfEventFieldTest { */ @Test public void testParseField_array() { - CtfTmfEventField result; - result = CtfTmfEventField.parseField(fixture.lookupArray(ARRAY), NAME); - String result2 = CtfTmfEventField.copyFrom(result).toString(); - assertEquals( result2, "test=[2, 2]"); //$NON-NLS-1$ + Definition fieldDef = fixture.lookupArray(ARRAY); + CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); + assertEquals("test=[2, 2]", result.toString()); } /** @@ -133,8 +130,7 @@ public class CtfTmfEventFieldTest { public void testParseField_int() { Definition fieldDef = fixture.lookupDefinition(INT); CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); - String result2 =CtfTmfEventField.copyFrom(result).toString(); - assertEquals( result2, "test=02"); //$NON-NLS-1$ + assertEquals("test=02", result.toString()); } /** @@ -144,8 +140,7 @@ public class CtfTmfEventFieldTest { public void testParseField_sequence() { Definition fieldDef = fixture.lookupDefinition(SEQ); CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); - String result2 =CtfTmfEventField.copyFrom(result).toString(); - assertEquals( result2, "test=[2, 2]"); //$NON-NLS-1$ + assertEquals("test=[2, 2]", result.toString()); } /** @@ -155,8 +150,7 @@ public class CtfTmfEventFieldTest { public void testParseField_sequence_value() { Definition fieldDef = fixture.lookupDefinition(SEQ); CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); - String result2 = CtfTmfEventField.copyFrom(result).getValue().toString(); - assertEquals( result2, "[2, 2]"); //$NON-NLS-1$ + assertEquals("[2, 2]", result.getValue().toString()); } /** @@ -166,17 +160,6 @@ public class CtfTmfEventFieldTest { public void testParseField_string() { Definition fieldDef = fixture.lookupDefinition(STR); CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); - String result2 =CtfTmfEventField.copyFrom(result).toString(); - assertEquals( result2, "test="); //$NON-NLS-1$ - } - - /** - * Test the clone() method. - */ - @Test - public void testClone() { - Definition fieldDef = fixture.lookupDefinition(STR); - CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); - assertNotNull(result.clone()); + assertEquals("test=", result.toString()); } } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java index 75924c3ead..cdee2293c6 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Ericsson + * Copyright (c) 2011-2013 Ericsson * * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 which @@ -8,9 +8,8 @@ * * Contributors: * Matthew Khouzam - Initial API and implementation - * Alexendre Montplaisir - Initial API and implementation + * Alexendre Montplaisir - Initial API and implementation, extend TmfEventField * Bernd Hufmann - Add Enum field handling - * *******************************************************************************/ package org.eclipse.linuxtools.tmf.core.ctfadaptor; @@ -28,7 +27,7 @@ import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; -import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; +import org.eclipse.linuxtools.tmf.core.event.TmfEventField; /** * The CTF implementation of the TMF event field model @@ -37,7 +36,7 @@ import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; * @author Matthew Khouzam * @author Alexandre Montplaisir */ -public abstract class CtfTmfEventField implements ITmfEventField { +public abstract class CtfTmfEventField extends TmfEventField { // ------------------------------------------------------------------------ // Class attributes @@ -58,13 +57,6 @@ public abstract class CtfTmfEventField implements ITmfEventField { /** @since 2.0 */ protected static final int FIELDTYPE_ENUM = 4; - // ------------------------------------------------------------------------ - // Attributes - // ------------------------------------------------------------------------ - - /** The name of this field */ - protected final String name; - // ------------------------------------------------------------------------ // Constructor // ------------------------------------------------------------------------ @@ -75,23 +67,16 @@ public abstract class CtfTmfEventField implements ITmfEventField { * * @param name * The name of this field + * @param value + * The value of this field. Its type should match the field type. + * @since 2.0 */ - protected CtfTmfEventField(String name) { - /* Strip the underscore */ - if (name.startsWith("_")) { //$NON-NLS-1$ - this.name = name.substring(1); - } else { - this.name = name; - } - } - - // ------------------------------------------------------------------------ - // Getters/Setters/Predicates - // ------------------------------------------------------------------------ - - @Override - public String getName() { - return this.name; + protected CtfTmfEventField(String name, Object value) { + super(/* Strip the underscore from the field name if there is one */ + name.startsWith("_") ? name.substring(1) : name, //$NON-NLS-1$ + value, + /* CTF fields do not have sub-fields */ + null); } // ------------------------------------------------------------------------ @@ -115,15 +100,18 @@ public abstract class CtfTmfEventField implements ITmfEventField { if (fieldDef instanceof IntegerDefinition) { IntegerDefinition intDef = (IntegerDefinition) fieldDef; int base = intDef.getDeclaration().getBase(); - field = new CTFIntegerField(intDef.getValue(), fieldName, base); + field = new CTFIntegerField(fieldName, intDef.getValue(), base); } else if (fieldDef instanceof EnumDefinition) { EnumDefinition enumDef = (EnumDefinition) fieldDef; - field = new CTFEnumField(new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue()), fieldName); + field = new CTFEnumField(fieldName, new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue())); } else if (fieldDef instanceof StringDefinition) { - field = new CTFStringField( - ((StringDefinition) fieldDef).getValue(), fieldName); + field = new CTFStringField(fieldName, ((StringDefinition) fieldDef).getValue()); + + } else if (fieldDef instanceof FloatDefinition) { + FloatDefinition floatDef = (FloatDefinition) fieldDef; + field = new CTFFloatField(fieldName, floatDef.getValue()); } else if (fieldDef instanceof ArrayDefinition) { ArrayDefinition arrayDef = (ArrayDefinition) fieldDef; @@ -131,16 +119,15 @@ public abstract class CtfTmfEventField implements ITmfEventField { if (arrayDef.isString()) { /* This is an array of UTF-8 bytes, a.k.a. a String! */ - field = new CTFStringField(fieldDef.toString(), fieldName); + field = new CTFStringField(fieldName, fieldDef.toString()); } else if (arrayDecl.getElementType() instanceof IntegerDeclaration) { /* This is a an array of CTF Integers */ - long[] values = new long[arrayDecl.getLength()]; + List values = new ArrayList(arrayDecl.getLength()); for (int i = 0; i < arrayDecl.getLength(); i++) { - values[i] = ((IntegerDefinition) arrayDef.getElem(i)) - .getValue(); + values.add(((IntegerDefinition) arrayDef.getElem(i)).getValue()); } - field = new CTFIntegerArrayField(values, fieldName); + field = new CTFIntegerArrayField(fieldName, values); } /* Add other types of arrays here */ @@ -150,62 +137,27 @@ public abstract class CtfTmfEventField implements ITmfEventField { if (seqDef.getLength() == 0) { /* Some sequences have length = 0. Simply use an empty string */ - field = new CTFStringField("", fieldName); //$NON-NLS-1$ + field = new CTFStringField(fieldName, ""); //$NON-NLS-1$ } else if (seqDef.isString()) { /* Interpret this sequence as a String */ - field = new CTFStringField(seqDef.toString(), fieldName); + field = new CTFStringField(fieldName, seqDef.toString()); } else if (seqDecl.getElementType() instanceof IntegerDeclaration) { /* Sequence of integers => CTFIntegerArrayField */ - long[] values = new long[seqDef.getLength()]; + List values = new ArrayList(seqDef.getLength()); for (int i = 0; i < seqDef.getLength(); i++) { - values[i] = ((IntegerDefinition) seqDef.getElem(i)) - .getValue(); + values.add(((IntegerDefinition) seqDef.getElem(i)).getValue()); } - field = new CTFIntegerArrayField(values, fieldName); + field = new CTFIntegerArrayField(fieldName, values); } /* Add other Sequence types here */ - } else if (fieldDef instanceof FloatDefinition) { - FloatDefinition floatDef = (FloatDefinition) fieldDef; - field = new CTFFloatField(floatDef.getValue(), fieldName); } - return field; } - /** - * Copy factory. Create a new field by (deep-) copying the information in an - * existing one. - * - * @param other - * The other CtfTmfEventField to copy - * @return The new CtfTmfEventField - */ - public static CtfTmfEventField copyFrom(CtfTmfEventField other) { - switch (other.getFieldType()) { - case FIELDTYPE_INTEGER: - CTFIntegerField intOther = (CTFIntegerField) other; - return new CTFIntegerField(intOther.getValue(), intOther.name, - intOther.getBase()); - case FIELDTYPE_STRING: - return new CTFStringField(((CTFStringField) other).getValue(), - other.name); - case FIELDTYPE_INTEGER_ARRAY: - return new CTFIntegerArrayField( - ((CTFIntegerArrayField) other).getLongValues(), other.name); - 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; - } - } - @Override - public CtfTmfEventField clone() { - return CtfTmfEventField.copyFrom(this); + public String toString() { + return getName() + '=' + getValue().toString(); } // ------------------------------------------------------------------------ @@ -219,44 +171,6 @@ public abstract class CtfTmfEventField implements ITmfEventField { */ public abstract int getFieldType(); - /** - * Return this field's value. You can cast it to the correct type depending - * on what getFieldType says. - * - * @return The field's value - */ - @Override - public abstract Object getValue(); - - // ------------------------------------------------------------------------ - // Other methods defined by ITmfEventField, but not used here. - // CTF fields do not have sub-fields (yet!) - // ------------------------------------------------------------------------ - - @Override - public String[] getFieldNames() { - return null; - } - - @Override - public String getFieldName(int index) { - return null; - } - - @Override - public ITmfEventField[] getFields() { - return null; - } - - @Override - public ITmfEventField getField(String fieldName) { - return null; - } - - @Override - public ITmfEventField getField(int index) { - return null; - } } /** @@ -266,7 +180,6 @@ public abstract class CtfTmfEventField implements ITmfEventField { */ final class CTFIntegerField extends CtfTmfEventField { - private final long longValue; private final int base; /** @@ -278,21 +191,11 @@ final class CTFIntegerField extends CtfTmfEventField { * @param name * The name of this field */ - CTFIntegerField(long longValue, String name, int base) { - super(name); - this.longValue = longValue; + CTFIntegerField(String name, long longValue, int base) { + super(name, longValue); this.base = base; } - /** - * Return the integer's base. (Not made public until it's needed.) - * - * @return The base, usually 10 or 16. - */ - int getBase() { - return base; - } - @Override public int getFieldType() { return FIELDTYPE_INTEGER; @@ -300,34 +203,37 @@ final class CTFIntegerField extends CtfTmfEventField { @Override public Long getValue() { - return this.longValue; + return (Long) super.getValue(); } + /** + * Custom-format the integer values depending on their base. + */ @Override public String toString() { - StringBuilder sb = new StringBuilder(name); + StringBuilder sb = new StringBuilder(getName()); sb.append('='); /* Format the number correctly according to the integer's base */ switch (base) { case 2: sb.append("0b"); //$NON-NLS-1$ - sb.append(Long.toBinaryString(longValue)); + sb.append(Long.toBinaryString(getValue())); break; case 8: sb.append('0'); - sb.append(Long.toOctalString(longValue)); + sb.append(Long.toOctalString(getValue())); break; case 10: - sb.append(longValue); + sb.append(getValue()); break; case 16: sb.append("0x"); //$NON-NLS-1$ - sb.append(Long.toHexString(longValue)); + sb.append(Long.toHexString(getValue())); break; default: /* Non-standard base, we'll just print it as a decimal number */ - sb.append(longValue); + sb.append(getValue().toString()); break; } return sb.toString(); @@ -341,8 +247,6 @@ final class CTFIntegerField extends CtfTmfEventField { */ final class CTFStringField extends CtfTmfEventField { - private final String strValue; - /** * Constructor for CTFStringField. * @@ -351,9 +255,8 @@ final class CTFStringField extends CtfTmfEventField { * @param name * The name of this field */ - CTFStringField(String strValue, String name) { - super(name); - this.strValue = strValue; + CTFStringField(String name, String strValue) { + super(name, strValue); } @Override @@ -363,12 +266,7 @@ final class CTFStringField extends CtfTmfEventField { @Override public String getValue() { - return this.strValue; - } - - @Override - public String toString() { - return name + '=' + strValue; + return (String) super.getValue(); } } @@ -379,8 +277,6 @@ final class CTFStringField extends CtfTmfEventField { */ final class CTFIntegerArrayField extends CtfTmfEventField { - private final long[] longValues; - /** * Constructor for CTFIntegerArrayField. * @@ -390,9 +286,8 @@ final class CTFIntegerArrayField extends CtfTmfEventField { * @param name * The name of this field */ - CTFIntegerArrayField(long[] longValues, String name) { - super(name); - this.longValues = longValues; + CTFIntegerArrayField(String name, List longValues) { + super(name, longValues); } @Override @@ -400,32 +295,9 @@ final class CTFIntegerArrayField extends CtfTmfEventField { return FIELDTYPE_INTEGER_ARRAY; } - /** - * Gets the values of the array - * @return the values in the array - * - * @since 2.0 - */ - long[] getLongValues() { - return this.longValues; - } - @Override public List getValue() { - List retVal = new ArrayList(); - for( Long l : longValues){ - retVal.add(l); - } - return retVal; - } - - @Override - public String toString() { - StringBuffer buffer = new StringBuffer(); - buffer.append(name); - buffer.append('='); - buffer.append(getValue()); - return buffer.toString(); + return (List) super.getValue(); } } @@ -436,8 +308,6 @@ final class CTFIntegerArrayField extends CtfTmfEventField { */ final class CTFFloatField extends CtfTmfEventField { - private final Double value; - /** * Constructor for CTFFloatField. * @@ -446,9 +316,8 @@ final class CTFFloatField extends CtfTmfEventField { * @param name * The name of this field */ - protected CTFFloatField(double value, String name) { - super(name); - this.value = value; + protected CTFFloatField(String name, double value) { + super(name, value); } @Override @@ -458,12 +327,7 @@ final class CTFFloatField extends CtfTmfEventField { @Override public Double getValue() { - return this.value; - } - - @Override - public String toString() { - return name + '=' + value; + return (Double) super.getValue(); } } @@ -474,8 +338,6 @@ final class CTFFloatField extends CtfTmfEventField { */ final class CTFEnumField extends CtfTmfEventField { - private final CtfEnumPair value; - /** * Constructor for CTFEnumField. * @@ -484,9 +346,9 @@ final class CTFEnumField extends CtfTmfEventField { * @param name * The name of this field */ - CTFEnumField(CtfEnumPair enumValue, String name) { - super(name); - this.value = new CtfEnumPair(enumValue.getFirst(), enumValue.getSecond().longValue()); + CTFEnumField(String name, CtfEnumPair enumValue) { + super(name, new CtfEnumPair(enumValue.getFirst(), + enumValue.getSecond().longValue())); } @Override @@ -495,13 +357,8 @@ final class CTFEnumField extends CtfTmfEventField { } @Override - public CtfEnumPair getValue() { - return this.value; - } - - @Override - public String toString() { - return name + '=' + value.toString(); + public CtfEnumPair getValue() { + return (CtfEnumPair) super.getValue(); } } -- 2.34.1