tmf: Make TmfEventField immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfEventField.java
index d458ea78eaa4a36da17eaac65c69b43b06d49d5f..665f6990f0289921f44c1e1f098bd45c41bbe171 100644 (file)
@@ -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<String, ITmfEventField> fNameMapping;
+    private final String[] fFieldNames;
+    private final Map<String, ITmfEventField> 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<String, ITmfEventField>();
+
+        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<String, ITmfEventField>();
-        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
     // ------------------------------------------------------------------------
This page took 0.045573 seconds and 5 git commands to generate.