From 80349bf74d882f75684356753948dd1aa81b025b Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Thu, 17 Jan 2013 15:55:12 -0500 Subject: [PATCH] tmf: Make TmfEventField immutable This required some minor adjustment in CustomEventContent. Those can still extend TmfEventField, but it requires creating a new Content object if you want to change its values. Change-Id: I1fe951fe60d505f4ad9564dea559af522a86ac9f Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/9806 Tested-by: Hudson CI Reviewed-by: Patrick Tasse IP-Clean: Patrick Tasse --- .../core/tests/event/TmfEventFieldTest.java | 72 --------------- .../tmf/core/ctfadaptor/CtfTmfEvent.java | 12 +-- .../tmf/core/event/ITmfEventField.java | 5 -- .../linuxtools/tmf/core/event/TmfEvent.java | 2 +- .../tmf/core/event/TmfEventField.java | 90 +++++-------------- .../tmf/core/event/TmfEventType.java | 14 +-- .../tmf/ui/parsers/custom/CustomEvent.java | 4 +- .../ui/parsers/custom/CustomEventContent.java | 25 +++--- 8 files changed, 50 insertions(+), 174 deletions(-) diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfEventFieldTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfEventFieldTest.java index 7429c9d5d0..cf8f85a961 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfEventFieldTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfEventFieldTest.java @@ -212,78 +212,6 @@ public class TmfEventFieldTest extends TestCase { } } - // ------------------------------------------------------------------------ - // Modifiers - // ------------------------------------------------------------------------ - - private static class MyField extends TmfEventField { - - public MyField(final String id, final Object value) { - super(id, value); - } - - public MyField(final TmfEventField field) { - super(field); - } - - @Override - public void setValue(final Object value, final ITmfEventField[] subfields) { - super.setValue(value, subfields); - } - } - - /** - * - */ - public void testSetValue() { - final TmfEventField field = new TmfEventField(fFieldName1, fValue1, null); - - final MyField myField = new MyField(field); - assertSame("getValue", fValue1, myField.getValue()); - myField.setValue(fValue2, null); - assertSame("getValue", fValue2, myField.getValue()); - myField.setValue(fValue2, new TmfEventField[] { field }); - assertSame("getValue", fValue2, myField.getValue()); - } - - // ------------------------------------------------------------------------ - // clone - // ------------------------------------------------------------------------ - - /** - * - */ - public void testFieldClone() { - TmfEventField clone = fField1.clone(); - assertTrue("clone", fField1.clone().equals(fField1)); - assertTrue("clone", clone.clone().equals(clone)); - assertEquals("clone", fField1, clone); - assertEquals("clone", clone, fField1); - - clone = fRootField.clone(); - assertTrue("clone", fRootField.clone().equals(fRootField)); - assertTrue("clone", clone.clone().equals(clone)); - assertEquals("clone", fRootField, clone); - assertEquals("clone", clone, fRootField); - } - - /** - * - */ - public void testStructFieldClone() { - TmfEventField clone = fStructTerminalField1.clone(); - assertTrue("clone", fStructTerminalField1.clone().equals(fStructTerminalField1)); - assertTrue("clone", clone.clone().equals(clone)); - assertEquals("clone", fStructTerminalField1, clone); - assertEquals("clone", clone, fStructTerminalField1); - - clone = fStructRootField.clone(); - assertTrue("clone", fStructRootField.clone().equals(fStructRootField)); - assertTrue("clone", clone.clone().equals(clone)); - assertEquals("clone", fStructRootField, clone); - assertEquals("clone", clone, fStructRootField); - } - // ------------------------------------------------------------------------ // hashCode // ------------------------------------------------------------------------ diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java index a42de95f29..3635ced6a5 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java @@ -186,12 +186,8 @@ public final class CtfTmfEvent implements ITmfEvent, IAdaptable, Cloneable { /* There is only one reference to the trace, so we can shallow-copy it */ this.fTrace = other.getTrace(); - /* - * Copy the timestamp - * FIXME This can be switched to a shallow-copy once timestamps are - * made immutable. - */ - this.fTimestamp = new CtfTmfTimestamp(other.fTimestamp.getValue()); + /* Copy the timestamp (immutable) */ + this.fTimestamp = other.fTimestamp; /* Primitives, those will be copied by value */ this.sourceCPU = other.sourceCPU; @@ -201,8 +197,8 @@ public final class CtfTmfEvent implements ITmfEvent, IAdaptable, Cloneable { this.eventName = other.eventName; this.fileName = other.fileName; - /* Copy the fields over */ - this.fContent = other.fContent.clone(); + /* Copy the fields over (immutable) */ + this.fContent = other.fContent; /* * Copy the reference to the custom attributes (should be the same diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/ITmfEventField.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/ITmfEventField.java index 7782b672c7..89189e51ce 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/ITmfEventField.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/ITmfEventField.java @@ -75,9 +75,4 @@ public interface ITmfEventField { */ public ITmfEventField getField(int index); - /** - * @return a clone of the event field - */ - public ITmfEventField clone(); - } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEvent.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEvent.java index 04c0b92485..61e2c1c52a 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEvent.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEvent.java @@ -247,7 +247,7 @@ public class TmfEvent implements ITmfEvent, IAdaptable, Cloneable { clone.fTimestamp = fTimestamp; clone.fSource = fSource; clone.fType = fType != null ? fType.clone() : null; - clone.fContent = fContent != null ? fContent.clone() : null; + clone.fContent = fContent; clone.fReference = fReference; } catch (final CloneNotSupportedException e) { } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventField.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventField.java index d458ea78ea..665f6990f0 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventField.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventField.java @@ -9,11 +9,11 @@ * Contributors: * Francois Chouinard - Initial API and implementation * Francois Chouinard - Updated as per TMF Event Model 1.0 + * Alexandre Montplaisir - Removed Cloneable, made immutable *******************************************************************************/ package org.eclipse.linuxtools.tmf.core.event; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -29,30 +29,23 @@ import java.util.Map; * @see ITmfEvent * @see ITmfEventType */ -public class TmfEventField implements ITmfEventField, Cloneable { +public class TmfEventField implements ITmfEventField { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private String fName; - private Object fValue; - private ITmfEventField[] fFields; + private final String fName; + private final Object fValue; + private final ITmfEventField[] fFields; - private String[] fFieldNames; - private Map fNameMapping; + private final String[] fFieldNames; + private final Map fNameMapping; // ------------------------------------------------------------------------ // Constructors // ------------------------------------------------------------------------ - /** - * Default constructor - */ - @SuppressWarnings("unused") - private TmfEventField() { - } - /** * Constructor for a structural field * @@ -86,8 +79,18 @@ public class TmfEventField implements ITmfEventField, Cloneable { } fName = name; fValue = value; - fFields = (fields != null) ? Arrays.copyOf(fields, fields.length) : null; - populateStructs(); + 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]); + } } /** @@ -103,7 +106,7 @@ public class TmfEventField implements ITmfEventField, Cloneable { fValue = field.fValue; fFields = field.fFields; fFieldNames = field.fFieldNames; - populateStructs(); + fNameMapping = field.fNameMapping; } // ------------------------------------------------------------------------ @@ -131,7 +134,7 @@ public class TmfEventField implements ITmfEventField, Cloneable { */ @Override public String[] getFieldNames() { - return Arrays.copyOf(fFieldNames, fFieldNames.length); + return fFieldNames; } /* (non-Javadoc) @@ -151,7 +154,7 @@ public class TmfEventField implements ITmfEventField, Cloneable { */ @Override public ITmfEventField[] getFields() { - return (fFields != null) ? Arrays.copyOf(fFields, fFields.length) : new ITmfEventField[0]; + return (fFields != null) ? fFields : new ITmfEventField[0]; } /* (non-Javadoc) @@ -173,20 +176,6 @@ public class TmfEventField implements ITmfEventField, Cloneable { return null; } - // ------------------------------------------------------------------------ - // Convenience setters - // ------------------------------------------------------------------------ - - /** - * @param value new field raw value - * @param fields the corresponding fields - */ - protected void setValue(final Object value, final ITmfEventField[] fields) { - fValue = value; - fFields = (fields != null) ? Arrays.copyOf(fields, fields.length) : null; - populateStructs(); - } - // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ @@ -206,41 +195,6 @@ public class TmfEventField implements ITmfEventField, Cloneable { return new TmfEventField(ITmfEventField.ROOT_FIELD_ID, fields); } - /* - * Populate the subfield names and the name map - */ - private void populateStructs() { - final int nbFields = (fFields != null) ? fFields.length : 0; - fFieldNames = new String[nbFields]; - fNameMapping = new HashMap(); - for (int i = 0; i < nbFields; i++) { - final String name = fFields[i].getName(); - fFieldNames[i] = name; - fNameMapping.put(name, fFields[i]); - } - } - - // ------------------------------------------------------------------------ - // Cloneable - // ------------------------------------------------------------------------ - - /* (non-Javadoc) - * @see java.lang.Object#clone() - */ - @Override - public TmfEventField clone() { - TmfEventField clone = null; - try { - clone = (TmfEventField) super.clone(); - clone.fName = fName; - clone.fValue = fValue; - clone.fFields = (fFields != null) ? fFields.clone() : null; - clone.populateStructs(); - } catch (final CloneNotSupportedException e) { - } - return clone; - } - // ------------------------------------------------------------------------ // Object // ------------------------------------------------------------------------ diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventType.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventType.java index a0cc90cd9f..6e4e14ffd1 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventType.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfEventType.java @@ -1,11 +1,11 @@ /******************************************************************************* * Copyright (c) 2009, 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: * Francois Chouinard - Initial API and implementation * Francois Chouinard - Updated as per TMF Event Model 1.0 @@ -15,10 +15,10 @@ package org.eclipse.linuxtools.tmf.core.event; /** * A basic implementation of ITmfEventType. - * + * * @version 1.0 * @author Francois Chouinard - * + * * @see ITmfEvent * @see ITmfEventField */ @@ -45,7 +45,7 @@ public class TmfEventType implements ITmfEventType, Cloneable { /** * Full constructor - * + * * @param context the type context * @param typeId the type name * @param root the root field @@ -64,7 +64,7 @@ public class TmfEventType implements ITmfEventType, Cloneable { /** * Copy constructor - * + * * @param type the other type */ public TmfEventType(final ITmfEventType type) { @@ -134,7 +134,7 @@ public class TmfEventType implements ITmfEventType, Cloneable { clone = (TmfEventType) super.clone(); clone.fContext = fContext; clone.fTypeId = fTypeId; - clone.fRootField = (fRootField != null) ? fRootField.clone() : null; + clone.fRootField = fRootField; } catch (final CloneNotSupportedException e) { } diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/parsers/custom/CustomEvent.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/parsers/custom/CustomEvent.java index edbbc0d5b3..fed9735f1b 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/parsers/custom/CustomEvent.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/parsers/custom/CustomEvent.java @@ -145,8 +145,8 @@ public class CustomEvent extends TmfEvent { fColumnData[i++] = new TmfEventField(outputColumn.name, (value != null ? value : "")); //$NON-NLS-1$ } } - CustomEventContent content = (CustomEventContent) getContent(); - content.setFields(fColumnData); + CustomEventContent curContent = (CustomEventContent) getContent(); + setContent(new CustomEventContent(curContent.getName(), curContent.getValue(), fColumnData)); fData = null; } diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/parsers/custom/CustomEventContent.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/parsers/custom/CustomEventContent.java index 7da672d555..454b5b5b2c 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/parsers/custom/CustomEventContent.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/parsers/custom/CustomEventContent.java @@ -34,6 +34,20 @@ public class CustomEventContent extends TmfEventField { super(ITmfEventField.ROOT_FIELD_ID, content); } + /** + * Create a new event field with sub-fields. + * + * @param name + * Field name + * @param content + * Event content + * @param fields + * The array of sub-fields + */ + public CustomEventContent(String name, Object content, ITmfEventField[] fields) { + super(name, content, fields); + } + @Override public int hashCode() { return super.hashCode(); @@ -52,15 +66,4 @@ public class CustomEventContent extends TmfEventField { } return true; } - - /** - * Modify the fields to the given value. - * - * @param fields - * The array of fields to use as event content - */ - public void setFields(ITmfEventField[] fields) { - super.setValue(getValue(), fields); - } - } -- 2.34.1