1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.types
;
15 import java
.util
.List
;
17 import org
.eclipse
.jdt
.annotation
.NonNull
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.AbstractArrayDefinition
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.CompoundDeclaration
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Definition
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDeclaration
;
27 import com
.google
.common
.collect
.ArrayListMultimap
;
28 import com
.google
.common
.collect
.ImmutableList
;
29 import com
.google
.common
.collect
.ImmutableList
.Builder
;
32 * A CTF array declaration
34 * Arrays are fixed-length. Their length is declared in the type declaration
35 * within the meta-data. They contain an array of "inner type" elements, which
36 * can refer to any type not containing the type of the array being declared (no
37 * circular dependency). The length is the number of elements in an array.
39 * @author Matthew Khouzam
41 public final class ArrayDeclaration
extends CompoundDeclaration
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 private final int fLength
;
48 private final IDeclaration fElemType
;
52 * Cache where we can pre-generate the children names
53 * Key: parent name
54 * Value: children names
55 * ex: field → {field[0], field[1], … field[n]}
58 * TODO: investigate performance
60 private final transient ArrayListMultimap
<String
, String
> fChildrenNames
= ArrayListMultimap
.create();
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
70 * how many elements in the array
72 * what type of element is in the array
74 public ArrayDeclaration(int length
, IDeclaration elemType
) {
79 // ------------------------------------------------------------------------
80 // Getters/Setters/Predicates
81 // ------------------------------------------------------------------------
84 public IDeclaration
getElementType() {
89 * Get the length of the array
91 * @return the length of the array
93 public int getLength() {
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 public AbstractArrayDefinition
createDefinition(@Nullable IDefinitionScope definitionScope
,
103 @NonNull String fieldName
, BitBuffer input
) throws CTFException
{
105 if (isAlignedBytes()) {
106 byte[] data
= new byte[fLength
];
107 if (input
.getByteBuffer().remaining() < fLength
) {
108 throw new CTFException("Buffer underflow"); //$NON-NLS-1$
112 return new ByteArrayDefinition(this, definitionScope
, fieldName
, data
);
114 @NonNull List
<@NonNull Definition
> definitions
= read(input
, definitionScope
, fieldName
);
115 return new ArrayDefinition(this, definitionScope
, fieldName
, definitions
);
119 public String
toString() {
120 /* Only used for debugging */
121 return "[declaration] array[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
124 private @NonNull List
<@NonNull Definition
> read(@NonNull BitBuffer input
, @Nullable IDefinitionScope definitionScope
, String fieldName
) throws CTFException
{
125 Builder
<@NonNull Definition
> definitions
= new ImmutableList
.Builder
<>();
126 if (!fChildrenNames
.containsKey(fieldName
)) {
127 for (int i
= 0; i
< fLength
; i
++) {
128 fChildrenNames
.put(fieldName
, fieldName
+ '[' + i
+ ']');
131 List
<String
> elemNames
= fChildrenNames
.get(fieldName
);
132 for (int i
= 0; i
< fLength
; i
++) {
133 String name
= elemNames
.get(i
);
135 throw new IllegalStateException();
137 definitions
.add(fElemType
.createDefinition(definitionScope
, name
, input
));
139 return definitions
.build();
143 public int getMaximumSize() {
144 long val
= (long) fLength
* fElemType
.getMaximumSize();
145 return (int) Math
.min(Integer
.MAX_VALUE
, val
);
149 public int hashCode() {
150 final int prime
= 31;
152 result
= prime
* result
+ fElemType
.hashCode();
153 result
= prime
* result
+ fLength
;
158 public boolean equals(@Nullable Object obj
) {
165 if (getClass() != obj
.getClass()) {
168 ArrayDeclaration other
= (ArrayDeclaration
) obj
;
169 if (!fElemType
.equals(other
.fElemType
)) {
172 if (fLength
!= other
.fLength
) {
179 public boolean isBinaryEquivalent(@Nullable IDeclaration obj
) {
186 if (getClass() != obj
.getClass()) {
189 ArrayDeclaration other
= (ArrayDeclaration
) obj
;
190 if (!fElemType
.isBinaryEquivalent(other
.fElemType
)) {
193 if (fLength
!= other
.fLength
) {