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