btf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / ArrayDeclaration.java
index f082eae7a573d29421ad04bf55c126e7d3ef18c8..fcbc3f3c76b11627cb470b8f3d577b6cc373b66e 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
+import java.util.List;
+
+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.ArrayListMultimap;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
+import com.google.common.collect.Multimap;
+
 /**
  * A CTF array declaration
  *
- * Arrays are fixed-length. Their length is declared in the type
- * declaration within the meta-data. They contain an array of "inner type"
- * elements, which can refer to any type not containing the type of the
- * array being declared (no circular dependency). The length is the number
- * of elements in an array.
+ * Arrays are fixed-length. Their length is declared in the type declaration
+ * within the meta-data. They contain an array of "inner type" elements, which
+ * can refer to any type not containing the type of the array being declared (no
+ * circular dependency). The length is the number of elements in an array.
  *
+ * @deprecated use
+ *             {@link org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration}
  * @version 1.0
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class ArrayDeclaration implements IDeclaration {
+@Deprecated
+public class ArrayDeclaration extends CompoundDeclaration {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final int length;
-    private final IDeclaration elemType;
+    private final int fLength;
+    private final IDeclaration fElemType;
+
+    /**
+     * <pre>
+     * Cache where we can pre-generate the children names
+     * Key&colon; parent name
+     * Value&colon; children names
+     * ex: field &#8594; &lbrace;field&lbrack;0&rbrack;, field&lbrack;1&rbrack;, &hellip; field&lbrack;n&rbrack;&rbrace;
+     * </pre>
+     *
+     * TODO: investigate performance
+     */
+    private final Multimap<String, String> fChildrenNames = ArrayListMultimap.<String, String> create();
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -40,24 +66,24 @@ public class ArrayDeclaration implements IDeclaration {
 
     /**
      * Constructor
-     * @param length how many elements in the array
-     * @param elemType what type of element is in the array
+     *
+     * @param length
+     *            how many elements in the array
+     * @param elemType
+     *            what type of element is in the array
      */
     public ArrayDeclaration(int length, IDeclaration elemType) {
-        this.length = length;
-        this.elemType = elemType;
+        fLength = length;
+        fElemType = elemType;
     }
 
     // ------------------------------------------------------------------------
     // Getters/Setters/Predicates
     // ------------------------------------------------------------------------
 
-    /**
-     *
-     * @return the type of element in the array
-     */
+    @Override
     public IDeclaration getElementType() {
-        return elemType;
+        return fElemType;
     }
 
     /**
@@ -65,21 +91,23 @@ public class ArrayDeclaration implements IDeclaration {
      * @return how many elements in the array
      */
     public int getLength() {
-        return length;
+        return fLength;
     }
 
-    @Override
-    public long getAlignment() {
-        return getElementType().getAlignment();
-    }
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
+    @Deprecated
     @Override
     public ArrayDefinition createDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        return new ArrayDefinition(this, definitionScope, fieldName);
+            @NonNull String fieldName, BitBuffer input) throws CTFReaderException {
+        alignRead(input);
+        List<Definition> definitions = read(input, definitionScope, fieldName);
+        return new ArrayDefinition(this, definitionScope, fieldName, definitions);
     }
 
     @Override
@@ -88,4 +116,34 @@ public class ArrayDeclaration implements IDeclaration {
         return "[declaration] array[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
     }
 
+    @NonNull
+    private List<Definition> read(@NonNull BitBuffer input, IDefinitionScope definitionScope, String fieldName) throws CTFReaderException {
+        Builder<Definition> definitions = new ImmutableList.Builder<>();
+        if (!fChildrenNames.containsKey(fieldName)) {
+            for (int i = 0; i < fLength; i++) {
+                fChildrenNames.put(fieldName, fieldName + '[' + i + ']');
+            }
+        }
+        List<String> elemNames = (List<String>) fChildrenNames.get(fieldName);
+        for (int i = 0; i < fLength; i++) {
+            String name = elemNames.get(i);
+            if (name == null) {
+                throw new IllegalStateException();
+            }
+            definitions.add(fElemType.createDefinition(definitionScope, name, input));
+        }
+        @SuppressWarnings("null")
+        @NonNull ImmutableList<Definition> ret = definitions.build();
+        return ret;
+    }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        long val = (long) fLength * fElemType.getMaximumSize();
+        return (int) Math.min(Integer.MAX_VALUE, val);
+    }
+
 }
This page took 0.025687 seconds and 5 git commands to generate.