package org.eclipse.linuxtools.tmf.core.tests.event;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.util.Collection;
+
import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
import org.junit.Test;
public void testTerminalStructConstructor() {
assertSame("getName", fFieldName1, fStructTerminalField1.getName());
assertNull("getValue", fStructTerminalField1.getValue());
- assertEquals("getFields", 0, fStructTerminalField1.getFields().length);
+ assertEquals("getFields", 0, fStructTerminalField1.getFields().size());
assertNull("getField(name)", fStructTerminalField1.getField(fFieldName1));
- assertNull("getField(index)", fStructTerminalField1.getField(0));
- assertEquals("getFieldNames", 0, fStructTerminalField1.getFieldNames().length);
- assertNull("getFieldName", fStructTerminalField1.getFieldName(-1));
- assertNull("getFieldName", fStructTerminalField1.getFieldName(0));
+ assertEquals("getFieldNames", 0, fStructTerminalField1.getFieldNames().size());
}
@Test
public void testNonTerminalStructConstructor() {
assertSame("getName", fStructRootFieldName, fStructRootField.getName());
assertNull("getValue", fStructRootField.getValue());
- assertEquals("getFields", 2, fStructRootField.getFields().length);
+ assertEquals("getFields", 2, fStructRootField.getFields().size());
assertSame("getField(name)", fStructTerminalField1, fStructRootField.getField(fFieldName1));
assertSame("getField(name)", fStructTerminalField2, fStructRootField.getField(fFieldName2));
- assertSame("getField(index)", fStructTerminalField1, fStructRootField.getField(0));
- assertSame("getField(index)", fStructTerminalField2, fStructRootField.getField(1));
-
- final String[] names = fStructRootField.getFieldNames();
- assertEquals("getFieldNames length", 2, names.length);
- for (int i = 0; i < names.length; i++) {
- assertSame("getFieldNames", fStructFieldNames[i], names[i]);
- assertSame("getFieldName", fFieldNames[i], fStructRootField.getFieldName(i));
- }
- assertNull("getFieldName", fStructRootField.getFieldName(-1));
- assertNull("getFieldName", fStructRootField.getFieldName(names.length));
+
+ final Collection<String> names = fStructRootField.getFieldNames();
+ assertEquals("getFieldNames length", 2, names.size());
+ assertArrayEquals(fStructFieldNames, names.toArray(new String[names.size()]));
}
@Test
public void testTerminalConstructor() {
assertSame("getName", fFieldName1, fField1.getName());
assertSame("getValue", fValue1, fField1.getValue());
- assertEquals("getFields", 0, fField1.getFields().length);
+ assertEquals("getFields", 0, fField1.getFields().size());
assertNull("getField(name)", fField1.getField(fFieldName1));
- assertNull("getField(index)", fField1.getField(0));
- assertEquals("getFieldNames", 0, fField1.getFieldNames().length);
- assertNull("getFieldName", fField1.getFieldName(0));
+ assertEquals("getFieldNames", 0, fField1.getFieldNames().size());
assertSame("getName", fFieldName2, fField2.getName());
assertSame("getValue", fValue2, fField2.getValue());
- assertEquals("getFields", 0, fField2.getFields().length);
+ assertEquals("getFields", 0, fField2.getFields().size());
assertNull("getField(name)", fField2.getField(fFieldName2));
- assertNull("getField(index)", fField2.getField(0));
- assertEquals("getFieldNames", 0, fField2.getFieldNames().length);
- assertNull("getFieldName", fField2.getFieldName(0));
}
@Test
public void testNonTerminalConstructor() {
assertSame("getName", fRootFieldName, fRootField.getName());
assertNull("getValue", fRootField.getValue());
- assertEquals("getFields", 2, fRootField.getFields().length);
+ assertEquals("getFields", 2, fRootField.getFields().size());
assertSame("getField(name)", fField1, fRootField.getField(fFieldName1));
assertSame("getField(name)", fField2, fRootField.getField(fFieldName2));
- assertSame("getField(index)", fField1, fRootField.getField(0));
- assertSame("getField(index)", fField2, fRootField.getField(1));
-
- final String[] names = fRootField.getFieldNames();
- assertEquals("getFieldNames length", 2, names.length);
- for (int i = 0; i < names.length; i++) {
- assertSame("getFieldNames", fFieldNames[i], names[i]);
- assertSame("getFieldName", fFieldNames[i], fRootField.getFieldName(i));
- }
- assertNull("getFieldName", fRootField.getFieldName(-1));
- assertNull("getFieldName", fRootField.getFieldName(names.length));
+
+ final Collection<String> names = fRootField.getFieldNames();
+ assertEquals("getFieldNames length", 2, names.size());
+ assertArrayEquals(fFieldNames, names.toArray(new String[names.size()]));
}
@Test
final TmfEventField copy = new TmfEventField(fField1);
assertSame("getName", fFieldName1, copy.getName());
assertSame("getValue", fValue1, copy.getValue());
- assertEquals("getFields", 0, copy.getFields().length);
+ assertEquals("getFields", 0, copy.getFields().size());
assertNull("getField(name)", copy.getField(fFieldName1));
- assertNull("getField(index)", copy.getField(0));
- assertEquals("getFieldNames", 0, copy.getFieldNames().length);
- assertNull("getFieldName", copy.getFieldName(0));
+ assertEquals("getFieldNames", 0, copy.getFieldNames().size());
}
@Test
public void testNonTerminalCopyConstructor() {
assertSame("getName", fRootFieldName, fRootField.getName());
assertNull("getValue", fRootField.getValue());
- assertEquals("getFields", 2, fRootField.getFields().length);
+ assertEquals("getFields", 2, fRootField.getFields().size());
assertSame("getField(name)", fField1, fRootField.getField(fFieldName1));
assertSame("getField(name)", fField2, fRootField.getField(fFieldName2));
- assertSame("getField(index)", fField1, fRootField.getField(0));
- assertSame("getField(index)", fField2, fRootField.getField(1));
-
- final String[] names = fRootField.getFieldNames();
- assertEquals("getFieldNames length", 2, names.length);
- for (int i = 0; i < names.length; i++) {
- assertSame("getFieldNames", fFieldNames[i], names[i]);
- assertSame("getFieldName", fFieldNames[i], fRootField.getFieldName(i));
- }
- assertNull("getFieldName", fRootField.getFieldName(names.length));
+
+ final Collection<String> names = fRootField.getFieldNames();
+ assertEquals("getFieldNames length", 2, names.size());
+ assertArrayEquals(fFieldNames, names.toArray(new String[names.size()]));
}
@Test
}
}
+ /**
+ * Test that we correctly fail to create a field with subfields having the
+ * same name.
+ */
+ @Test
+ public void testDuplicateFieldNames() {
+ ITmfEventField[] fields = {
+ new TmfEventField("samename", null, null),
+ new TmfEventField("samename", null, null)
+ };
+
+ try {
+ new TmfEventField("field", null, fields);
+ fail("TmfEventField: Duplicate field names");
+ } catch (IllegalArgumentException e) {
+ /* Expected exception */
+ }
+ }
+
// ------------------------------------------------------------------------
// hashCode
// ------------------------------------------------------------------------
@Test
public void testMakeRoot() {
ITmfEventField root = TmfEventField.makeRoot(fStructFieldNames);
- String[] names = root.getFieldNames();
- assertEquals("getFieldNames length", 2, names.length);
- for (int i = 0; i < names.length; i++) {
- assertSame("getFieldNames", fStructFieldNames[i], names[i]);
- assertSame("getFieldName", fStructFieldNames[i], root.getFieldName(i));
- assertNull("getValue", root.getField(i).getValue());
- }
- assertNull("getFieldName", root.getFieldName(-1));
- assertNull("getFieldName", root.getFieldName(names.length));
+ Collection<String> names = root.getFieldNames();
+ assertEquals("getFieldNames length", 2, names.size());
+ assertArrayEquals(fStructFieldNames, names.toArray(new String[names.size()]));
root = TmfEventField.makeRoot(fFieldNames);
names = root.getFieldNames();
- assertEquals("getFieldNames length", 2, names.length);
- for (int i = 0; i < names.length; i++) {
- assertSame("getFieldNames", fFieldNames[i], names[i]);
- assertSame("getFieldName", fFieldNames[i], root.getFieldName(i));
- assertNull("getValue", root.getField(i).getValue());
- }
- assertNull("getFieldName", root.getFieldName(-1));
- assertNull("getFieldName", root.getFieldName(names.length));
+ assertEquals("getFieldNames length", 2, names.size());
+ assertArrayEquals(fFieldNames, names.toArray(new String[names.size()]));
}
-
}
private final String fLabel0 = "label1";
private final String fLabel1 = "label2";
+ private final String fLabel2 = "label3";
private final String[] fLabels0 = new String[] { };
private final String[] fLabels1 = new String[] { fLabel0, fLabel1 };
- private final String[] fLabels2 = new String[] { fLabel1, fLabel0, fLabel1 };
+ private final String[] fLabels2 = new String[] { fLabel1, fLabel0, fLabel2 };
private final TmfEventType fType0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0));
private final TmfEventType fType1 = new TmfEventType(fContext1, fTypeId2, TmfEventField.makeRoot(fLabels1));
package org.eclipse.linuxtools.tmf.core.tests.event;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.util.Collection;
+
import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
private final String fLabel0 = "label1";
private final String fLabel1 = "label2";
+ private final String fLabel2 = "label3";
private final String[] fLabels0 = new String[] { };
private final String[] fLabels1 = new String[] { fLabel0, fLabel1 };
- private final String[] fLabels2 = new String[] { fLabel1, fLabel0, fLabel1 };
+ private final String[] fLabels2 = new String[] { fLabel1, fLabel0, fLabel2 };
private final ITmfEventType fType0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0));
private final ITmfEventType fType1 = new TmfEventType(fContext1, fTypeId2, TmfEventField.makeRoot(fLabels1));
assertEquals("getContext", ITmfEventType.DEFAULT_CONTEXT_ID, type.getContext());
assertEquals("getName", ITmfEventType.DEFAULT_TYPE_ID, type.getName());
assertNull("getRootField", type.getRootField());
- assertEquals("getFieldNames", 0, type.getFieldNames().length);
- assertNull("getFieldName", type.getFieldName(0));
+ assertEquals("getFieldNames", 0, type.getFieldNames().size());
}
@Test
assertEquals("getContext", fContext1, type0.getContext());
assertEquals("getName", fTypeId1, type0.getName());
assertEquals("getRootField", TmfEventField.makeRoot(fLabels0), type0.getRootField());
- final String[] labels0 = type0.getFieldNames();
- assertEquals("getFieldNames length", fLabels0.length, labels0.length);
- for (int i = 0; i < labels0.length; i++) {
- assertEquals("getFieldNames", fLabels0[i], labels0[i]);
- }
- assertNull("getFieldName", type0.getFieldName(labels0.length));
+ final Collection<String> labels0 = type0.getFieldNames();
+ assertEquals("getFieldNames length", fLabels0.length, labels0.size());
+ assertArrayEquals(fLabels0, labels0.toArray(new String[labels0.size()]));
final ITmfEventType type1 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1));
assertEquals("getContext", fContext1, type1.getContext());
assertEquals("getName", fTypeId1, type1.getName());
assertEquals("getRootField", TmfEventField.makeRoot(fLabels1), type1.getRootField());
- final String[] labels1 = type1.getFieldNames();
- assertEquals("getFieldNames length", fLabels1.length, labels1.length);
- for (int i = 0; i < labels1.length; i++) {
- assertEquals("getFieldNames", fLabels1[i], labels1[i]);
- }
- assertNull("getFieldName", type1.getFieldName(labels1.length));
+ final Collection<String> labels1 = type1.getFieldNames();
+ assertEquals("getFieldNames length", fLabels1.length, labels1.size());
+ assertArrayEquals(fLabels1, labels1.toArray(new String[labels1.size()]));
final ITmfEventType type2 = new TmfEventType(fContext2, fTypeId2, TmfEventField.makeRoot(fLabels2));
assertEquals("getContext", fContext2, type2.getContext());
assertEquals("getName", fTypeId2, type2.getName());
assertEquals("getRootField", TmfEventField.makeRoot(fLabels2), type2.getRootField());
- final String[] labels2 = type2.getFieldNames();
- assertEquals("getFieldNames length", fLabels2.length, labels2.length);
- for (int i = 0; i < labels2.length; i++) {
- assertEquals("getFieldNames", fLabels2[i], labels2[i]);
- }
- assertNull("getFieldName", type2.getFieldName(labels2.length));
+ final Collection<String> labels2 = type2.getFieldNames();
+ assertEquals("getFieldNames length", fLabels2.length, labels2.size());
+ assertArrayEquals(fLabels2, labels2.toArray(new String[labels2.size()]));
}
@Test
assertEquals("getContext", fContext1, copy.getContext());
assertEquals("getName", fTypeId1, copy.getName());
assertEquals("getRootField", TmfEventField.makeRoot(fLabels1), copy.getRootField());
- final String[] labels1 = copy.getFieldNames();
- assertEquals("getFieldNames length", fLabels1.length, labels1.length);
- for (int i = 0; i < labels1.length; i++) {
- assertEquals("getFieldNames", fLabels1[i], labels1[i]);
- }
- assertNull("getFieldName", copy.getFieldName(labels1.length));
+ final Collection<String> labels1 = copy.getFieldNames();
+ assertEquals("getFieldNames length", fLabels1.length, labels1.size());
+ assertArrayEquals(fLabels1, labels1.toArray(new String[labels1.size()]));
}
@Test
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import java.util.List;
+
import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
import org.eclipse.linuxtools.tmf.core.trace.text.TextTraceEventContent;
import org.eclipse.linuxtools.tmf.tests.stubs.trace.text.SyslogEventType;
// ------------------------------------------------------------------------
// Members
// ------------------------------------------------------------------------
+
private TextTraceEventContent fEventContent1;
+ private TextTraceEventContent fEventContent1Clone;
private TextTraceEventContent fEventContent2;
+ private TextTraceEventContent fEventContent2Clone;
public TextTraceEventContentTest () {
fEventContent1 = new TextTraceEventContent(SyslogEventType.LABELS);
fEventContent1.setFieldValue(Index.LOGGER, "LoggerA");
fEventContent1.setFieldValue(Index.MESSAGE, "MessageA");
+ fEventContent1Clone = new TextTraceEventContent(SyslogEventType.LABELS);
+ fEventContent1Clone.setValue("CONTENT");
+ fEventContent1Clone.setFieldValue(Index.TIMESTAMP, "Jan 1 01:01:01");
+ fEventContent1Clone.setFieldValue(Index.HOST, "HostA");
+ fEventContent1Clone.setFieldValue(Index.LOGGER, "LoggerA");
+ fEventContent1Clone.setFieldValue(Index.MESSAGE, "MessageA");
+
fEventContent2 = new TextTraceEventContent(SyslogEventType.LABELS);
fEventContent2.setFieldValue(SyslogEventType.LABELS[0], "Jan 1 02:02:02");
fEventContent2.setFieldValue(SyslogEventType.LABELS[1], "HostB");
StringBuffer buffer = new StringBuffer();
buffer.append("Message B");
fEventContent2.setFieldValue(SyslogEventType.LABELS[3], buffer);
+
+ fEventContent2Clone = new TextTraceEventContent(SyslogEventType.LABELS);
+ fEventContent2Clone.setFieldValue(SyslogEventType.LABELS[0], "Jan 1 02:02:02");
+ fEventContent2Clone.setFieldValue(SyslogEventType.LABELS[1], "HostB");
+ fEventContent2Clone.setFieldValue(SyslogEventType.LABELS[2], "LoggerB");
+ buffer = new StringBuffer();
+ buffer.append("Message B");
+ fEventContent2Clone.setFieldValue(SyslogEventType.LABELS[3], buffer);
}
public void testConstructorConstructor() {
SyslogEventType eventType = SyslogEventType.INSTANCE;
assertEquals("getTypeId", "Syslog", eventType.getName());
assertNotNull ("instance", eventType);
- assertEquals("getLabel", "Timestamp", eventType.getFieldNames()[0]);
- assertEquals("getLabel", "Host", eventType.getFieldNames()[1]);
- assertEquals("getLabel", "Logger", eventType.getFieldNames()[2]);
- assertEquals("getLabel", "Message", eventType.getFieldNames()[3]);
+ assertTrue (eventType.getFieldNames().contains("Timestamp"));
+ assertTrue (eventType.getFieldNames().contains("Host"));
+ assertTrue (eventType.getFieldNames().contains("Logger"));
+ assertTrue (eventType.getFieldNames().contains("Message"));
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
@Test
- public void testEqualsReflexivity() {
- TextTraceEventContent content1 = fEventContent1.clone();
- TextTraceEventContent content2 = fEventContent2.clone();
- assertEquals("equals", content1, content1);
- assertEquals("equals", content2, content2);
-
- assertTrue("equals", !content1.equals(content2));
- assertTrue("equals", !content2.equals(content1));
- }
+ public void testEquals() {
+ assertEquals("equals", fEventContent1, fEventContent1);
+ assertEquals("equals", fEventContent2, fEventContent2);
- @Test
- public void testEqualsTransivity() {
- TextTraceEventContent content1 = fEventContent1.clone();
- TextTraceEventContent content2 = fEventContent1.clone();
- TextTraceEventContent content3 = fEventContent1.clone();
-
- assertEquals("equals", content1, content2);
- assertEquals("equals", content2, content3);
- assertEquals("equals", content1, content3);
+ assertTrue("equals", !fEventContent1.equals(fEventContent2));
+ assertTrue("equals", !fEventContent2.equals(fEventContent1));
+
+ assertEquals("equals", fEventContent1, fEventContent1Clone);
+ assertEquals("equals", fEventContent2, fEventContent2Clone);
}
@Test
// ------------------------------------------------------------------------
// hashCode
// ------------------------------------------------------------------------
+
@Test
public void testHashCode() {
- TextTraceEventContent content1 = fEventContent1.clone();
- TextTraceEventContent content2 = fEventContent2.clone();
- assertEquals("hashCode", fEventContent1.hashCode(), content1.hashCode());
- assertEquals("hashCode", fEventContent2.hashCode(), content2.hashCode());
+ assertEquals("hashCode", fEventContent1.hashCode(), fEventContent1Clone.hashCode());
+ assertEquals("hashCode", fEventContent2.hashCode(), fEventContent2Clone.hashCode());
- assertTrue("hashCode", fEventContent1.hashCode() != content2.hashCode());
- assertTrue("hashCode", fEventContent2.hashCode() != content1.hashCode());
+ assertNotEquals("hashCode", fEventContent1.hashCode(), fEventContent2.hashCode());
+ assertNotEquals("hashCode", fEventContent2.hashCode(), fEventContent1.hashCode());
}
// ------------------------------------------------------------------------
@Test
public void testGetFields() {
- ITmfEventField[] fields = fEventContent1.getFields();
- assertEquals(4, fields.length);
- assertEquals("getFields:TIMESTAMP", SyslogEventType.LABELS[0], fields[Index.TIMESTAMP].getName());
- assertEquals("getFields:TIMESTAMP", "Jan 1 01:01:01", fields[Index.TIMESTAMP].getValue());
- assertEquals("getFields:HOST", SyslogEventType.LABELS[1], fields[Index.HOST].getName());
- assertEquals("getFields:HOST", "HostA", fields[Index.HOST].getValue());
- assertEquals("getFields:LOGGER", SyslogEventType.LABELS[2], fields[Index.LOGGER].getName());
- assertEquals("getFields:LOGGER", "LoggerA", fields[Index.LOGGER].getValue());
- assertEquals("getFields:MESSAGE", SyslogEventType.LABELS[3], fields[Index.MESSAGE].getName());
- assertEquals("getFields:MESSAGE", "MessageA", fields[Index.MESSAGE].getValue());
+ List<TextTraceEventContent> fields = fEventContent1.getFields();
+ assertEquals(4, fields.size());
+ assertEquals("getFields:TIMESTAMP", SyslogEventType.LABELS[0], fields.get(Index.TIMESTAMP).getName());
+ assertEquals("getFields:TIMESTAMP", "Jan 1 01:01:01", fields.get(Index.TIMESTAMP).getValue());
+ assertEquals("getFields:HOST", SyslogEventType.LABELS[1], fields.get(Index.HOST).getName());
+ assertEquals("getFields:HOST", "HostA", fields.get(Index.HOST).getValue());
+ assertEquals("getFields:LOGGER", SyslogEventType.LABELS[2], fields.get(Index.LOGGER).getName());
+ assertEquals("getFields:LOGGER", "LoggerA", fields.get(Index.LOGGER).getValue());
+ assertEquals("getFields:MESSAGE", SyslogEventType.LABELS[3], fields.get(Index.MESSAGE).getName());
+ assertEquals("getFields:MESSAGE", "MessageA", fields.get(Index.MESSAGE).getValue());
}
@Test
assertNull(field);
}
- @Test
- public void testGetFieldWithIndex() {
- ITmfEventField field = fEventContent1.getField(0);
- assertEquals("getField:TIMESTAMP", SyslogEventType.LABELS[0], field.getName());
- assertEquals("getField:TIMESTAMP", "Jan 1 01:01:01", field.getValue());
-
- field = fEventContent1.getField(1);
- assertEquals("getField:HOST", SyslogEventType.LABELS[1], field.getName());
- assertEquals("getField:HOST", "HostA", field.getValue());
-
- field = fEventContent1.getField(2);
- assertEquals("getField:LOGGER", SyslogEventType.LABELS[2], field.getName());
- assertEquals("getField:LOGGER", "LoggerA", field.getValue());
-
- field = fEventContent1.getField(3);
- assertEquals("getField:MESSAGE", SyslogEventType.LABELS[3], field.getName());
- assertEquals("getField:MESSAGE", "MessageA", field.getValue());
-
- field = fEventContent1.getField(4);
- assertNull(field);
- }
-
@Test
public void testGetFormattedValue() {
assertEquals("CONTENT", fEventContent1.getFormattedValue());
@Test
public void testGetFieldNames() {
String[] labels = {"Timestamp", "Host", "Logger", "Message"};
- String[] names = fEventContent1.getFieldNames();
- assertArrayEquals(labels, names);
+ List<String> names = fEventContent1.getFieldNames();
+ assertArrayEquals(labels, names.toArray(new String[names.size()]));
}
}
org.eclipse.linuxtools.tmf.core.trace.text,
org.eclipse.linuxtools.tmf.core.uml2sd,
org.eclipse.linuxtools.tmf.core.util
-Import-Package: com.google.common.collect
+Import-Package: com.google.common.base,
+ com.google.common.collect
/*******************************************************************************
- * Copyright (c) 2012, 2013 Ericsson
+ * Copyright (c) 2012, 2014 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
*
* Contributors:
* Francois Chouinard - Initial API and implementation
+ * Alexandre Montplaisir - Removed arrays from the API
*******************************************************************************/
package org.eclipse.linuxtools.tmf.core.event;
+import java.util.Collection;
+
+import org.eclipse.jdt.annotation.NonNull;
+
/**
* The generic event payload in TMF. Each field can be either a terminal or
* further decomposed into subfields.
/**
* The root field id (the main container)
*/
- public static final String ROOT_FIELD_ID = ":root:"; //$NON-NLS-1$
+ public static final @NonNull String ROOT_FIELD_ID = ":root:"; //$NON-NLS-1$
// ------------------------------------------------------------------------
// Getters
String getFormattedValue();
/**
- * @return the list of subfield names (empty array if none)
- */
- String[] getFieldNames();
-
- /**
- * @param index The index of the field
- * @return the nth field name (null if absent or inexistent)
+ * Return the subfield names. The iteration order is the same as
+ * {@link #getFields()}. The returned Collection is immutable.
+ *
+ * @return The subfield names (empty Collection if none)
+ * @since 3.0
*/
- String getFieldName(int index);
+ Collection<String> getFieldNames();
/**
- * @return the list of subfields (empty array if none)
+ * Return the subfield. The iteration order is the same as
+ * {@link #getFieldNames()}. The returned Collection is immutable.
+ *
+ * @return The subfields (empty Collection if none)
+ * @since 3.0
*/
- ITmfEventField[] getFields();
+ Collection<? extends ITmfEventField> getFields();
/**
* @param name The name of the field
*/
ITmfEventField getField(String name);
- /**
- * @param index The index of the field to return
- * @return a specific subfield by index (null if absent or inexistent)
- */
- ITmfEventField getField(int index);
-
/**
* Gets the a sub-field of this field, which may be multiple levels down.
*
* found
* @since 3.0
*/
- ITmfEventField getSubField(String[] path);
+ ITmfEventField getSubField(String... path);
}
package org.eclipse.linuxtools.tmf.core.event;
+import java.util.Collection;
+
/**
* The generic event type in TMF. It contains a reference to the full field structure
* for that event type.
/**
* @return the event field names (labels)
+ * @since 3.0
*/
- String[] getFieldNames();
-
- /**
- * @param index the event field index
- * @return the corresponding event field label
- */
- String getFieldName(int index);
+ Collection<String> getFieldNames();
}
/*******************************************************************************
- * Copyright (c) 2009, 2013 Ericsson
+ * Copyright (c) 2009, 2014 Ericsson
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0 which
package org.eclipse.linuxtools.tmf.core.event;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.Collection;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableMap;
/**
* A basic implementation of ITmfEventField.
// Attributes
// ------------------------------------------------------------------------
- private final String fName;
- private final Object fValue;
- private final ITmfEventField[] fFields;
-
- private final String[] fFieldNames;
- private final Map<String, ITmfEventField> fNameMapping;
+ private final @NonNull String fName;
+ private final @Nullable Object fValue;
+ private final @NonNull ImmutableMap<String, ITmfEventField> fFields;
// ------------------------------------------------------------------------
// Constructors
/**
* Full constructor
*
- * @param name the event field id
- * @param value the event field value
- * @param fields the list of subfields
+ * @param name
+ * the event field id
+ * @param value
+ * the event field value
+ * @param fields
+ * the list of subfields
+ * @throws IllegalArgumentException
+ * If 'name' is null, or if 'fields' has duplicate field names.
*/
- public TmfEventField(final String name, final Object value, final ITmfEventField[] fields) {
+ @SuppressWarnings("null") /* ImmutableMap methods do not return @NonNull */
+ public TmfEventField(String name, @Nullable Object value, @Nullable ITmfEventField[] fields) {
if (name == null) {
throw new IllegalArgumentException();
}
fName = name;
fValue = value;
- fFields = fields;
- /* Fill the fFieldNames and fNameMapping structures */
- final int nbFields = (fFields != null) ? fFields.length : 0;
- fFieldNames = new String[nbFields];
- fNameMapping = new HashMap<>();
-
- for (int i = 0; i < nbFields; i++) {
- final String curName = fFields[i].getName();
- fFieldNames[i] = curName;
- fNameMapping.put(curName, fFields[i]);
+ if (fields == null) {
+ fFields = ImmutableMap.of();
+ } else {
+ /* Java 8 streams will make this even more simple! */
+ ImmutableMap.Builder<String, ITmfEventField> mapBuilder = new ImmutableMap.Builder<>();
+ for (ITmfEventField field : fields) {
+ final String curName = field.getName();
+ mapBuilder.put(curName, field);
+ }
+ fFields = mapBuilder.build();
}
}
fName = field.fName;
fValue = field.fValue;
fFields = field.fFields;
- fFieldNames = field.fFieldNames;
- fNameMapping = field.fNameMapping;
}
// ------------------------------------------------------------------------
return fValue;
}
+ /**
+ * @since 3.0
+ */
@Override
- public String[] getFieldNames() {
- return fFieldNames;
- }
-
- @Override
- public String getFieldName(final int index) {
- final ITmfEventField field = getField(index);
- if (field != null) {
- return field.getName();
- }
- return null;
+ public Collection<String> getFieldNames() {
+ return fFields.keySet();
}
+ /**
+ * @since 3.0
+ */
@Override
- public ITmfEventField[] getFields() {
- return (fFields != null) ? fFields : new ITmfEventField[0];
+ public Collection<ITmfEventField> getFields() {
+ return fFields.values();
}
@Override
public ITmfEventField getField(final String name) {
- return fNameMapping.get(name);
- }
-
- @Override
- public ITmfEventField getField(final int index) {
- if (fFields != null && index >= 0 && index < fFields.length) {
- return fFields[index];
- }
- return null;
+ return fFields.get(name);
}
/**
* @since 3.0
*/
@Override
- public ITmfEventField getSubField(final String[] names) {
+ public ITmfEventField getSubField(final String... names) {
ITmfEventField field = this;
for (String name : names) {
field = field.getField(name);
@Override
public int hashCode() {
+ Object value = fValue;
final int prime = 31;
int result = 1;
result = prime * result + fName.hashCode();
- result = prime * result + ((fValue == null) ? 0 : fValue.hashCode());
+ result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
if (!fName.equals(other.fName)) {
return false;
}
- if (fValue == null) {
+ Object value = this.fValue;
+ if (value == null) {
if (other.fValue != null) {
return false;
}
- } else if (!fValue.equals(other.fValue)) {
+ } else if (!value.equals(other.fValue)) {
return false;
}
return true;
ret.append('=');
ret.append(fValue);
- if (fFields != null && fFields.length > 0) {
+ if (!fFields.isEmpty()) {
/*
* In addition to its own name/value, this field also has
* sub-fields.
}
private void appendSubFields(StringBuilder sb) {
- ITmfEventField field;
- for (int i = 0; i < getFields().length; i++) {
- field = getFields()[i];
- if (i != 0) {
- sb.append(", ");//$NON-NLS-1$
- }
- sb.append(field.toString());
- }
+ Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
+ sb.append(joiner.join(getFields()));
}
/**
package org.eclipse.linuxtools.tmf.core.event;
+import java.util.Collection;
+import java.util.Collections;
+
/**
* A basic implementation of ITmfEventType.
*
return fRootField;
}
+ /**
+ * @since 3.0
+ */
@Override
- public String[] getFieldNames() {
- return (fRootField != null) ? fRootField.getFieldNames() : new String[0];
- }
-
- @Override
- public String getFieldName(final int index) {
- return (fRootField != null) ? fRootField.getFieldName(index) : null;
+ public Collection<String> getFieldNames() {
+ return (fRootField != null) ? fRootField.getFieldNames() : Collections.EMPTY_SET;
}
// ------------------------------------------------------------------------
package org.eclipse.linuxtools.tmf.core.trace.text;
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
/**
*
* @since 3.0
*/
-public class TextTraceEventContent implements ITmfEventField, Cloneable {
+public class TextTraceEventContent implements ITmfEventField {
- private String fName;
- private Object fValue;
- private TextTraceEventContent[] fFields;
+ private final @NonNull String fName;
+ private final @NonNull List<TextTraceEventContent> fFields;
+
+ private @Nullable Object fValue;
// ------------------------------------------------------------------------
// Constructors
* the array of field names
*/
public TextTraceEventContent(String[] fieldNames) {
- this(ITmfEventField.ROOT_FIELD_ID);
- fFields = new TextTraceEventContent[fieldNames.length];
- for (int i = 0; i < fFields.length; i++) {
- fFields[i] = new TextTraceEventContent(fieldNames[i]);
+ fName = ITmfEventField.ROOT_FIELD_ID;
+ fValue = null;
+ fFields = new ArrayList<>(fieldNames.length);
+ for (String fieldName : fieldNames) {
+ if (fieldName == null) {
+ throw new IllegalArgumentException("Null field name not allowed"); //$NON-NLS-1$
+ }
+ fFields.add(new TextTraceEventContent(fieldName));
}
}
* @param fieldNames
* the array of field names
*/
- private TextTraceEventContent(String fieldName) {
+ @SuppressWarnings("null") // Collections.EMPTY_LIST
+ private TextTraceEventContent(@NonNull String fieldName) {
fName = fieldName;
+ fValue = null;
+ fFields = Collections.EMPTY_LIST;
}
// ------------------------------------------------------------------------
}
@Override
- public String[] getFieldNames() {
- String[] fieldNames = new String[fFields.length];
- for (int i = 0; i < fieldNames.length; i++) {
- fieldNames[i] = fFields[i].getName();
+ public List<String> getFieldNames() {
+ List<String> fieldNames = new ArrayList<>(fFields.size());
+ for (TextTraceEventContent field : fFields) {
+ fieldNames.add(field.getName());
}
return fieldNames;
}
- @Override
+ /**
+ * Get a field name by index
+ *
+ * @param index
+ * The index of the field
+ * @return The name of the field at that index
+ */
public String getFieldName(int index) {
- if (index >= 0 && index < fFields.length) {
- return fFields[index].getName();
+ if (index >= 0 && index < fFields.size()) {
+ return fFields.get(index).getName();
}
return null;
}
@Override
- public ITmfEventField[] getFields() {
- return fFields;
+ public List<TextTraceEventContent> getFields() {
+ return new ArrayList<>(fFields);
}
@Override
public ITmfEventField getField(String name) {
- for (int i = 0; i < fFields.length; i++) {
- if (fFields[i].getName().equals(name)) {
- return fFields[i];
+ for (TextTraceEventContent field : fFields) {
+ if (field.getName().equals(name)) {
+ return field;
}
}
return null;
}
- @Override
- public ITmfEventField getField(int index) {
- if (index >= 0 && index < fFields.length) {
- return fFields[index];
- }
- return null;
- }
-
@Override
public String getFormattedValue() {
- return fValue.toString();
+ Object value = fValue;
+ if (value == null) {
+ return null;
+ }
+ return value.toString();
}
@Override
- public ITmfEventField getSubField(String[] names) {
+ public ITmfEventField getSubField(String... names) {
// There are no sub fields
if (names.length == 1) {
return getField(names[0]);
* @return field value object
*/
public Object getFieldValue(String name) {
- for (int i = 0; i < fFields.length; i++) {
- if (fFields[i].getName().equals(name)) {
- return fFields[i].getValue();
+ for (int i = 0; i < fFields.size(); i++) {
+ if (fFields.get(i).getName().equals(name)) {
+ return fFields.get(i).getValue();
}
}
return null;
* @return field value object
*/
public Object getFieldValue(int index) {
- if (index >= 0 && index < fFields.length) {
- return fFields[index].getValue();
+ if (index >= 0 && index < fFields.size()) {
+ return fFields.get(index).getValue();
}
return null;
}
* the subfield value
*/
public void setFieldValue(String name, Object value) {
- for (int i = 0; i < fFields.length; i++) {
- if (fFields[i].getName().equals(name)) {
- fFields[i].fValue = value;
+ for (int i = 0; i < fFields.size(); i++) {
+ if (fFields.get(i).getName().equals(name)) {
+ fFields.get(i).fValue = value;
}
}
}
* the subfield value
*/
public void setFieldValue(int index, Object value) {
- if (index >= 0 && index < fFields.length) {
- fFields[index].fValue = value;
+ if (index >= 0 && index < fFields.size()) {
+ fFields.get(index).fValue = value;
}
}
- // ------------------------------------------------------------------------
- // Cloneable
- // ------------------------------------------------------------------------
-
- @Override
- public TextTraceEventContent clone() {
- TextTraceEventContent clone = null;
- try {
- clone = (TextTraceEventContent) super.clone();
- clone.fName = fName;
- if (fValue instanceof StringBuffer) {
- StringBuffer value = new StringBuffer(fValue.toString());
- clone.fValue = value;
- } else {
- clone.fValue = fValue;
- }
- clone.fFields = (fFields != null) ? fFields.clone() : null;
- if (fFields != null) {
- for (int i = 0; i < fFields.length; i++) {
- clone.fFields[i] = fFields[i].clone();
- }
- }
- } catch (CloneNotSupportedException e) {
- }
- return clone;
- }
-
// ------------------------------------------------------------------------
// Object
// ------------------------------------------------------------------------
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + Arrays.hashCode(fFields);
- result = prime * result + ((fName == null) ? 0 : fName.hashCode());
+ result = prime * result + fFields.hashCode();
+ result = prime * result + fName.hashCode();
int tmpHash = 0; // initialize for fValue equals null;
- if (fValue != null) {
- if (fValue instanceof StringBuffer) {
- tmpHash = fValue.toString().hashCode();
+ Object value = fValue;
+ if (value != null) {
+ if (value instanceof StringBuffer) {
+ tmpHash = value.toString().hashCode();
} else {
- tmpHash = fValue.hashCode();
+ tmpHash = value.hashCode();
}
}
result = prime * result + tmpHash;
return false;
}
TextTraceEventContent other = (TextTraceEventContent) obj;
- if (!Arrays.equals(fFields, other.fFields)) {
+ if (!fFields.equals(other.fFields)) {
return false;
}
- if (fName == null) {
- if (other.fName != null) {
- return false;
- }
- } else if (!fName.equals(other.fName)) {
+ if (!fName.equals(other.fName)) {
return false;
}
- if (fValue == null) {
+
+ Object value = fValue;
+ if (value == null) {
if (other.fValue != null) {
return false;
}
} else {
- if ((fValue instanceof StringBuffer) && (other.fValue instanceof StringBuffer)) {
- if (!fValue.toString().equals(other.fValue.toString())) {
+ if ((value instanceof StringBuffer) && (other.fValue instanceof StringBuffer)) {
+ Object otherValue = other.getValue();
+ if (otherValue == null) {
+ return false;
+ }
+ if (!value.toString().equals(otherValue.toString())) {
return false;
}
- } else if (!fValue.equals(other.fValue)) {
+ } else if (!value.equals(other.fValue)) {
return false;
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
if (fName == ITmfEventField.ROOT_FIELD_ID) {
- for (int i = 0; i < getFields().length; i++) {
- ITmfEventField field = getFields()[i];
+ for (int i = 0; i < getFields().size(); i++) {
+ ITmfEventField field = getFields().get(i);
if (i != 0) {
sb.append(", "); //$NON-NLS-1$
}
package org.eclipse.linuxtools.tmf.ctf.core.tests;
-import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assume.assumeTrue;
+import java.util.Collection;
import java.util.Set;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
*/
@Test
public void testGetFieldNames() {
- String[] result = fixture.getContent().getFieldNames();
+ Collection<String> result = fixture.getContent().getFieldNames();
assertNotNull(result);
}
*/
@Test
public void testGetFields() {
- ITmfEventField[] fields = nullEvent.getContent().getFields();
- ITmfEventField[] fields2 = new ITmfEventField[0];
- assertArrayEquals(fields2, fields);
+ Collection<? extends ITmfEventField> fields = nullEvent.getContent().getFields();
+ assertEquals(0, fields.size());
}
/**
assertEquals(-1, nullEvent.getCPU());
assertEquals("Empty CTF event", nullEvent.getType().getName());
assertNull(nullEvent.getReference());
- assertArrayEquals(new ITmfEventField[0], nullEvent.getContent().getFields());
+ assertEquals(0, nullEvent.getContent().getFields().size());
assertEquals(-1L, nullEvent.getID());
assertEquals(-1L, nullEvent.getTimestamp().getValue());
}
@Override
public IPropertyDescriptor[] getPropertyDescriptors() {
- List<IPropertyDescriptor> descriptors= new ArrayList<>(fContent.getFields().length);
+ List<IPropertyDescriptor> descriptors = new ArrayList<>(fContent.getFields().size());
for (ITmfEventField field : fContent.getFields()) {
if (field != null) {
descriptors.add(new ReadOnlyTextPropertyDescriptor(field, field.getName()));
@Override
public Object getPropertyValue(Object id) {
ITmfEventField field = (ITmfEventField) id;
- if (field.getFields() != null && field.getFields().length > 0) {
+ if (field.getFields() != null && field.getFields().size() > 0) {
return new ContentPropertySource(field);
}
return field.getFormattedValue();
try {
ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
ITmfEventType eventType = event.getType();
- if (eventType != null && eventType.getFieldNames().length > 0) {
+ if (eventType != null && eventType.getFieldNames().size() > 0) {
fieldsList.add("[" + TmfTraceType.getCategoryName(ce.getAttribute(TmfTraceType.CATEGORY_ATTR)) + //$NON-NLS-1$
" : " + ce.getAttribute(TmfTraceType.NAME_ATTR) + "]"); //$NON-NLS-1$ //$NON-NLS-2$
for (String field : eventType.getFieldNames()) {