Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/ConversionUtils.java
41162 views
1
/*
2
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
package nsk.share.jpda;
24
25
/*
26
Static methods checking whether given primitive type value can be
27
converted to another primitive type without information loss
28
29
Note:
30
the spec defines following 2 types of primitive values conversions:
31
32
Widening primitive conversions (don't loose information, but may lose precision):
33
* byte to short, int, long, float, or double
34
* short to int, long, float, or double
35
* char to int, long, float, or double
36
* int to long, float, or double
37
* long to float or double
38
* float to double
39
40
Narrowing primitive conversions (may loose information and may loose precision):
41
* byte to char
42
* short to byte or char
43
* char to byte or short
44
* int to byte, short, or char
45
* long to byte, short, char, or int
46
* float to byte, short, char, int, or long
47
* double to byte, short, char, int, long, or float
48
49
Examples:
50
Conversions (int)1234567890 -> (float)1.23456794E9 and (float)1.5 -> (int)1 loose precision.
51
Conversion (byte)-1 -> (char) ffff and (double)Double.MAX_VALUE -> (int)Integer.MAX_VALUE loose information.
52
53
(See the "JavaTM Language Specification" section 5.2 for more information
54
on assignment compatibility)
55
*/
56
public class ConversionUtils {
57
58
/*
59
* Methods checking that value can be converted to the value of the
60
* same type (like 'informationLossByteToByte') were added to simplify
61
* clients coding (when this methods exist clients shouldn't handle this case
62
* in a specific way)
63
*/
64
65
/*
66
* Byte
67
*/
68
public static boolean informationLossByteToByte(Byte value) {
69
return false;
70
}
71
72
public static boolean informationLossByteToShort(Byte value) {
73
return false;
74
}
75
76
public static boolean informationLossByteToChar(Byte value) {
77
return (value.byteValue() > Character.MAX_VALUE) || (value.byteValue() < Character.MIN_VALUE);
78
}
79
80
public static boolean informationLossByteToInt(Byte value) {
81
return false;
82
}
83
84
public static boolean informationLossByteToLong(Byte value) {
85
return false;
86
}
87
88
public static boolean informationLossByteToFloat(Byte value) {
89
return false;
90
}
91
92
public static boolean informationLossByteToDouble(Byte value) {
93
return false;
94
}
95
96
/*
97
* Short
98
*/
99
public static boolean informationLossShortToShort(Short value) {
100
return false;
101
}
102
103
public static boolean informationLossShortToByte(Short value) {
104
return (value.shortValue() > Byte.MAX_VALUE) || (value.shortValue() < Byte.MIN_VALUE);
105
}
106
107
public static boolean informationLossShortToChar(Short value) {
108
return (value.shortValue() > Character.MAX_VALUE) || (value.shortValue() < Character.MIN_VALUE);
109
}
110
111
public static boolean informationLossShortToInt(Short value) {
112
return false;
113
}
114
115
public static boolean informationLossShortToLong(Short value) {
116
return false;
117
}
118
119
public static boolean informationLossShortToFloat(Short value) {
120
return false;
121
}
122
123
public static boolean informationLossShortToDouble(Short value) {
124
return false;
125
}
126
127
/*
128
* Char
129
*/
130
public static boolean informationLossCharToChar(Character value) {
131
return false;
132
}
133
134
public static boolean informationLossCharToByte(Character value) {
135
return (value.charValue() > Byte.MAX_VALUE) || (value.charValue() < Byte.MIN_VALUE);
136
}
137
138
public static boolean informationLossCharToShort(Character value) {
139
return (value.charValue() > Short.MAX_VALUE) || (value.charValue() < Short.MIN_VALUE);
140
}
141
142
public static boolean informationLossCharToInt(Character value) {
143
return false;
144
}
145
146
public static boolean informationLossCharToLong(Character value) {
147
return false;
148
}
149
150
public static boolean informationLossCharToFloat(Character value) {
151
return false;
152
}
153
154
public static boolean informationLossCharToDouble(Character value) {
155
return false;
156
}
157
158
/*
159
* Integer
160
*/
161
public static boolean informationLossIntToInt(Integer value) {
162
return false;
163
}
164
165
public static boolean informationLossIntToByte(Integer value) {
166
return (value.intValue() > Byte.MAX_VALUE) || (value.intValue() < Byte.MIN_VALUE);
167
}
168
169
public static boolean informationLossIntToShort(Integer value) {
170
return (value.intValue() > Short.MAX_VALUE) || (value.intValue() < Short.MIN_VALUE);
171
}
172
173
public static boolean informationLossIntToChar(Integer value) {
174
return (value.intValue() > Character.MAX_VALUE) || (value.intValue() < Character.MIN_VALUE);
175
}
176
177
public static boolean informationLossIntToLong(Integer value) {
178
return false;
179
}
180
181
public static boolean informationLossIntToFloat(Integer value) {
182
return false;
183
}
184
185
public static boolean informationLossIntToDouble(Integer value) {
186
return false;
187
}
188
189
/*
190
* Long
191
*/
192
public static boolean informationLossLongToLong(Long value) {
193
return false;
194
}
195
196
public static boolean informationLossLongToByte(Long value) {
197
return (value.longValue() > Byte.MAX_VALUE) || (value.longValue() < Byte.MIN_VALUE);
198
}
199
200
public static boolean informationLossLongToShort(Long value) {
201
return (value.longValue() > Short.MAX_VALUE) || (value.longValue() < Short.MIN_VALUE);
202
}
203
204
public static boolean informationLossLongToChar(Long value) {
205
return (value.longValue() > Character.MAX_VALUE) || (value.longValue() < Character.MIN_VALUE);
206
}
207
208
public static boolean informationLossLongToInt(Long value) {
209
return (value.longValue() > Integer.MAX_VALUE) || (value.longValue() < Integer.MIN_VALUE);
210
}
211
212
public static boolean informationLossLongToFloat(Long value) {
213
return false;
214
}
215
216
public static boolean informationLossLongToDouble(Long value) {
217
return false;
218
}
219
220
/*
221
* Float
222
*/
223
public static boolean informationLossFloatToFloat(Float value) {
224
return false;
225
}
226
227
public static boolean informationLossFloatToByte(Float value) {
228
if (value.isInfinite())
229
return true;
230
231
if (value.isNaN())
232
return true;
233
234
return (value.floatValue() > Byte.MAX_VALUE) || (value.floatValue() < Byte.MIN_VALUE);
235
}
236
237
public static boolean informationLossFloatToShort(Float value) {
238
if (value.isInfinite())
239
return true;
240
241
if (value.isNaN())
242
return true;
243
244
return (value.floatValue() > Short.MAX_VALUE) || (value.floatValue() < Short.MIN_VALUE);
245
}
246
247
public static boolean informationLossFloatToChar(Float value) {
248
if (value.isInfinite())
249
return true;
250
251
if (value.isNaN())
252
return true;
253
254
return (value.floatValue() > Character.MAX_VALUE) || (value.floatValue() < Character.MIN_VALUE);
255
}
256
257
public static boolean informationLossFloatToInt(Float value) {
258
if (value.isInfinite())
259
return true;
260
261
if (value.isNaN())
262
return true;
263
264
return (value.floatValue() > Integer.MAX_VALUE) || (value.floatValue() < Integer.MIN_VALUE)
265
|| ((int)value.floatValue() != value.floatValue());
266
}
267
268
public static boolean informationLossFloatToLong(Float value) {
269
if (value.isInfinite())
270
return true;
271
272
if (value.isNaN())
273
return true;
274
275
return (value.floatValue() > Long.MAX_VALUE) || (value.floatValue() < Long.MIN_VALUE)
276
|| ((long)value.floatValue() != value.floatValue());
277
}
278
279
public static boolean informationLossFloatToDouble(Float value) {
280
return false;
281
}
282
283
284
/*
285
* Double
286
*/
287
public static boolean informationLossDoubleToDouble(Double value) {
288
return false;
289
}
290
291
public static boolean informationLossDoubleToByte(Double value) {
292
if (value.isInfinite())
293
return true;
294
295
if (value.isNaN())
296
return true;
297
298
return (value.doubleValue() > Byte.MAX_VALUE) || (value.doubleValue() < Byte.MIN_VALUE);
299
}
300
301
public static boolean informationLossDoubleToShort(Double value) {
302
if (value.isInfinite())
303
return true;
304
305
if (value.isNaN())
306
return true;
307
308
return (value.doubleValue() > Short.MAX_VALUE) || (value.doubleValue() < Short.MIN_VALUE);
309
}
310
311
public static boolean informationLossDoubleToChar(Double value) {
312
if (value.isInfinite())
313
return true;
314
315
if (value.isNaN())
316
return true;
317
318
return (value.doubleValue() > Character.MAX_VALUE) || (value.doubleValue() < Character.MIN_VALUE);
319
}
320
321
public static boolean informationLossDoubleToInt(Double value) {
322
if (value.isInfinite())
323
return true;
324
325
if (value.isNaN())
326
return true;
327
328
return (value.doubleValue() > Integer.MAX_VALUE) || (value.doubleValue() < Integer.MIN_VALUE);
329
}
330
331
public static boolean informationLossDoubleToLong(Double value) {
332
if (value.isInfinite())
333
return true;
334
335
if (value.isNaN())
336
return true;
337
338
return (value.doubleValue() > Long.MAX_VALUE) || (value.doubleValue() < Long.MIN_VALUE)
339
|| ((long)value.doubleValue() != value.doubleValue());
340
}
341
342
public static boolean informationLossDoubleToFloat(Double value) {
343
if (value.isInfinite())
344
return false;
345
346
if (value.isNaN())
347
return false;
348
349
float f = (float) value.doubleValue();
350
351
return f != value.doubleValue();
352
}
353
}
354
355