tmf/lttng: Remove unneeded (non-Javadoc) comments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / IntegerDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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 @Override
86 public Long getIntegerValue() {
87 return getValue();
88 }
89
90 @Override
91 public String getStringValue() {
92 return this.toString();
93 }
94
95 @Override
96 public void read(BitBuffer input) {
97 final long longNegBit = 0x0000000080000000L;
98 int align = (int) declaration.getAlignment();
99 int pos = input.position() + ((align - (input.position() % align)) % align);
100 input.position(pos);
101 boolean signed = declaration.isSigned();
102 int length = declaration.getLength();
103 long bits = 0;
104
105 /*
106 * Is the endianness of this field the same as the endianness of the
107 * input buffer? If not, then temporarily set the buffer's endianness to
108 * this field's just to read the data
109 */
110 ByteOrder byteOrder = input.getByteOrder();
111 if ((this.declaration.getByteOrder() != null) &&
112 (this.declaration.getByteOrder() != input.getByteOrder())) {
113 input.setByteOrder(this.declaration.getByteOrder());
114 }
115
116 // TODO: use the eventual getLong from BitBuffer
117 if (length == 64) {
118 long low = input.getInt(32, false);
119 low = low & 0x00000000FFFFFFFFL;
120 long high = input.getInt(32, false);
121 high = high & 0x00000000FFFFFFFFL;
122 if (this.declaration.getByteOrder() != ByteOrder.BIG_ENDIAN) {
123 bits = (high << 32) | low;
124 } else {
125 bits = (low << 32) | high;
126 }
127 } else {
128 bits = input.getInt(length, signed);
129 bits = bits & 0x00000000FFFFFFFFL;
130 /*
131 * The previous line loses sign information but is necessary, this
132 * fixes the sign for 32 bit numbers. Sorry, in java all 64 bit ints
133 * are signed.
134 */
135 if ((longNegBit == (bits & longNegBit)) && signed) {
136 bits |= 0xffffffff00000000L;
137 }
138 }
139 /*
140 * Put the input buffer's endianness back to original if it was changed
141 */
142 if (byteOrder != input.getByteOrder()) {
143 input.setByteOrder(byteOrder);
144 }
145
146 value = bits;
147 }
148
149 @Override
150 public String toString() {
151 if (declaration.isCharacter()) {
152 char c = (char) value;
153 return Character.toString(c);
154 }
155 return String.valueOf(value);
156 }
157 }
This page took 0.03317 seconds and 5 git commands to generate.