1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
15 import java
.util
.List
;
17 import org
.eclipse
.jdt
.annotation
.NonNull
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
22 import com
.google
.common
.collect
.ArrayListMultimap
;
23 import com
.google
.common
.collect
.ImmutableList
;
24 import com
.google
.common
.collect
.ImmutableList
.Builder
;
25 import com
.google
.common
.collect
.Multimap
;
28 * A CTF array declaration
30 * Arrays are fixed-length. Their length is declared in the type declaration
31 * within the meta-data. They contain an array of "inner type" elements, which
32 * can refer to any type not containing the type of the array being declared (no
33 * circular dependency). The length is the number of elements in an array.
36 * {@link org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration}
38 * @author Matthew Khouzam
39 * @author Simon Marchi
42 public class ArrayDeclaration
extends CompoundDeclaration
{
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
48 private final int fLength
;
49 private final IDeclaration fElemType
;
53 * Cache where we can pre-generate the children names
54 * Key: parent name
55 * Value: children names
56 * ex: field → {field[0], field[1], … field[n]}
59 * TODO: investigate performance
61 private final Multimap
<String
, String
> fChildrenNames
= ArrayListMultimap
.<String
, String
> create();
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
71 * how many elements in the array
73 * what type of element is in the array
75 public ArrayDeclaration(int length
, IDeclaration elemType
) {
80 // ------------------------------------------------------------------------
81 // Getters/Setters/Predicates
82 // ------------------------------------------------------------------------
85 public IDeclaration
getElementType() {
91 * @return how many elements in the array
93 public int getLength() {
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
106 public ArrayDefinition
createDefinition(IDefinitionScope definitionScope
,
107 @NonNull String fieldName
, BitBuffer input
) throws CTFReaderException
{
109 List
<Definition
> definitions
= read(input
, definitionScope
, fieldName
);
110 return new ArrayDefinition(this, definitionScope
, fieldName
, definitions
);
114 public String
toString() {
115 /* Only used for debugging */
116 return "[declaration] array[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
120 private List
<Definition
> read(@NonNull BitBuffer input
, IDefinitionScope definitionScope
, String fieldName
) throws CTFReaderException
{
121 Builder
<Definition
> definitions
= new ImmutableList
.Builder
<>();
122 if (!fChildrenNames
.containsKey(fieldName
)) {
123 for (int i
= 0; i
< fLength
; i
++) {
124 fChildrenNames
.put(fieldName
, fieldName
+ '[' + i
+ ']');
127 List
<String
> elemNames
= (List
<String
>) fChildrenNames
.get(fieldName
);
128 for (int i
= 0; i
< fLength
; i
++) {
129 String name
= elemNames
.get(i
);
131 throw new IllegalStateException();
133 definitions
.add(fElemType
.createDefinition(definitionScope
, name
, input
));
135 @SuppressWarnings("null")
136 @NonNull ImmutableList
<Definition
> ret
= definitions
.build();
144 public int getMaximumSize() {
145 long val
= (long) fLength
* fElemType
.getMaximumSize();
146 return (int) Math
.min(Integer
.MAX_VALUE
, val
);