Fix NLS-related Javadoc warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / IntegerDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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.linuxtools.ctf.core.event.types;
14
15 import java.nio.ByteOrder;
16
17 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
18
19 /**
20 * A CTF integer definition.
21 *
22 * The definition of a integer basic data type. It will take the data
23 * from a trace and store it (and make it fit) as a long.
24 *
25 * @version 1.0
26 * @author Matthew Khouzam
27 * @author Simon Marchi
28 */
29 public class IntegerDefinition extends SimpleDatatypeDefinition {
30
31 // ------------------------------------------------------------------------
32 // Attributes
33 // ------------------------------------------------------------------------
34
35 private final IntegerDeclaration declaration;
36 private long value;
37
38 // ------------------------------------------------------------------------
39 // Contructors
40 // ------------------------------------------------------------------------
41
42 /**
43 * Constructor
44 * @param declaration the parent declaration
45 * @param definitionScope the parent scope
46 * @param fieldName the field name
47 */
48 public IntegerDefinition(IntegerDeclaration declaration,
49 IDefinitionScope definitionScope, String fieldName) {
50 super(definitionScope, fieldName);
51 this.declaration = declaration;
52 }
53
54 // ------------------------------------------------------------------------
55 // Gettters/Setters/Predicates
56 // ------------------------------------------------------------------------
57
58 /**
59 * Gets the value of the integer
60 * @return the value of the integer (in long)
61 */
62 public long getValue() {
63 return value;
64 }
65
66 /**
67 * Sets the value of an integer
68 * @param val the value
69 */
70 public void setValue(long val) {
71 value = val;
72 }
73
74 @Override
75 public IntegerDeclaration getDeclaration() {
76 return declaration;
77 }
78
79
80
81 // ------------------------------------------------------------------------
82 // Operations
83 // ------------------------------------------------------------------------
84
85 /* (non-Javadoc)
86 * @see org.eclipse.linuxtools.ctf.core.event.types.SimpleDatatypeDefinition#getLongValue()
87 */
88 @Override
89 public Long getIntegerValue() {
90 return getValue();
91 }
92
93 /* (non-Javadoc)
94 * @see org.eclipse.linuxtools.ctf.core.event.types.SimpleDatatypeDefinition#getStringValue()
95 */
96 @Override
97 public String getStringValue() {
98 return this.toString();
99 }
100
101 @Override
102 public void read(BitBuffer input) {
103 final long longNegBit = 0x0000000080000000L;
104 int align = (int) declaration.getAlignment();
105 int pos = input.position() + ((align - (input.position() % align)) % align);
106 input.position(pos);
107 boolean signed = declaration.isSigned();
108 int length = declaration.getLength();
109 long bits = 0;
110
111 /*
112 * Is the endianness of this field the same as the endianness of the
113 * input buffer? If not, then temporarily set the buffer's endianness to
114 * this field's just to read the data
115 */
116 ByteOrder byteOrder = input.getByteOrder();
117 if ((this.declaration.getByteOrder() != null) &&
118 (this.declaration.getByteOrder() != input.getByteOrder())) {
119 input.setByteOrder(this.declaration.getByteOrder());
120 }
121
122 // TODO: use the eventual getLong from BitBuffer
123 if (length == 64) {
124 long low = input.getInt(32, false);
125 low = low & 0x00000000FFFFFFFFL;
126 long high = input.getInt(32, false);
127 high = high & 0x00000000FFFFFFFFL;
128 if (this.declaration.getByteOrder() != ByteOrder.BIG_ENDIAN) {
129 bits = (high << 32) | low;
130 } else {
131 bits = (low << 32) | high;
132 }
133 } else {
134 bits = input.getInt(length, signed);
135 bits = bits & 0x00000000FFFFFFFFL;
136 /*
137 * The previous line loses sign information but is necessary, this
138 * fixes the sign for 32 bit numbers. Sorry, in java all 64 bit ints
139 * are signed.
140 */
141 if ((longNegBit == (bits & longNegBit)) && signed) {
142 bits |= 0xffffffff00000000L;
143 }
144 }
145 /*
146 * Put the input buffer's endianness back to original if it was changed
147 */
148 if (byteOrder != input.getByteOrder()) {
149 input.setByteOrder(byteOrder);
150 }
151
152 value = bits;
153 }
154
155 @Override
156 public String toString() {
157 if (declaration.isCharacter()) {
158 char c = (char) value;
159 return Character.toString(c);
160 }
161 return String.valueOf(value);
162 }
163 }
This page took 0.033672 seconds and 5 git commands to generate.