Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/ConversionUtils.java
41162 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package nsk.share.jpda;2324/*25Static methods checking whether given primitive type value can be26converted to another primitive type without information loss2728Note:29the spec defines following 2 types of primitive values conversions:3031Widening primitive conversions (don't loose information, but may lose precision):32* byte to short, int, long, float, or double33* short to int, long, float, or double34* char to int, long, float, or double35* int to long, float, or double36* long to float or double37* float to double3839Narrowing primitive conversions (may loose information and may loose precision):40* byte to char41* short to byte or char42* char to byte or short43* int to byte, short, or char44* long to byte, short, char, or int45* float to byte, short, char, int, or long46* double to byte, short, char, int, long, or float4748Examples:49Conversions (int)1234567890 -> (float)1.23456794E9 and (float)1.5 -> (int)1 loose precision.50Conversion (byte)-1 -> (char) ffff and (double)Double.MAX_VALUE -> (int)Integer.MAX_VALUE loose information.5152(See the "JavaTM Language Specification" section 5.2 for more information53on assignment compatibility)54*/55public class ConversionUtils {5657/*58* Methods checking that value can be converted to the value of the59* same type (like 'informationLossByteToByte') were added to simplify60* clients coding (when this methods exist clients shouldn't handle this case61* in a specific way)62*/6364/*65* Byte66*/67public static boolean informationLossByteToByte(Byte value) {68return false;69}7071public static boolean informationLossByteToShort(Byte value) {72return false;73}7475public static boolean informationLossByteToChar(Byte value) {76return (value.byteValue() > Character.MAX_VALUE) || (value.byteValue() < Character.MIN_VALUE);77}7879public static boolean informationLossByteToInt(Byte value) {80return false;81}8283public static boolean informationLossByteToLong(Byte value) {84return false;85}8687public static boolean informationLossByteToFloat(Byte value) {88return false;89}9091public static boolean informationLossByteToDouble(Byte value) {92return false;93}9495/*96* Short97*/98public static boolean informationLossShortToShort(Short value) {99return false;100}101102public static boolean informationLossShortToByte(Short value) {103return (value.shortValue() > Byte.MAX_VALUE) || (value.shortValue() < Byte.MIN_VALUE);104}105106public static boolean informationLossShortToChar(Short value) {107return (value.shortValue() > Character.MAX_VALUE) || (value.shortValue() < Character.MIN_VALUE);108}109110public static boolean informationLossShortToInt(Short value) {111return false;112}113114public static boolean informationLossShortToLong(Short value) {115return false;116}117118public static boolean informationLossShortToFloat(Short value) {119return false;120}121122public static boolean informationLossShortToDouble(Short value) {123return false;124}125126/*127* Char128*/129public static boolean informationLossCharToChar(Character value) {130return false;131}132133public static boolean informationLossCharToByte(Character value) {134return (value.charValue() > Byte.MAX_VALUE) || (value.charValue() < Byte.MIN_VALUE);135}136137public static boolean informationLossCharToShort(Character value) {138return (value.charValue() > Short.MAX_VALUE) || (value.charValue() < Short.MIN_VALUE);139}140141public static boolean informationLossCharToInt(Character value) {142return false;143}144145public static boolean informationLossCharToLong(Character value) {146return false;147}148149public static boolean informationLossCharToFloat(Character value) {150return false;151}152153public static boolean informationLossCharToDouble(Character value) {154return false;155}156157/*158* Integer159*/160public static boolean informationLossIntToInt(Integer value) {161return false;162}163164public static boolean informationLossIntToByte(Integer value) {165return (value.intValue() > Byte.MAX_VALUE) || (value.intValue() < Byte.MIN_VALUE);166}167168public static boolean informationLossIntToShort(Integer value) {169return (value.intValue() > Short.MAX_VALUE) || (value.intValue() < Short.MIN_VALUE);170}171172public static boolean informationLossIntToChar(Integer value) {173return (value.intValue() > Character.MAX_VALUE) || (value.intValue() < Character.MIN_VALUE);174}175176public static boolean informationLossIntToLong(Integer value) {177return false;178}179180public static boolean informationLossIntToFloat(Integer value) {181return false;182}183184public static boolean informationLossIntToDouble(Integer value) {185return false;186}187188/*189* Long190*/191public static boolean informationLossLongToLong(Long value) {192return false;193}194195public static boolean informationLossLongToByte(Long value) {196return (value.longValue() > Byte.MAX_VALUE) || (value.longValue() < Byte.MIN_VALUE);197}198199public static boolean informationLossLongToShort(Long value) {200return (value.longValue() > Short.MAX_VALUE) || (value.longValue() < Short.MIN_VALUE);201}202203public static boolean informationLossLongToChar(Long value) {204return (value.longValue() > Character.MAX_VALUE) || (value.longValue() < Character.MIN_VALUE);205}206207public static boolean informationLossLongToInt(Long value) {208return (value.longValue() > Integer.MAX_VALUE) || (value.longValue() < Integer.MIN_VALUE);209}210211public static boolean informationLossLongToFloat(Long value) {212return false;213}214215public static boolean informationLossLongToDouble(Long value) {216return false;217}218219/*220* Float221*/222public static boolean informationLossFloatToFloat(Float value) {223return false;224}225226public static boolean informationLossFloatToByte(Float value) {227if (value.isInfinite())228return true;229230if (value.isNaN())231return true;232233return (value.floatValue() > Byte.MAX_VALUE) || (value.floatValue() < Byte.MIN_VALUE);234}235236public static boolean informationLossFloatToShort(Float value) {237if (value.isInfinite())238return true;239240if (value.isNaN())241return true;242243return (value.floatValue() > Short.MAX_VALUE) || (value.floatValue() < Short.MIN_VALUE);244}245246public static boolean informationLossFloatToChar(Float value) {247if (value.isInfinite())248return true;249250if (value.isNaN())251return true;252253return (value.floatValue() > Character.MAX_VALUE) || (value.floatValue() < Character.MIN_VALUE);254}255256public static boolean informationLossFloatToInt(Float value) {257if (value.isInfinite())258return true;259260if (value.isNaN())261return true;262263return (value.floatValue() > Integer.MAX_VALUE) || (value.floatValue() < Integer.MIN_VALUE)264|| ((int)value.floatValue() != value.floatValue());265}266267public static boolean informationLossFloatToLong(Float value) {268if (value.isInfinite())269return true;270271if (value.isNaN())272return true;273274return (value.floatValue() > Long.MAX_VALUE) || (value.floatValue() < Long.MIN_VALUE)275|| ((long)value.floatValue() != value.floatValue());276}277278public static boolean informationLossFloatToDouble(Float value) {279return false;280}281282283/*284* Double285*/286public static boolean informationLossDoubleToDouble(Double value) {287return false;288}289290public static boolean informationLossDoubleToByte(Double value) {291if (value.isInfinite())292return true;293294if (value.isNaN())295return true;296297return (value.doubleValue() > Byte.MAX_VALUE) || (value.doubleValue() < Byte.MIN_VALUE);298}299300public static boolean informationLossDoubleToShort(Double value) {301if (value.isInfinite())302return true;303304if (value.isNaN())305return true;306307return (value.doubleValue() > Short.MAX_VALUE) || (value.doubleValue() < Short.MIN_VALUE);308}309310public static boolean informationLossDoubleToChar(Double value) {311if (value.isInfinite())312return true;313314if (value.isNaN())315return true;316317return (value.doubleValue() > Character.MAX_VALUE) || (value.doubleValue() < Character.MIN_VALUE);318}319320public static boolean informationLossDoubleToInt(Double value) {321if (value.isInfinite())322return true;323324if (value.isNaN())325return true;326327return (value.doubleValue() > Integer.MAX_VALUE) || (value.doubleValue() < Integer.MIN_VALUE);328}329330public static boolean informationLossDoubleToLong(Double value) {331if (value.isInfinite())332return true;333334if (value.isNaN())335return true;336337return (value.doubleValue() > Long.MAX_VALUE) || (value.doubleValue() < Long.MIN_VALUE)338|| ((long)value.doubleValue() != value.doubleValue());339}340341public static boolean informationLossDoubleToFloat(Double value) {342if (value.isInfinite())343return false;344345if (value.isNaN())346return false;347348float f = (float) value.doubleValue();349350return f != value.doubleValue();351}352}353354355