Fix some null warnings
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / types / SequenceDeclaration.java
CommitLineData
7b4f13e6
MK
1/*******************************************************************************
2 * Copyright (c) 2014 Ericsson, Ecole Polytechnique de Montreal and others
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: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
f357bcd4 12package org.eclipse.tracecompass.internal.ctf.core.event.types;
7b4f13e6 13
5db5a3a4
AM
14import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
15
7b4f13e6
MK
16import java.util.Collection;
17import java.util.List;
18
aa353506 19import org.eclipse.jdt.annotation.NonNull;
f78eb6a7 20import org.eclipse.jdt.annotation.Nullable;
680f9173 21import org.eclipse.tracecompass.ctf.core.CTFException;
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;
28import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
29import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
7b4f13e6
MK
30
31import com.google.common.collect.ArrayListMultimap;
32import com.google.common.collect.ImmutableList;
33import com.google.common.collect.ImmutableList.Builder;
34import com.google.common.collect.Multimap;
35
36/**
37 * A CTF sequence declaration.
38 *
39 * An array where the size is fixed but declared in the trace, unlike array
40 * where it is declared with a literal
41 *
42 * @author Matthew Khouzam
7b4f13e6
MK
43 */
44public class SequenceDeclaration extends CompoundDeclaration {
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49
50 private final IDeclaration fElemType;
51 private final String fLengthName;
f78eb6a7 52 private final transient Multimap<String, String> fPaths = ArrayListMultimap.create();
7b4f13e6
MK
53
54 // ------------------------------------------------------------------------
55 // Constructors
56 // ------------------------------------------------------------------------
57
58 /**
59 * Constructor
60 *
61 * @param lengthName
62 * the name of the field describing the length
63 * @param elemType
64 * The element type
65 */
f78eb6a7 66 public SequenceDeclaration(@Nullable String lengthName, IDeclaration elemType) {
7b4f13e6
MK
67 fElemType = elemType;
68 fLengthName = lengthName;
69 }
70
71 // ------------------------------------------------------------------------
72 // Getters/Setters/Predicates
73 // ------------------------------------------------------------------------
74
75 @Override
76 public IDeclaration getElementType() {
77 return fElemType;
78 }
79
80 /**
81 * Gets the name of the length field
82 *
83 * @return the name of the length field
84 */
85 public String getLengthName() {
86 return fLengthName;
87 }
88
7b4f13e6
MK
89 // ------------------------------------------------------------------------
90 // Operations
91 // ------------------------------------------------------------------------
92
7b4f13e6
MK
93 @Override
94 public AbstractArrayDefinition createDefinition(
680f9173 95 @Nullable IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFException {
cc98c947 96 IDefinition lenDef = null;
7b4f13e6
MK
97
98 if (definitionScope != null) {
99 lenDef = definitionScope.lookupDefinition(getLengthName());
100 }
101
102 if (lenDef == null) {
680f9173 103 throw new CTFException("Sequence length field not found"); //$NON-NLS-1$
7b4f13e6
MK
104 }
105
106 if (!(lenDef instanceof IntegerDefinition)) {
680f9173 107 throw new CTFException("Sequence length field not integer"); //$NON-NLS-1$
7b4f13e6
MK
108 }
109
110 IntegerDefinition lengthDefinition = (IntegerDefinition) lenDef;
111
112 if (lengthDefinition.getDeclaration().isSigned()) {
680f9173 113 throw new CTFException("Sequence length must not be signed"); //$NON-NLS-1$
7b4f13e6
MK
114 }
115
116 long length = lengthDefinition.getValue();
117 if ((length > Integer.MAX_VALUE) || (!input.canRead((int) length * fElemType.getMaximumSize()))) {
680f9173 118 throw new CTFException("Sequence length too long " + length); //$NON-NLS-1$
7b4f13e6
MK
119 }
120
15f6223a 121 if (isAlignedBytes()) {
7b4f13e6
MK
122 // Don't create "useless" definitions
123 byte[] data = new byte[(int) length];
124 input.get(data);
125 return new ByteArrayDefinition(this, definitionScope, fieldName, data);
126 }
127 Collection<String> collection = fPaths.get(fieldName);
128 while (collection.size() < length) {
129 fPaths.put(fieldName, fieldName + '[' + collection.size() + ']');
130 }
131 List<String> paths = (List<String>) fPaths.get(fieldName);
aa353506 132 Builder<@NonNull Definition> definitions = new ImmutableList.Builder<>();
7b4f13e6 133 for (int i = 0; i < length; i++) {
5db5a3a4
AM
134 /* We should not have inserted any null values */
135 String elemName = checkNotNull(paths.get(i));
7b4f13e6
MK
136 definitions.add(fElemType.createDefinition(definitionScope, elemName, input));
137 }
aa353506 138 List<@NonNull Definition> list = checkNotNull(definitions.build());
5db5a3a4 139 return new ArrayDefinition(this, definitionScope, fieldName, list);
7b4f13e6
MK
140 }
141
142 @Override
143 public String toString() {
144 /* Only used for debugging */
145 return "[declaration] sequence[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
146 }
147
148 @Override
149 public int getMaximumSize() {
150 return Integer.MAX_VALUE;
151 }
152
e00e6663
MK
153 @Override
154 public int hashCode() {
155 final int prime = 31;
156 int result = 1;
157 result = prime * result + fElemType.hashCode();
158 result = prime * result + fLengthName.hashCode();
159 return result;
160 }
161
162 @Override
163 public boolean equals(@Nullable Object obj) {
164 if (this == obj) {
165 return true;
166 }
167 if (obj == null) {
168 return false;
169 }
170 if (getClass() != obj.getClass()) {
171 return false;
172 }
173 SequenceDeclaration other = (SequenceDeclaration) obj;
174 if (!fElemType.equals(other.fElemType)) {
175 return false;
176 }
177 if (!fLengthName.equals(other.fLengthName)) {
178 return false;
179 }
180 return true;
181 }
182
66aa25f0
MK
183 @Override
184 public boolean isBinaryEquivalent(@Nullable IDeclaration obj) {
185 if (this == obj) {
186 return true;
187 }
188 if (obj == null) {
189 return false;
190 }
191 if (getClass() != obj.getClass()) {
192 return false;
193 }
194 SequenceDeclaration other = (SequenceDeclaration) obj;
195 if (!fElemType.isBinaryEquivalent(other.fElemType)) {
196 return false;
197 }
198 if (!fLengthName.equals(other.fLengthName)) {
199 return false;
200 }
201 return true;
202 }
203
7b4f13e6 204}
This page took 0.061503 seconds and 5 git commands to generate.