gdbtrace: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / IntegerDefinition.java
CommitLineData
866e5b51 1/*******************************************************************************
11252342 2 * Copyright (c) 2011, 2013 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 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.ctf.core.event.types;
14
3b11ddc7 15import java.math.BigInteger;
72dbc4ac 16
a4fa4e36
MK
17import org.eclipse.jdt.annotation.NonNull;
18import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
866e5b51
FC
19
20/**
d37aaa7f 21 * A CTF integer definition.
dc209be3 22 *
3b11ddc7
JCK
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.
d37aaa7f 25 *
d37aaa7f
FC
26 * @version 1.0
27 * @author Matthew Khouzam
28 * @author Simon Marchi
866e5b51 29 */
a4fa4e36 30public final class IntegerDefinition extends SimpleDatatypeDefinition {
866e5b51
FC
31
32 // ------------------------------------------------------------------------
33 // Attributes
34 // ------------------------------------------------------------------------
35
a4fa4e36 36 private final long fValue;
866e5b51
FC
37
38 // ------------------------------------------------------------------------
a511da0d 39 // Constructors
866e5b51
FC
40 // ------------------------------------------------------------------------
41
9ac2eb62
MK
42 /**
43 * Constructor
3b11ddc7
JCK
44 *
45 * @param declaration
46 * the parent declaration
47 * @param definitionScope
48 * the parent scope
49 * @param fieldName
50 * the field name
a4fa4e36
MK
51 * @param value
52 * integer value
53 * @since 3.0
9ac2eb62 54 */
a4fa4e36
MK
55 public IntegerDefinition(@NonNull IntegerDeclaration declaration,
56 IDefinitionScope definitionScope, @NonNull String fieldName, long value) {
57 super(declaration, definitionScope, fieldName);
58 fValue = value;
866e5b51
FC
59 }
60
61 // ------------------------------------------------------------------------
a511da0d 62 // Getters/Setters/Predicates
866e5b51
FC
63 // ------------------------------------------------------------------------
64
9ac2eb62
MK
65 /**
66 * Gets the value of the integer
3b11ddc7 67 *
9ac2eb62
MK
68 * @return the value of the integer (in long)
69 */
866e5b51 70 public long getValue() {
a4fa4e36 71 return fValue;
866e5b51
FC
72 }
73
9ac2eb62 74 @Override
866e5b51 75 public IntegerDeclaration getDeclaration() {
a4fa4e36 76 return (IntegerDeclaration) super.getDeclaration();
866e5b51
FC
77 }
78
79 // ------------------------------------------------------------------------
80 // Operations
81 // ------------------------------------------------------------------------
82
21fb02fa
MK
83 @Override
84 public Long getIntegerValue() {
85 return getValue();
86 }
87
21fb02fa
MK
88 @Override
89 public String getStringValue() {
90 return this.toString();
91 }
92
866e5b51
FC
93 @Override
94 public String toString() {
a4fa4e36
MK
95 if (getDeclaration().isCharacter()) {
96 char c = (char) fValue;
866e5b51
FC
97 return Character.toString(c);
98 }
a4fa4e36 99 return formatNumber(fValue, getDeclaration().getBase(), getDeclaration().isSigned());
3b11ddc7
JCK
100 }
101
102 /**
103 * Print a numeric value as a string in a given base
104 *
105 * @param value
106 * The value to print as string
107 * @param base
108 * The base for this value
109 * @param signed
110 * Is the value signed or not
111 * @return formatted number string
112 * @since 3.0
113 */
114 public static final String formatNumber(long value, int base, boolean signed) {
115 String s;
116 /* Format the number correctly according to the integer's base */
117 switch (base) {
118 case 2:
119 s = "0b" + Long.toBinaryString(value); //$NON-NLS-1$
120 break;
121 case 8:
122 s = "0" + Long.toOctalString(value); //$NON-NLS-1$
123 break;
124 case 16:
125 s = "0x" + Long.toHexString(value); //$NON-NLS-1$
126 break;
127 case 10:
128 default:
129 /* For non-standard base, we'll just print it as a decimal number */
130 if (!signed && value < 0) {
131 /*
132 * Since there are no 'unsigned long', handle this case with
133 * BigInteger
134 */
135 BigInteger bigInteger = BigInteger.valueOf(value);
136 /*
137 * we add 2^64 to the negative number to get the real unsigned
138 * value
139 */
140 bigInteger = bigInteger.add(BigInteger.valueOf(1).shiftLeft(64));
141 s = bigInteger.toString();
142 } else {
143 s = Long.toString(value);
144 }
145 break;
146 }
147 return s;
866e5b51
FC
148 }
149}
This page took 0.058586 seconds and 5 git commands to generate.