ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDeclaration.java
index ec235cbdc8d70e6dbc4dfae1933e22a2b6c5eacd..03a682936a6959984034b63d5f3ba8377caf0b6a 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import java.util.HashMap;
-import java.util.LinkedList;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
+import com.google.common.collect.ImmutableList;
+
 /**
  * A CTF structure declaration.
  *
@@ -28,15 +34,23 @@ import java.util.Map;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class StructDeclaration implements IDeclaration {
+public class StructDeclaration extends Declaration {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final Map<String, IDeclaration> fields = new HashMap<>();
-    private final List<String> fieldsList = new LinkedList<>();
-    private long maxAlign;
+    /** linked list of field names. So fieldName->fieldValue */
+    private final Map<String, IDeclaration> fFieldMap = new LinkedHashMap<>();
+
+    /** List of strings for acceleration */
+    @NonNull
+    private ImmutableList<String> fFieldNames;
+    /** array declaration for acceleration */
+    private List<IDeclaration> fFieldDeclarations;
+
+    /** maximum bit alignment */
+    private long fMaxAlign;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -50,8 +64,30 @@ public class StructDeclaration implements IDeclaration {
      *            aligned and has a 32 bit aligned field, the struct becomes 32
      *            bit aligned.
      */
+    @SuppressWarnings("null")
+    // ImmutableList.of()
     public StructDeclaration(long align) {
-        this.maxAlign = Math.max(align, 1);
+        fMaxAlign = Math.max(align, 1);
+        fFieldNames = ImmutableList.of();
+    }
+
+    /**
+     * Struct declaration constructor
+     *
+     * @param names
+     *            the names of all the fields
+     * @param declarations
+     *            all the fields
+     * @since 3.0
+     */
+    @SuppressWarnings("null")
+    // ImmutableList.of()
+    public StructDeclaration(String[] names, Declaration[] declarations) {
+        fMaxAlign = 1;
+        fFieldNames = ImmutableList.of();
+        for (int i = 0; i < names.length; i++) {
+            addField(names[i], declarations[i]);
+        }
     }
 
     // ------------------------------------------------------------------------
@@ -60,62 +96,100 @@ public class StructDeclaration implements IDeclaration {
 
     /**
      * Get current alignment
+     *
      * @return the alignment of the struct and all its fields
      */
     public long getMaxAlign() {
-        return maxAlign;
+        return fMaxAlign;
     }
 
     /**
      * Query if the struct has a given field
-     * @param name the name of the field, scopeless please
+     *
+     * @param name
+     *            the name of the field, scopeless please
      * @return does the field exist?
      */
     public boolean hasField(String name) {
-        return this.fields.containsKey(name);
+        return fFieldMap.containsKey(name);
     }
 
     /**
      * get the fields of the struct in a map. Faster access time than a list.
+     *
      * @return a HashMap of the fields (key is the name)
      * @since 2.0
      */
     public Map<String, IDeclaration> getFields() {
-        return this.fields;
+        return fFieldMap;
     }
 
     /**
-     * Gets the field list. Very important since the map of fields does not retain the order of the fields.
+     * Gets the field list. Very important since the map of fields does not
+     * retain the order of the fields.
+     *
      * @return the field list.
+     * @since 3.0
      */
-    public List<String> getFieldsList() {
-        return this.fieldsList;
+    public Iterable<String> getFieldsList() {
+        return fFieldMap.keySet();
     }
 
     @Override
     public long getAlignment() {
-        return this.maxAlign;
+        return this.fMaxAlign;
+    }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        int maxSize = 0;
+        if (fFieldDeclarations != null) {
+            for (IDeclaration field : fFieldDeclarations) {
+                maxSize += field.getMaximumSize();
+            }
+        }
+        return Math.min(maxSize, Integer.MAX_VALUE);
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
+    @SuppressWarnings("null")
+    // immutablelist
     @Override
     public StructDefinition createDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        return new StructDefinition(this, definitionScope, fieldName);
+            String fieldName, BitBuffer input) throws CTFReaderException {
+        alignRead(input);
+        final Definition[] myFields = new Definition[fFieldNames.size()];
+        StructDefinition structDefinition = new StructDefinition(this, definitionScope, fieldName, fFieldNames, myFields);
+        for (int i = 0; i < fFieldNames.size(); i++) {
+            myFields[i] = fFieldDeclarations.get(i).createDefinition(structDefinition, fFieldNames.get(i), input);
+        }
+        return structDefinition;
     }
 
     /**
      * Add a field to the struct
-     * @param name the name of the field, scopeless
-     * @param declaration the declaration of the field
+     *
+     * @param name
+     *            the name of the field, scopeless
+     * @param declaration
+     *            the declaration of the field
      */
+    @SuppressWarnings("null")
+    // Immutable list copyof cannot return null
     public void addField(String name, IDeclaration declaration) {
-        this.fields.put(name, declaration);
-        this.fieldsList.add(name);
-        maxAlign = Math.max(maxAlign, declaration.getAlignment());
+        fFieldMap.put(name, declaration);
+        fMaxAlign = Math.max(fMaxAlign, declaration.getAlignment());
+        fFieldNames = ImmutableList.copyOf(fFieldMap.keySet());
+        fFieldDeclarations = ImmutableList.<IDeclaration>copyOf(fFieldMap.values());
     }
 
     @Override
@@ -128,8 +202,8 @@ public class StructDeclaration implements IDeclaration {
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = (prime * result) + fieldsList.hashCode();
-        result = (prime * result) + (int) (maxAlign ^ (maxAlign >>> 32));
+        result = (prime * result) + fFieldMap.entrySet().hashCode();
+        result = (prime * result) + (int) (fMaxAlign ^ (fMaxAlign >>> 32));
         return result;
     }
 
@@ -145,10 +219,10 @@ public class StructDeclaration implements IDeclaration {
             return false;
         }
         StructDeclaration other = (StructDeclaration) obj;
-        if (!fieldsList.equals(other.fieldsList)) {
+        if (!fFieldMap.entrySet().equals(other.fFieldMap.entrySet())) {
             return false;
         }
-        if (maxAlign != other.maxAlign) {
+        if (fMaxAlign != other.fMaxAlign) {
             return false;
         }
         return true;
This page took 0.027177 seconds and 5 git commands to generate.