1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
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
10 * Matthew Khouzam - Initial API and implementation
11 * Simon Marchi - Initial API and implementation
12 * Marc-Andre Laperle - Add min/maximum for validation
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
17 import java
.math
.BigInteger
;
18 import java
.nio
.ByteOrder
;
21 * A CTF integer declaration.
23 * The declaration of a integer basic data type.
26 * @author Matthew Khouzam
27 * @author Simon Marchi
29 public class IntegerDeclaration
implements IDeclaration
{
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
35 private final int length
;
36 private final boolean signed
;
37 private final int base
;
38 private final ByteOrder byteOrder
;
39 private final Encoding encoding
;
40 private final long alignment
;
41 private final String clock
;
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
53 * Is the integer signed? false == unsigned
55 * The base (10-16 are most common)
57 * Big-endian little-endian or other
59 * ascii, utf8 or none.
61 * The clock path, can be null
63 * The minimum alignment. Should be ≥ 1
65 public IntegerDeclaration(int len
, boolean signed
, int base
,
66 ByteOrder byteOrder
, Encoding encoding
, String clock
, long alignment
) {
67 if (len
<= 0 || len
== 1 && signed
) {
68 throw new IllegalArgumentException();
73 this.byteOrder
= byteOrder
;
74 this.encoding
= encoding
;
76 this.alignment
= Math
.max(alignment
, 1);
79 // ------------------------------------------------------------------------
80 // Getters/Setters/Predicates
81 // ------------------------------------------------------------------------
84 * Is the integer signed?
86 * @return the is the integer signed
88 public boolean isSigned() {
93 * Get the integer base commonly decimal or hex
95 * @return the integer base
97 public int getBase() {
104 * @return the byte order
106 public ByteOrder
getByteOrder() {
111 * Get encoding, chars are 8 bit ints
113 * @return the encoding
115 public Encoding
getEncoding() {
120 * Is the integer a character (8 bits and encoded?)
122 * @return is the integer a char
124 public boolean isCharacter() {
125 return (length
== 8) && (encoding
!= Encoding
.NONE
);
129 * Get the length in bits for this integer
131 * @return the length of the integer
133 public int getLength() {
138 public long getAlignment() {
143 * The integer's clock, since timestamps are stored in ints
145 * @return the integer's clock, can be null. (most often it is)
147 public String
getClock() {
151 // ------------------------------------------------------------------------
153 // ------------------------------------------------------------------------
156 public IntegerDefinition
createDefinition(IDefinitionScope definitionScope
,
158 return new IntegerDefinition(this, definitionScope
, fieldName
);
162 public String
toString() {
163 /* Only used for debugging */
164 return "[declaration] integer[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
168 * Get the maximum value for this integer declaration
170 * @return The maximum value for this integer declaration
173 public BigInteger
getMaxValue() {
175 * Compute the number of bits able to represent an unsigned number,
178 int significant_bits
= length
- (signed ?
1 : 0);
180 * For a given N significant bits, compute the maximal value which is
183 return BigInteger
.ONE
.shiftLeft(significant_bits
).subtract(BigInteger
.ONE
);
187 * Get the minimum value for this integer declaration
189 * @return The minimum value for this integer declaration
192 public BigInteger
getMinValue() {
194 return BigInteger
.ZERO
;
198 * Compute the number of bits able to represent an unsigned number,
199 * without the sign bit.
201 int significant_bits
= length
- 1;
203 * For a given N significant bits, compute the minimal value which is
206 return BigInteger
.ONE
.shiftLeft(significant_bits
).negate();