1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2000-2009. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
package com.ericsson.otp.erlang;
import java.math.BigInteger;
/**
* Provides a Java representation of Erlang integral types. Erlang does not
* distinguish between different integral types, however this class and its
* subclasses {@link OtpErlangByte}, {@link OtpErlangChar}, {@link OtpErlangInt}
* , and {@link OtpErlangShort} attempt to map the Erlang types onto the various
* Java integral types. Two additional classes, {@link OtpErlangUInt} and
* {@link OtpErlangUShort} are provided for Corba compatibility. See the
* documentation for IC for more information.
*/
public class OtpErlangLong extends OtpErlangObject {
// don't change this!
private static final long serialVersionUID = 1610466859236755096L;
private long val;
private BigInteger bigVal = null;
/**
* Create an Erlang integer from the given value.
*
* @param l
* the long value to use.
*/
public OtpErlangLong(final long l) {
val = l;
}
/**
* Create an Erlang integer from the given value.
*
* @param v
* the big integer value to use.
*/
public OtpErlangLong(final BigInteger v) {
if (v == null) {
throw new java.lang.NullPointerException();
}
if (v.bitLength() < 64) {
val = v.longValue();
} else {
bigVal = v;
}
}
/**
* Create an Erlang integer from a stream containing an integer encoded in
* Erlang external format.
*
* @param buf
* the stream containing the encoded value.
*
* @exception OtpErlangDecodeException
* if the buffer does not contain a valid external
* representation of an Erlang integer.
*/
public OtpErlangLong(final OtpInputStream buf)
throws OtpErlangDecodeException {
final byte[] b = buf.read_integer_byte_array();
try {
val = OtpInputStream.byte_array_to_long(b, false);
} catch (final OtpErlangDecodeException e) {
bigVal = new BigInteger(b);
}
}
/**
* Get this number as a BigInteger.
*
* @return the value of this number, as a BigInteger.
*/
public BigInteger bigIntegerValue() {
if (bigVal != null) {
return bigVal;
}
return BigInteger.valueOf(val);
}
/**
* Get this number as a long, or rather truncate all but the least
* significant 64 bits from the 2's complement representation of this number
* and return them as a long.
*
* @return the value of this number, as a long.
*/
public long longValue() {
if (bigVal != null) {
return bigVal.longValue();
}
return val;
}
/**
* Determine if this value can be represented as a long without truncation.
*
* @return true if this value fits in a long, false otherwise.
*/
public boolean isLong() {
// To just chech this.bigVal is a wee bit to simple, since
// there just might have be a mean bignum that arrived on
// a stream, and was a long disguised as more than 8 byte integer.
if (bigVal != null) {
return bigVal.bitLength() < 64;
}
return true;
}
/**
* Determine if this value can be represented as an unsigned long without
* truncation, that is if the value is non-negative and its bit pattern
* completely fits in a long.
*
* @return true if this value is non-negative and fits in a long false
* otherwise.
*/
public boolean isULong() {
// Here we have the same problem as for isLong(), plus
// the whole range 1<<63 .. (1<<64-1) is allowed.
if (bigVal != null) {
return bigVal.signum() >= 0 && bigVal.bitLength() <= 64;
}
return val >= 0;
}
/**
* Returns the number of bits in the minimal two's-complement representation
* of this BigInteger, excluding a sign bit.
*
* @return number of bits in the minimal two's-complement representation of
* this BigInteger, excluding a sign bit.
*/
public int bitLength() {
if (bigVal != null) {
return bigVal.bitLength();
}
if (val == 0 || val == -1) {
return 0;
}
// Binary search for bit length
int i = 32; // mask length
long m = (1L << i) - 1; // AND mask with ones in little end
if (val < 0) {
m = ~m; // OR mask with ones in big end
for (int j = i >> 1; j > 0; j >>= 1) { // mask delta
if ((val | m) == val) { // mask >= enough
i -= j;
m >>= j; // try less bits
} else {
i += j;
m <<= j; // try more bits
}
}
if ((val | m) != val) {
i++; // mask < enough
}
} else {
for (int j = i >> 1; j > 0; j >>= 1) { // mask delta
if ((val & m) == val) { // mask >= enough
i -= j;
m >>= j; // try less bits
} else {
i += j;
m = m << j | m; // try more bits
}
}
if ((val & m) != val) {
i++; // mask < enough
}
}
return i;
}
/**
* Return the signum function of this object.
*
* @return -1, 0 or 1 as the value is negative, zero or positive.
*/
public int signum() {
if (bigVal != null) {
return bigVal.signum();
}
return val > 0 ? 1 : val < 0 ? -1 : 0;
}
/**
* Get this number as an int.
*
* @return the value of this number, as an int.
*
* @exception OtpErlangRangeException
* if the value is too large to be represented as an int.
*/
public int intValue() throws OtpErlangRangeException {
final long l = longValue();
final int i = (int) l;
if (i != l) {
throw new OtpErlangRangeException("Value too large for int: " + val);
}
return i;
}
/**
* Get this number as a non-negative int.
*
* @return the value of this number, as an int.
*
* @exception OtpErlangRangeException
* if the value is too large to be represented as an int, or
* if the value is negative.
*/
public int uIntValue() throws OtpErlangRangeException {
final long l = longValue();
final int i = (int) l;
if (i != l) {
throw new OtpErlangRangeException("Value too large for int: " + val);
} else if (i < 0) {
throw new OtpErlangRangeException("Value not positive: " + val);
}
return i;
}
/**
* Get this number as a short.
*
* @return the value of this number, as a short.
*
* @exception OtpErlangRangeException
* if the value is too large to be represented as a short.
*/
public short shortValue() throws OtpErlangRangeException {
final long l = longValue();
final short i = (short) l;
if (i != l) {
throw new OtpErlangRangeException("Value too large for short: "
+ val);
}
return i;
}
/**
* Get this number as a non-negative short.
*
* @return the value of this number, as a short.
*
* @exception OtpErlangRangeException
* if the value is too large to be represented as a short, or
* if the value is negative.
*/
public short uShortValue() throws OtpErlangRangeException {
final long l = longValue();
final short i = (short) l;
if (i != l) {
throw new OtpErlangRangeException("Value too large for short: "
+ val);
} else if (i < 0) {
throw new OtpErlangRangeException("Value not positive: " + val);
}
return i;
}
/**
* Get this number as a char.
*
* @return the char value of this number.
*
* @exception OtpErlangRangeException
* if the value is too large to be represented as a char.
*/
public char charValue() throws OtpErlangRangeException {
final long l = longValue();
final char i = (char) l;
if (i != l) {
throw new OtpErlangRangeException("Value too large for char: "
+ val);
}
return i;
}
/**
* Get this number as a byte.
*
* @return the byte value of this number.
*
* @exception OtpErlangRangeException
* if the value is too large to be represented as a byte.
*/
public byte byteValue() throws OtpErlangRangeException {
final long l = longValue();
final byte i = (byte) l;
if (i != l) {
throw new OtpErlangRangeException("Value too large for byte: "
+ val);
}
return i;
}
/**
* Get the string representation of this number.
*
* @return the string representation of this number.
*/
@Override
public String toString() {
if (bigVal != null) {
return "" + bigVal;
}
return "" + val;
}
/**
* Convert this number to the equivalent Erlang external representation.
*
* @param buf
* an output stream to which the encoded number should be
* written.
*/
@Override
public void encode(final OtpOutputStream buf) {
if (bigVal != null) {
buf.write_big_integer(bigVal);
} else {
buf.write_long(val);
}
}
/**
* Determine if two numbers are equal. Numbers are equal if they contain the
* same value.
*
* @param o
* the number to compare to.
*
* @return true if the numbers have the same value.
*/
@Override
public boolean equals(final Object o) {
if (!(o instanceof OtpErlangLong)) {
return false;
}
final OtpErlangLong that = (OtpErlangLong) o;
if (bigVal != null && that.bigVal != null) {
return bigVal.equals(that.bigVal);
} else if (bigVal == null && that.bigVal == null) {
return val == that.val;
}
return false;
}
@Override
protected int doHashCode() {
if (bigVal != null) {
return bigVal.hashCode();
}
return BigInteger.valueOf(val).hashCode();
}
}
|