1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.ctf
.core
.event
.types
;
15 import java
.math
.BigInteger
;
17 import org
.eclipse
.jdt
.annotation
.NonNull
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
21 * A CTF integer definition.
23 * The definition of a integer basic data type. It will take the data from a
24 * trace and store it (and make it fit) as a long.
27 * @author Matthew Khouzam
28 * @author Simon Marchi
30 public final class IntegerDefinition
extends SimpleDatatypeDefinition
{
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
36 private static final int INT_BASE_10
= 10;
37 private static final int INT_BASE_16
= 16;
38 private static final int INT_BASE_8
= 8;
39 private static final int INT_BASE_2
= 2;
40 private final long fValue
;
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
50 * the parent declaration
51 * @param definitionScope
58 public IntegerDefinition(@NonNull IntegerDeclaration declaration
,
59 IDefinitionScope definitionScope
, @NonNull String fieldName
, long value
) {
60 super(declaration
, definitionScope
, fieldName
);
64 // ------------------------------------------------------------------------
65 // Getters/Setters/Predicates
66 // ------------------------------------------------------------------------
69 * Gets the value of the integer
71 * @return the value of the integer (in long)
73 public long getValue() {
78 public IntegerDeclaration
getDeclaration() {
79 return (IntegerDeclaration
) super.getDeclaration();
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
87 public Long
getIntegerValue() {
92 public String
getStringValue() {
93 return this.toString();
97 public String
toString() {
98 if (getDeclaration().isCharacter()) {
99 char c
= (char) fValue
;
100 return Character
.toString(c
);
102 return formatNumber(fValue
, getDeclaration().getBase(), getDeclaration().isSigned());
106 * Print a numeric value as a string in a given base
109 * The value to print as string
111 * The base for this value
113 * Is the value signed or not
114 * @return formatted number string
116 public static String
formatNumber(long value
, int base
, boolean signed
) {
118 /* Format the number correctly according to the integer's base */
121 s
= "0b" + Long
.toBinaryString(value
); //$NON-NLS-1$
124 s
= "0" + Long
.toOctalString(value
); //$NON-NLS-1$
127 s
= "0x" + Long
.toHexString(value
); //$NON-NLS-1$
131 /* For non-standard base, we'll just print it as a decimal number */
132 if (!signed
&& value
< 0) {
134 * Since there are no 'unsigned long', handle this case with
137 BigInteger bigInteger
= BigInteger
.valueOf(value
);
139 * we add 2^64 to the negative number to get the real unsigned
142 bigInteger
= bigInteger
.add(BigInteger
.valueOf(1).shiftLeft(64));
143 s
= bigInteger
.toString();
145 s
= Long
.toString(value
);