Fix some null warnings
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / types / SequenceDeclaration.java
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
12 package org.eclipse.tracecompass.internal.ctf.core.event.types;
13
14 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
15
16 import java.util.Collection;
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 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
29 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
30
31 import com.google.common.collect.ArrayListMultimap;
32 import com.google.common.collect.ImmutableList;
33 import com.google.common.collect.ImmutableList.Builder;
34 import 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
43 */
44 public class SequenceDeclaration extends CompoundDeclaration {
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49
50 private final IDeclaration fElemType;
51 private final String fLengthName;
52 private final transient Multimap<String, String> fPaths = ArrayListMultimap.create();
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 */
66 public SequenceDeclaration(@Nullable String lengthName, IDeclaration elemType) {
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
89 // ------------------------------------------------------------------------
90 // Operations
91 // ------------------------------------------------------------------------
92
93 @Override
94 public AbstractArrayDefinition createDefinition(
95 @Nullable IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFException {
96 IDefinition lenDef = null;
97
98 if (definitionScope != null) {
99 lenDef = definitionScope.lookupDefinition(getLengthName());
100 }
101
102 if (lenDef == null) {
103 throw new CTFException("Sequence length field not found"); //$NON-NLS-1$
104 }
105
106 if (!(lenDef instanceof IntegerDefinition)) {
107 throw new CTFException("Sequence length field not integer"); //$NON-NLS-1$
108 }
109
110 IntegerDefinition lengthDefinition = (IntegerDefinition) lenDef;
111
112 if (lengthDefinition.getDeclaration().isSigned()) {
113 throw new CTFException("Sequence length must not be signed"); //$NON-NLS-1$
114 }
115
116 long length = lengthDefinition.getValue();
117 if ((length > Integer.MAX_VALUE) || (!input.canRead((int) length * fElemType.getMaximumSize()))) {
118 throw new CTFException("Sequence length too long " + length); //$NON-NLS-1$
119 }
120
121 if (isAlignedBytes()) {
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);
132 Builder<@NonNull Definition> definitions = new ImmutableList.Builder<>();
133 for (int i = 0; i < length; i++) {
134 /* We should not have inserted any null values */
135 String elemName = checkNotNull(paths.get(i));
136 definitions.add(fElemType.createDefinition(definitionScope, elemName, input));
137 }
138 List<@NonNull Definition> list = checkNotNull(definitions.build());
139 return new ArrayDefinition(this, definitionScope, fieldName, list);
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
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
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
204 }
This page took 0.03758 seconds and 5 git commands to generate.