Remove all existing @since annotations
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / types / ArrayDeclaration.java
CommitLineData
7b4f13e6
MK
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
f357bcd4 13package org.eclipse.tracecompass.internal.ctf.core.event.types;
7b4f13e6 14
5db5a3a4
AM
15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
7b4f13e6
MK
17import java.util.List;
18
19import org.eclipse.jdt.annotation.NonNull;
f78eb6a7 20import org.eclipse.jdt.annotation.Nullable;
453a59f0 21import org.eclipse.tracecompass.ctf.core.CTFReaderException;
f357bcd4
AM
22import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
23import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
24import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
25import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
26import org.eclipse.tracecompass.ctf.core.event.types.Definition;
27import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
7b4f13e6
MK
28
29import com.google.common.collect.ArrayListMultimap;
30import com.google.common.collect.ImmutableList;
31import 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
7b4f13e6
MK
42 */
43public 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 */
e00e6663 62 private final transient ArrayListMultimap<String, String> fChildrenNames = ArrayListMultimap.create();
7b4f13e6
MK
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
7b4f13e6
MK
99 // ------------------------------------------------------------------------
100 // Operations
101 // ------------------------------------------------------------------------
102
103 @Override
f78eb6a7 104 public AbstractArrayDefinition createDefinition(@Nullable IDefinitionScope definitionScope,
7b4f13e6
MK
105 @NonNull String fieldName, BitBuffer input) throws CTFReaderException {
106 alignRead(input);
107 if (isString()) {
108 byte[] data = new byte[fLength];
109 input.get(data);
110 return new ByteArrayDefinition(this, definitionScope, fieldName, data);
111 }
f78eb6a7 112 @NonNull List<Definition> definitions = read(input, definitionScope, fieldName);
7b4f13e6
MK
113 return new ArrayDefinition(this, definitionScope, fieldName, definitions);
114 }
115
116 @Override
117 public String toString() {
118 /* Only used for debugging */
119 return "[declaration] array[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
120 }
121
e00e6663 122 private @NonNull List<Definition> read(@NonNull BitBuffer input, @Nullable IDefinitionScope definitionScope, String fieldName) throws CTFReaderException {
7b4f13e6
MK
123 Builder<Definition> definitions = new ImmutableList.Builder<>();
124 if (!fChildrenNames.containsKey(fieldName)) {
125 for (int i = 0; i < fLength; i++) {
126 fChildrenNames.put(fieldName, fieldName + '[' + i + ']');
127 }
128 }
129 List<String> elemNames = fChildrenNames.get(fieldName);
130 for (int i = 0; i < fLength; i++) {
131 String name = elemNames.get(i);
132 if (name == null) {
133 throw new IllegalStateException();
134 }
135 definitions.add(fElemType.createDefinition(definitionScope, name, input));
136 }
5db5a3a4 137 return checkNotNull(definitions.build());
7b4f13e6
MK
138 }
139
140 @Override
141 public int getMaximumSize() {
142 long val = (long) fLength * fElemType.getMaximumSize();
143 return (int) Math.min(Integer.MAX_VALUE, val);
144 }
145
e00e6663
MK
146 @Override
147 public int hashCode() {
148 final int prime = 31;
149 int result = 1;
150 result = prime * result + fElemType.hashCode();
151 result = prime * result + fLength;
152 return result;
153 }
154
155 @Override
156 public boolean equals(@Nullable Object obj) {
157 if (this == obj) {
158 return true;
159 }
160 if (obj == null) {
161 return false;
162 }
163 if (getClass() != obj.getClass()) {
164 return false;
165 }
166 ArrayDeclaration other = (ArrayDeclaration) obj;
167 if (!fElemType.equals(other.fElemType)) {
168 return false;
169 }
170 if (fLength != other.fLength) {
171 return false;
172 }
173 return true;
174 }
175
66aa25f0
MK
176 @Override
177 public boolean isBinaryEquivalent(@Nullable IDeclaration obj) {
178 if (this == obj) {
179 return true;
180 }
181 if (obj == null) {
182 return false;
183 }
184 if (getClass() != obj.getClass()) {
185 return false;
186 }
187 ArrayDeclaration other = (ArrayDeclaration) obj;
188 if (!fElemType.isBinaryEquivalent(other.fElemType)) {
189 return false;
190 }
191 if (fLength != other.fLength) {
192 return false;
193 }
194 return true;
195 }
196
7b4f13e6 197}
This page took 0.050176 seconds and 5 git commands to generate.