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