ctf: remove some null warnings in TmfTraceManagerTest
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / event / types / IntegerDeclaration.java
CommitLineData
866e5b51 1/*******************************************************************************
0f231de2 2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
866e5b51
FC
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 *
4311ac8b
MAL
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 * Simon Marchi - Initial API and implementation
12 * Marc-Andre Laperle - Add min/maximum for validation
866e5b51
FC
13 *******************************************************************************/
14
f357bcd4 15package org.eclipse.tracecompass.ctf.core.event.types;
866e5b51 16
5db5a3a4
AM
17import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
18
4311ac8b 19import java.math.BigInteger;
866e5b51
FC
20import java.nio.ByteOrder;
21
a4fa4e36
MK
22import org.eclipse.jdt.annotation.NonNullByDefault;
23import org.eclipse.jdt.annotation.Nullable;
680f9173 24import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4
AM
25import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
26import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
a4fa4e36 27
866e5b51 28/**
d37aaa7f 29 * A CTF integer declaration.
4311ac8b 30 *
d37aaa7f
FC
31 * The declaration of a integer basic data type.
32 *
33 * @version 1.0
34 * @author Matthew Khouzam
35 * @author Simon Marchi
866e5b51 36 */
a4fa4e36 37@NonNullByDefault
f068c622 38public final class IntegerDeclaration extends Declaration implements ISimpleDatatypeDeclaration {
a4fa4e36
MK
39
40 // ------------------------------------------------------------------------
41 // Helpers
42 // ------------------------------------------------------------------------
43
f068c622
MK
44 private static final int SIZE_64 = 64;
45 private static final int SIZE_32 = 32;
46 private static final int SIZE_27 = 27;
47 private static final int SIZE_16 = 16;
48 private static final int SIZE_8 = 8;
49 private static final int SIZE_5 = 5;
50 private static final int BYTE_ALIGN = 8;
51 private static final int BASE_10 = 10;
a4fa4e36
MK
52 /**
53 * unsigned int 32 bits big endian
a4fa4e36
MK
54 */
55 public static final IntegerDeclaration UINT_32B_DECL = new IntegerDeclaration(32, false, ByteOrder.BIG_ENDIAN);
56 /**
57 * unsigned int 32 bits little endian
a4fa4e36
MK
58 */
59 public static final IntegerDeclaration UINT_32L_DECL = new IntegerDeclaration(32, false, ByteOrder.LITTLE_ENDIAN);
60 /**
61 * signed int 32 bits big endian
a4fa4e36
MK
62 */
63 public static final IntegerDeclaration INT_32B_DECL = new IntegerDeclaration(32, true, ByteOrder.BIG_ENDIAN);
64 /**
65 * signed int 32 bits little endian
a4fa4e36
MK
66 */
67 public static final IntegerDeclaration INT_32L_DECL = new IntegerDeclaration(32, true, ByteOrder.LITTLE_ENDIAN);
68 /**
69 * unsigned int 32 bits big endian
a4fa4e36
MK
70 */
71 public static final IntegerDeclaration UINT_64B_DECL = new IntegerDeclaration(64, false, ByteOrder.BIG_ENDIAN);
72 /**
73 * unsigned int 64 bits little endian
a4fa4e36
MK
74 */
75 public static final IntegerDeclaration UINT_64L_DECL = new IntegerDeclaration(64, false, ByteOrder.LITTLE_ENDIAN);
76 /**
77 * signed int 64 bits big endian
a4fa4e36
MK
78 */
79 public static final IntegerDeclaration INT_64B_DECL = new IntegerDeclaration(64, true, ByteOrder.BIG_ENDIAN);
80 /**
81 * signed int 64 bits little endian
a4fa4e36
MK
82 */
83 public static final IntegerDeclaration INT_64L_DECL = new IntegerDeclaration(64, true, ByteOrder.LITTLE_ENDIAN);
84 /**
85 * unsigned 8 bit int endianness doesn't matter since it's 8 bits (byte)
a4fa4e36
MK
86 */
87 public static final IntegerDeclaration UINT_8_DECL = new IntegerDeclaration(8, false, ByteOrder.BIG_ENDIAN);
88 /**
89 * signed 8 bit int endianness doesn't matter since it's 8 bits (char)
a4fa4e36
MK
90 */
91 public static final IntegerDeclaration INT_8_DECL = new IntegerDeclaration(8, true, ByteOrder.BIG_ENDIAN);
6c7592e1
MK
92 /**
93 * Unsigned 5 bit int, used for event headers
6c7592e1 94 */
2dd7bd51 95 public static final IntegerDeclaration UINT_5B_DECL = new IntegerDeclaration(5, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 1); //$NON-NLS-1$
6c7592e1
MK
96 /**
97 * Unsigned 5 bit int, used for event headers
6c7592e1 98 */
2dd7bd51 99 public static final IntegerDeclaration UINT_5L_DECL = new IntegerDeclaration(5, false, 10, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, "", 1); //$NON-NLS-1$
6c7592e1
MK
100 /**
101 * Unsigned 5 bit int, used for event headers
6c7592e1 102 */
2dd7bd51 103 public static final IntegerDeclaration UINT_27B_DECL = new IntegerDeclaration(27, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 1); //$NON-NLS-1$
6c7592e1
MK
104 /**
105 * Unsigned 5 bit int, used for event headers
6c7592e1 106 */
2dd7bd51 107 public static final IntegerDeclaration UINT_27L_DECL = new IntegerDeclaration(27, false, 10, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, "", 1); //$NON-NLS-1$
6c7592e1
MK
108 /**
109 * Unsigned 16 bit int, used for event headers
6c7592e1
MK
110 */
111 public static final IntegerDeclaration UINT_16B_DECL = new IntegerDeclaration(16, false, ByteOrder.BIG_ENDIAN);
112 /**
113 * Unsigned 16 bit int, used for event headers
6c7592e1
MK
114 */
115 public static final IntegerDeclaration UINT_16L_DECL = new IntegerDeclaration(16, false, ByteOrder.LITTLE_ENDIAN);
866e5b51
FC
116 // ------------------------------------------------------------------------
117 // Attributes
118 // ------------------------------------------------------------------------
119
a4fa4e36
MK
120 private final int fLength;
121 private final boolean fSigned;
122 private final int fBase;
123 private final ByteOrder fByteOrder;
124 private final Encoding fEncoding;
125 private final long fAlignment;
126 private final String fClock;
866e5b51
FC
127
128 // ------------------------------------------------------------------------
129 // Constructors
130 // ------------------------------------------------------------------------
131
a4fa4e36
MK
132 /**
133 * Factory, some common types cached
134 *
135 * @param len
136 * The length in bits
137 * @param signed
138 * Is the integer signed? false == unsigned
139 * @param base
140 * The base (10-16 are most common)
141 * @param byteOrder
142 * Big-endian little-endian or other
143 * @param encoding
144 * ascii, utf8 or none.
145 * @param clock
146 * The clock path, can be null
147 * @param alignment
148 * The minimum alignment. Should be >= 1
149 * @return the integer declaration
a4fa4e36
MK
150 */
151 public static IntegerDeclaration createDeclaration(int len, boolean signed, int base,
152 @Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment) {
f068c622
MK
153 if (encoding.equals(Encoding.NONE) && (clock.equals("")) && base == BASE_10) { //$NON-NLS-1$
154 if (alignment == BYTE_ALIGN) {
2dd7bd51 155 switch (len) {
f068c622 156 case SIZE_8:
2dd7bd51 157 return signed ? INT_8_DECL : UINT_8_DECL;
f068c622 158 case SIZE_16:
2dd7bd51 159 if (!signed) {
f068c622 160 if (isBigEndian(byteOrder)) {
2dd7bd51
MK
161 return UINT_16B_DECL;
162 }
163 return UINT_16L_DECL;
6c7592e1 164 }
2dd7bd51 165 break;
f068c622 166 case SIZE_32:
2dd7bd51 167 if (signed) {
f068c622 168 if (isBigEndian(byteOrder)) {
2dd7bd51
MK
169 return INT_32B_DECL;
170 }
171 return INT_32L_DECL;
6c7592e1 172 }
f068c622 173 if (isBigEndian(byteOrder)) {
2dd7bd51 174 return UINT_32B_DECL;
6c7592e1 175 }
2dd7bd51 176 return UINT_32L_DECL;
f068c622 177 case SIZE_64:
2dd7bd51 178 if (signed) {
f068c622 179 if (isBigEndian(byteOrder)) {
2dd7bd51
MK
180 return INT_64B_DECL;
181 }
182 return INT_64L_DECL;
a4fa4e36 183 }
f068c622 184 if (isBigEndian(byteOrder)) {
2dd7bd51 185 return UINT_64B_DECL;
a4fa4e36 186 }
2dd7bd51 187 return UINT_64L_DECL;
f068c622 188
2dd7bd51 189 default:
f068c622 190
a4fa4e36 191 }
f068c622 192
2dd7bd51
MK
193 } else if (alignment == 1) {
194 switch (len) {
f068c622 195 case SIZE_5:
2dd7bd51 196 if (!signed) {
f068c622 197 if (isBigEndian(byteOrder)) {
2dd7bd51
MK
198 return UINT_5B_DECL;
199 }
200 return UINT_5L_DECL;
201 }
202 break;
f068c622 203 case SIZE_27:
2dd7bd51 204 if (!signed) {
f068c622 205 if (isBigEndian(byteOrder)) {
2dd7bd51
MK
206 return UINT_27B_DECL;
207 }
208 return UINT_27L_DECL;
209 }
210 break;
211 default:
212 break;
a4fa4e36 213 }
a4fa4e36
MK
214 }
215 }
216 return new IntegerDeclaration(len, signed, base, byteOrder, encoding, clock, alignment);
217 }
218
f068c622
MK
219 private static boolean isBigEndian(@Nullable ByteOrder byteOrder) {
220 return (byteOrder != null) && byteOrder.equals(ByteOrder.BIG_ENDIAN);
221 }
222
9ac2eb62 223 /**
a511da0d 224 * Constructor
056ebaf1
AM
225 *
226 * @param len
227 * The length in bits
228 * @param signed
229 * Is the integer signed? false == unsigned
230 * @param base
231 * The base (10-16 are most common)
232 * @param byteOrder
a511da0d 233 * Big-endian little-endian or other
056ebaf1
AM
234 * @param encoding
235 * ascii, utf8 or none.
236 * @param clock
237 * The clock path, can be null
238 * @param alignment
ab04fc6b 239 * The minimum alignment. Should be ≥ 1
9ac2eb62 240 */
a4fa4e36
MK
241 private IntegerDeclaration(int len, boolean signed, int base,
242 @Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment) {
a4fa4e36
MK
243 fLength = len;
244 fSigned = signed;
245 fBase = base;
246
5db5a3a4 247 fByteOrder = (byteOrder == null ? checkNotNull(ByteOrder.nativeOrder()) : byteOrder);
a4fa4e36
MK
248
249 fEncoding = encoding;
250 fClock = clock;
251 fAlignment = Math.max(alignment, 1);
252 }
253
254 private IntegerDeclaration(int len, boolean signed, @Nullable ByteOrder byteOrder) {
f068c622 255 this(len, signed, BASE_10, byteOrder, Encoding.NONE, "", BYTE_ALIGN); //$NON-NLS-1$
866e5b51
FC
256 }
257
258 // ------------------------------------------------------------------------
a511da0d 259 // Getters/Setters/Predicates
866e5b51
FC
260 // ------------------------------------------------------------------------
261
9ac2eb62
MK
262 /**
263 * Is the integer signed?
bdc1590e 264 *
9ac2eb62
MK
265 * @return the is the integer signed
266 */
866e5b51 267 public boolean isSigned() {
a4fa4e36 268 return fSigned;
866e5b51
FC
269 }
270
9ac2eb62 271 /**
a511da0d 272 * Get the integer base commonly decimal or hex
bdc1590e 273 *
9ac2eb62
MK
274 * @return the integer base
275 */
866e5b51 276 public int getBase() {
a4fa4e36 277 return fBase;
866e5b51
FC
278 }
279
9ac2eb62 280 /**
bdc1590e
EB
281 * Get the byte order
282 *
9ac2eb62
MK
283 * @return the byte order
284 */
866e5b51 285 public ByteOrder getByteOrder() {
a4fa4e36 286 return fByteOrder;
866e5b51
FC
287 }
288
9ac2eb62 289 /**
a511da0d 290 * Get encoding, chars are 8 bit ints
bdc1590e 291 *
9ac2eb62
MK
292 * @return the encoding
293 */
866e5b51 294 public Encoding getEncoding() {
a4fa4e36 295 return fEncoding;
866e5b51
FC
296 }
297
9ac2eb62 298 /**
a511da0d 299 * Is the integer a character (8 bits and encoded?)
bdc1590e 300 *
9ac2eb62
MK
301 * @return is the integer a char
302 */
bdc1590e 303 public boolean isCharacter() {
f068c622 304 return (fLength == SIZE_8) && (fEncoding != Encoding.NONE);
866e5b51
FC
305 }
306
7b4f13e6
MK
307 /**
308 * Is the integer an unsigned byte (8 bits and no sign)?
309 *
310 * @return is the integer an unsigned byte
7b4f13e6
MK
311 */
312 public boolean isUnsignedByte() {
f068c622 313 return (fLength == SIZE_8) && (!fSigned);
7b4f13e6
MK
314 }
315
bdc1590e
EB
316 /**
317 * Get the length in bits for this integer
318 *
319 * @return the length of the integer
320 */
866e5b51 321 public int getLength() {
a4fa4e36 322 return fLength;
866e5b51
FC
323 }
324
07002e0a 325 @Override
6f04e06c 326 public long getAlignment() {
a4fa4e36 327 return fAlignment;
fd74e6c1
MK
328 }
329
9ac2eb62
MK
330 /**
331 * The integer's clock, since timestamps are stored in ints
bdc1590e 332 *
9ac2eb62
MK
333 * @return the integer's clock, can be null. (most often it is)
334 */
6f04e06c 335 public String getClock() {
a4fa4e36
MK
336 return fClock;
337 }
338
a4fa4e36
MK
339 @Override
340 public int getMaximumSize() {
341 return fLength;
fd74e6c1 342 }
bdc1590e 343
866e5b51
FC
344 // ------------------------------------------------------------------------
345 // Operations
346 // ------------------------------------------------------------------------
347
348 @Override
a4fa4e36 349 public IntegerDefinition createDefinition(@Nullable IDefinitionScope definitionScope,
680f9173 350 String fieldName, BitBuffer input) throws CTFException {
733c614c
MK
351 ByteOrder byteOrder = input.getByteOrder();
352 input.setByteOrder(fByteOrder);
a4fa4e36 353 long value = read(input);
733c614c 354 input.setByteOrder(byteOrder);
a4fa4e36 355 return new IntegerDefinition(this, definitionScope, fieldName, value);
866e5b51
FC
356 }
357
358 @Override
359 public String toString() {
2dd7bd51 360 return "[declaration] integer[length:" + fLength + (fSigned ? " " : " un") + "signed" + " base:" + fBase + " byteOrder:" + fByteOrder + " encoding:" + fEncoding + " alignment:" + fAlignment + " clock:" + fClock + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$
866e5b51
FC
361 }
362
4311ac8b 363 /**
a4fa4e36 364 * Get the maximum value for this integer declaration.
4311ac8b
MAL
365 *
366 * @return The maximum value for this integer declaration
4311ac8b
MAL
367 */
368 public BigInteger getMaxValue() {
bdc1590e
EB
369 /*
370 * Compute the number of bits able to represent an unsigned number,
371 * ignoring sign bit.
372 */
a4fa4e36 373 int significantBits = fLength - (fSigned ? 1 : 0);
bdc1590e 374 /*
a4fa4e36
MK
375 * For a given N significant bits, compute the maximal value which is (1
376 * << N) - 1.
bdc1590e 377 */
5db5a3a4 378 return checkNotNull(BigInteger.ONE.shiftLeft(significantBits).subtract(BigInteger.ONE));
4311ac8b
MAL
379 }
380
381 /**
a4fa4e36 382 * Get the minimum value for this integer declaration.
4311ac8b
MAL
383 *
384 * @return The minimum value for this integer declaration
4311ac8b
MAL
385 */
386 public BigInteger getMinValue() {
a4fa4e36 387 if (!fSigned) {
5db5a3a4 388 return checkNotNull(BigInteger.ZERO);
4311ac8b
MAL
389 }
390
bdc1590e
EB
391 /*
392 * Compute the number of bits able to represent an unsigned number,
393 * without the sign bit.
394 */
a4fa4e36
MK
395 int significantBits = fLength - 1;
396 /*
397 * For a given N significant bits, compute the minimal value which is -
398 * (1 << N).
399 */
5db5a3a4 400 return checkNotNull(BigInteger.ONE.shiftLeft(significantBits).negate());
a4fa4e36
MK
401 }
402
680f9173 403 private long read(BitBuffer input) throws CTFException {
a4fa4e36
MK
404 /* Offset the buffer position wrt the current alignment */
405 alignRead(input);
406
407 boolean signed = isSigned();
408 int length = getLength();
409 long bits = 0;
410
411 /*
412 * Is the endianness of this field the same as the endianness of the
413 * input buffer? If not, then temporarily set the buffer's endianness to
414 * this field's just to read the data
415 */
416 ByteOrder previousByteOrder = input.getByteOrder();
417 if ((getByteOrder() != input.getByteOrder())) {
418 input.setByteOrder(getByteOrder());
419 }
420
f068c622 421 if (length > SIZE_64) {
680f9173 422 throw new CTFException("Cannot read an integer with over 64 bits. Length given: " + length); //$NON-NLS-1$
a4fa4e36
MK
423 }
424
425 bits = input.get(length, signed);
426
bdc1590e 427 /*
a4fa4e36 428 * Put the input buffer's endianness back to original if it was changed
bdc1590e 429 */
a4fa4e36
MK
430 if (previousByteOrder != input.getByteOrder()) {
431 input.setByteOrder(previousByteOrder);
432 }
433
434 return bits;
4311ac8b
MAL
435 }
436
e00e6663
MK
437 @Override
438 public int hashCode() {
439 final int prime = 31;
440 int result = 1;
441 result = prime * result + (int) (fAlignment ^ (fAlignment >>> 32));
442 result = prime * result + fBase;
f068c622
MK
443 result = prime * result + fByteOrder.toString().hashCode();
444 result = prime * result + fClock.hashCode();
445 result = prime * result + fEncoding.hashCode();
e00e6663
MK
446 result = prime * result + fLength;
447 result = prime * result + (fSigned ? 1231 : 1237);
448 return result;
449 }
450
451 @Override
452 public boolean equals(@Nullable Object obj) {
453 if (this == obj) {
454 return true;
455 }
456 if (obj == null) {
457 return false;
458 }
459 if (getClass() != obj.getClass()) {
460 return false;
461 }
462 IntegerDeclaration other = (IntegerDeclaration) obj;
f068c622 463 if (!isBinaryEquivalent(other)) {
e00e6663
MK
464 return false;
465 }
466 if (!fByteOrder.equals(other.fByteOrder)) {
467 return false;
468 }
469 if (!fClock.equals(other.fClock)) {
470 return false;
471 }
472 if (fEncoding != other.fEncoding) {
473 return false;
474 }
f068c622 475 if (fBase != other.fBase) {
e00e6663
MK
476 return false;
477 }
478 return true;
479 }
480
66aa25f0
MK
481 @Override
482 public boolean isBinaryEquivalent(@Nullable IDeclaration obj) {
483 if (this == obj) {
484 return true;
485 }
486 if (obj == null) {
487 return false;
488 }
489 if (getClass() != obj.getClass()) {
490 return false;
491 }
492 IntegerDeclaration other = (IntegerDeclaration) obj;
f068c622
MK
493 return isBinaryEquivalent(other);
494 }
495
496 private boolean isBinaryEquivalent(IntegerDeclaration other) {
66aa25f0
MK
497 if (fAlignment != other.fAlignment) {
498 return false;
499 }
500 if (fLength != other.fLength) {
501 return false;
502 }
503 if (fSigned != other.fSigned) {
504 return false;
505 }
506 // no need for base
507 // no need for encoding
508 // no need for clock
509 // byte inversion is ok on byte order if the element is one byte long
f068c622 510 if ((fLength != BYTE_ALIGN) && !fByteOrder.equals(other.fByteOrder)) {
66aa25f0
MK
511 return false;
512 }
513 return true;
514 }
515
866e5b51 516}
This page took 0.091762 seconds and 5 git commands to generate.