Commit | Line | Data |
---|---|---|
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 | ||
15 | package org.eclipse.linuxtools.ctf.core.event.types; | |
16 | ||
4311ac8b | 17 | import java.math.BigInteger; |
866e5b51 FC |
18 | import 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 | */ |
29 | public 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 | |
056ebaf1 AM |
49 | * |
50 | * @param len | |
51 | * The length in bits | |
52 | * @param signed | |
53 | * Is the integer signed? false == unsigned | |
54 | * @param base | |
55 | * The base (10-16 are most common) | |
56 | * @param byteOrder | |
57 | * Big endian little endian or other | |
58 | * @param encoding | |
59 | * ascii, utf8 or none. | |
60 | * @param clock | |
61 | * The clock path, can be null | |
62 | * @param alignment | |
63 | * The minimum alignment. Should be >= 1 | |
9ac2eb62 | 64 | */ |
866e5b51 | 65 | public IntegerDeclaration(int len, boolean signed, int base, |
fd74e6c1 | 66 | ByteOrder byteOrder, Encoding encoding, String clock, long alignment) { |
4311ac8b MAL |
67 | if (len <= 0 || len == 1 && signed) { |
68 | throw new IllegalArgumentException(); | |
69 | } | |
866e5b51 FC |
70 | this.length = len; |
71 | this.signed = signed; | |
72 | this.base = base; | |
73 | this.byteOrder = byteOrder; | |
74 | this.encoding = encoding; | |
284fdee8 | 75 | this.clock = clock; |
056ebaf1 | 76 | this.alignment = Math.max(alignment, 1); |
866e5b51 FC |
77 | } |
78 | ||
79 | // ------------------------------------------------------------------------ | |
80 | // Gettters/Setters/Predicates | |
81 | // ------------------------------------------------------------------------ | |
82 | ||
9ac2eb62 MK |
83 | /** |
84 | * Is the integer signed? | |
85 | * @return the is the integer signed | |
86 | */ | |
866e5b51 FC |
87 | public boolean isSigned() { |
88 | return signed; | |
89 | } | |
90 | ||
9ac2eb62 MK |
91 | /** |
92 | * get the integer base commonly decimal or hex | |
93 | * @return the integer base | |
94 | */ | |
866e5b51 FC |
95 | public int getBase() { |
96 | return base; | |
97 | } | |
98 | ||
9ac2eb62 MK |
99 | /** |
100 | * gets the byte order | |
101 | * @return the byte order | |
102 | */ | |
866e5b51 FC |
103 | public ByteOrder getByteOrder() { |
104 | return byteOrder; | |
105 | } | |
106 | ||
9ac2eb62 MK |
107 | /** |
108 | * get encoding, chars are 8 bit ints | |
109 | * @return the encoding | |
110 | */ | |
866e5b51 FC |
111 | public Encoding getEncoding() { |
112 | return encoding; | |
113 | } | |
114 | ||
9ac2eb62 MK |
115 | /** |
116 | * is the integer a character (8 bits and encoded?) | |
117 | * @return is the integer a char | |
118 | */ | |
fd74e6c1 | 119 | public boolean isCharacter() { |
866e5b51 FC |
120 | return (length == 8) && (encoding != Encoding.NONE); |
121 | } | |
122 | ||
9ac2eb62 MK |
123 | /** |
124 | * How many bits is this int | |
125 | * @return the length of the int | |
126 | */ | |
866e5b51 FC |
127 | public int getLength() { |
128 | return length; | |
129 | } | |
130 | ||
07002e0a | 131 | @Override |
fd74e6c1 MK |
132 | public long getAlignment(){ |
133 | return alignment; | |
134 | } | |
135 | ||
9ac2eb62 MK |
136 | /** |
137 | * The integer's clock, since timestamps are stored in ints | |
138 | * @return the integer's clock, can be null. (most often it is) | |
139 | */ | |
fd74e6c1 MK |
140 | public String getClock(){ |
141 | return clock; | |
142 | } | |
866e5b51 FC |
143 | // ------------------------------------------------------------------------ |
144 | // Operations | |
145 | // ------------------------------------------------------------------------ | |
146 | ||
147 | @Override | |
148 | public IntegerDefinition createDefinition(IDefinitionScope definitionScope, | |
149 | String fieldName) { | |
150 | return new IntegerDefinition(this, definitionScope, fieldName); | |
151 | } | |
152 | ||
153 | @Override | |
154 | public String toString() { | |
155 | /* Only used for debugging */ | |
156 | return "[declaration] integer[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
157 | } | |
158 | ||
4311ac8b MAL |
159 | /** |
160 | * Get the maximum value for this integer declaration | |
161 | * | |
162 | * @return The maximum value for this integer declaration | |
163 | * @since 2.0 | |
164 | */ | |
165 | public BigInteger getMaxValue() { | |
166 | BigInteger capacity = BigInteger.ONE.shiftLeft(length); | |
167 | BigInteger max = signed ? capacity.divide(BigInteger.valueOf(2)) : capacity; | |
168 | return max.subtract(BigInteger.ONE); | |
169 | } | |
170 | ||
171 | /** | |
172 | * Get the minimum value for this integer declaration | |
173 | * | |
174 | * @return The minimum value for this integer declaration | |
175 | * @since 2.0 | |
176 | */ | |
177 | public BigInteger getMinValue() { | |
178 | if (!signed) { | |
179 | return BigInteger.ZERO; | |
180 | } | |
181 | ||
182 | BigInteger capacity = BigInteger.ONE.shiftLeft(length); | |
183 | return capacity.divide(BigInteger.valueOf(2)).negate(); | |
184 | } | |
185 | ||
866e5b51 | 186 | } |