ctf: Fix some Sonar warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / IntegerDeclaration.java
CommitLineData
866e5b51 1/*******************************************************************************
4311ac8b 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 *
4311ac8b
MAL
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 * Simon Marchi - Initial API and implementation
12 * Marc-Andre Laperle - Add min/maximum for validation
866e5b51
FC
13 *******************************************************************************/
14
15package org.eclipse.linuxtools.ctf.core.event.types;
16
4311ac8b 17import java.math.BigInteger;
866e5b51
FC
18import java.nio.ByteOrder;
19
20/**
d37aaa7f 21 * A CTF integer declaration.
4311ac8b 22 *
d37aaa7f
FC
23 * The declaration of a integer basic data type.
24 *
25 * @version 1.0
26 * @author Matthew Khouzam
27 * @author Simon Marchi
866e5b51
FC
28 */
29public class IntegerDeclaration implements IDeclaration {
30
31 // ------------------------------------------------------------------------
32 // Attributes
33 // ------------------------------------------------------------------------
34
0594c61c
AM
35 private final int length;
36 private final boolean signed;
37 private final int base;
38 private final ByteOrder byteOrder;
39 private final Encoding encoding;
40 private final long alignment;
41 private final String clock;
866e5b51
FC
42
43 // ------------------------------------------------------------------------
44 // Constructors
45 // ------------------------------------------------------------------------
46
9ac2eb62
MK
47 /**
48 * Contructor
49 * @param len the length in bits
50 * @param signed is the integer signed? false == unsigned
51 * @param base the base (10-16 are most common)
52 * @param byteOrder Big endian little endian or other
53 * @param encoding ascii, utf8 or none.
54 * @param clock the clock path, can be null
55 * @param alignment the minimum alignment
56 */
866e5b51 57 public IntegerDeclaration(int len, boolean signed, int base,
fd74e6c1 58 ByteOrder byteOrder, Encoding encoding, String clock, long alignment) {
4311ac8b
MAL
59 if (len <= 0 || len == 1 && signed) {
60 throw new IllegalArgumentException();
61 }
866e5b51
FC
62 this.length = len;
63 this.signed = signed;
64 this.base = base;
65 this.byteOrder = byteOrder;
66 this.encoding = encoding;
284fdee8 67 this.clock = clock;
fd74e6c1 68 this.alignment = alignment;
866e5b51
FC
69 }
70
71 // ------------------------------------------------------------------------
72 // Gettters/Setters/Predicates
73 // ------------------------------------------------------------------------
74
9ac2eb62
MK
75 /**
76 * Is the integer signed?
77 * @return the is the integer signed
78 */
866e5b51
FC
79 public boolean isSigned() {
80 return signed;
81 }
82
9ac2eb62
MK
83 /**
84 * get the integer base commonly decimal or hex
85 * @return the integer base
86 */
866e5b51
FC
87 public int getBase() {
88 return base;
89 }
90
9ac2eb62
MK
91 /**
92 * gets the byte order
93 * @return the byte order
94 */
866e5b51
FC
95 public ByteOrder getByteOrder() {
96 return byteOrder;
97 }
98
9ac2eb62
MK
99 /**
100 * get encoding, chars are 8 bit ints
101 * @return the encoding
102 */
866e5b51
FC
103 public Encoding getEncoding() {
104 return encoding;
105 }
106
9ac2eb62
MK
107 /**
108 * is the integer a character (8 bits and encoded?)
109 * @return is the integer a char
110 */
fd74e6c1 111 public boolean isCharacter() {
866e5b51
FC
112 return (length == 8) && (encoding != Encoding.NONE);
113 }
114
9ac2eb62
MK
115 /**
116 * How many bits is this int
117 * @return the length of the int
118 */
866e5b51
FC
119 public int getLength() {
120 return length;
121 }
122
07002e0a 123 @Override
fd74e6c1
MK
124 public long getAlignment(){
125 return alignment;
126 }
127
9ac2eb62
MK
128 /**
129 * The integer's clock, since timestamps are stored in ints
130 * @return the integer's clock, can be null. (most often it is)
131 */
fd74e6c1
MK
132 public String getClock(){
133 return clock;
134 }
866e5b51
FC
135 // ------------------------------------------------------------------------
136 // Operations
137 // ------------------------------------------------------------------------
138
139 @Override
140 public IntegerDefinition createDefinition(IDefinitionScope definitionScope,
141 String fieldName) {
142 return new IntegerDefinition(this, definitionScope, fieldName);
143 }
144
145 @Override
146 public String toString() {
147 /* Only used for debugging */
148 return "[declaration] integer[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
149 }
150
4311ac8b
MAL
151 /**
152 * Get the maximum value for this integer declaration
153 *
154 * @return The maximum value for this integer declaration
155 * @since 2.0
156 */
157 public BigInteger getMaxValue() {
158 BigInteger capacity = BigInteger.ONE.shiftLeft(length);
159 BigInteger max = signed ? capacity.divide(BigInteger.valueOf(2)) : capacity;
160 return max.subtract(BigInteger.ONE);
161 }
162
163 /**
164 * Get the minimum value for this integer declaration
165 *
166 * @return The minimum value for this integer declaration
167 * @since 2.0
168 */
169 public BigInteger getMinValue() {
170 if (!signed) {
171 return BigInteger.ZERO;
172 }
173
174 BigInteger capacity = BigInteger.ONE.shiftLeft(length);
175 return capacity.divide(BigInteger.valueOf(2)).negate();
176 }
177
866e5b51 178}
This page took 0.038278 seconds and 5 git commands to generate.