3983f34804e8842971bcb49054c77185fb3ef1b4
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / types / ArrayDeclaration.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.ctf.core.event.types;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.tracecompass.ctf.core.CTFException;
22 import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
23 import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
24 import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
25 import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
26 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
27 import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
28
29 import com.google.common.collect.ArrayListMultimap;
30 import com.google.common.collect.ImmutableList;
31 import com.google.common.collect.ImmutableList.Builder;
32
33 /**
34 * A CTF array declaration
35 *
36 * Arrays are fixed-length. Their length is declared in the type declaration
37 * within the meta-data. They contain an array of "inner type" elements, which
38 * can refer to any type not containing the type of the array being declared (no
39 * circular dependency). The length is the number of elements in an array.
40 *
41 * @author Matthew Khouzam
42 */
43 public final class ArrayDeclaration extends CompoundDeclaration {
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48
49 private final int fLength;
50 private final IDeclaration fElemType;
51
52 /**
53 * <pre>
54 * Cache where we can pre-generate the children names
55 * Key&colon; parent name
56 * Value&colon; children names
57 * ex: field &#8594; &lbrace;field&lbrack;0&rbrack;, field&lbrack;1&rbrack;, &hellip; field&lbrack;n&rbrack;&rbrace;
58 * </pre>
59 *
60 * TODO: investigate performance
61 */
62 private final transient ArrayListMultimap<String, String> fChildrenNames = ArrayListMultimap.create();
63
64 // ------------------------------------------------------------------------
65 // Constructors
66 // ------------------------------------------------------------------------
67
68 /**
69 * Constructor
70 *
71 * @param length
72 * how many elements in the array
73 * @param elemType
74 * what type of element is in the array
75 */
76 public ArrayDeclaration(int length, IDeclaration elemType) {
77 fLength = length;
78 fElemType = elemType;
79 }
80
81 // ------------------------------------------------------------------------
82 // Getters/Setters/Predicates
83 // ------------------------------------------------------------------------
84
85 @Override
86 public IDeclaration getElementType() {
87 return fElemType;
88 }
89
90 /**
91 * Get the length of the array
92 *
93 * @return the length of the array
94 */
95 public int getLength() {
96 return fLength;
97 }
98
99 // ------------------------------------------------------------------------
100 // Operations
101 // ------------------------------------------------------------------------
102
103 @Override
104 public AbstractArrayDefinition createDefinition(@Nullable IDefinitionScope definitionScope,
105 @NonNull String fieldName, BitBuffer input) throws CTFException {
106 alignRead(input);
107 if (isAlignedBytes()) {
108 byte[] data = new byte[fLength];
109 if (input.getByteBuffer().remaining() < fLength) {
110 throw new CTFException("Buffer underflow"); //$NON-NLS-1$
111 }
112 input.get(data);
113
114 return new ByteArrayDefinition(this, definitionScope, fieldName, data);
115 }
116 @NonNull List<Definition> definitions = read(input, definitionScope, fieldName);
117 return new ArrayDefinition(this, definitionScope, fieldName, definitions);
118 }
119
120 @Override
121 public String toString() {
122 /* Only used for debugging */
123 return "[declaration] array[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
124 }
125
126 private @NonNull List<@NonNull Definition> read(@NonNull BitBuffer input, @Nullable IDefinitionScope definitionScope, String fieldName) throws CTFException {
127 Builder<@NonNull Definition> definitions = new ImmutableList.Builder<>();
128 if (!fChildrenNames.containsKey(fieldName)) {
129 for (int i = 0; i < fLength; i++) {
130 fChildrenNames.put(fieldName, fieldName + '[' + i + ']');
131 }
132 }
133 List<String> elemNames = fChildrenNames.get(fieldName);
134 for (int i = 0; i < fLength; i++) {
135 String name = elemNames.get(i);
136 if (name == null) {
137 throw new IllegalStateException();
138 }
139 definitions.add(fElemType.createDefinition(definitionScope, name, input));
140 }
141 return checkNotNull(definitions.build());
142 }
143
144 @Override
145 public int getMaximumSize() {
146 long val = (long) fLength * fElemType.getMaximumSize();
147 return (int) Math.min(Integer.MAX_VALUE, val);
148 }
149
150 @Override
151 public int hashCode() {
152 final int prime = 31;
153 int result = 1;
154 result = prime * result + fElemType.hashCode();
155 result = prime * result + fLength;
156 return result;
157 }
158
159 @Override
160 public boolean equals(@Nullable Object obj) {
161 if (this == obj) {
162 return true;
163 }
164 if (obj == null) {
165 return false;
166 }
167 if (getClass() != obj.getClass()) {
168 return false;
169 }
170 ArrayDeclaration other = (ArrayDeclaration) obj;
171 if (!fElemType.equals(other.fElemType)) {
172 return false;
173 }
174 if (fLength != other.fLength) {
175 return false;
176 }
177 return true;
178 }
179
180 @Override
181 public boolean isBinaryEquivalent(@Nullable IDeclaration obj) {
182 if (this == obj) {
183 return true;
184 }
185 if (obj == null) {
186 return false;
187 }
188 if (getClass() != obj.getClass()) {
189 return false;
190 }
191 ArrayDeclaration other = (ArrayDeclaration) obj;
192 if (!fElemType.isBinaryEquivalent(other.fElemType)) {
193 return false;
194 }
195 if (fLength != other.fLength) {
196 return false;
197 }
198 return true;
199 }
200
201 }
This page took 0.035185 seconds and 5 git commands to generate.