📄 stringtools.java
字号:
if (!num.supportsType(numberClass)) { return dft; } else { return num.toNumber(dft); } } } // ------------------------------------------------------------------------ /** *** Parse the specified byte array, representing a IEEE 754 floating-point into a double value *** @param b The byte array to parse *** @param ofs The offset within the byte array to begin parsing *** @param isBigEndian True if the IEEE 754 with the byte array in in BigEndian order *** @param dft The default double returned if unable to parse a double value *** @return The parsed IEEE 754 double value **/ public static double parseDouble(byte b[], int ofs, boolean isBigEndian, double dft) { /* valid byte array */ if ((b == null) || ((ofs + 8) > b.length)) { return dft; } /* parse IEEE 754 double */ int i = ofs; long doubleLong = 0L; if (isBigEndian) { doubleLong = (((long)b[i+0] & 0xFF) << (7*8)) + (((long)b[i+1] & 0xFF) << (6*8)) + (((long)b[i+2] & 0xFF) << (5*8)) + (((long)b[i+3] & 0xFF) << (4*8)) + (((long)b[i+4] & 0xFF) << (3*8)) + (((long)b[i+5] & 0xFF) << (2*8)) + (((long)b[i+6] & 0xFF) << (1*8)) + ((long)b[i+7] & 0xFF); } else { doubleLong = (((long)b[i+7] & 0xFF) << (7*8)) + (((long)b[i+6] & 0xFF) << (6*8)) + (((long)b[i+5] & 0xFF) << (5*8)) + (((long)b[i+4] & 0xFF) << (4*8)) + (((long)b[i+3] & 0xFF) << (3*8)) + (((long)b[i+2] & 0xFF) << (2*8)) + (((long)b[i+1] & 0xFF) << (1*8)) + ((long)b[i+0] & 0xFF); } return Double.longBitsToDouble(doubleLong); } /** *** Parse the specified object into a double value *** @param data The object to parse *** @param dft The default double value if unable to parse the specified object *** @return The parsed double value **/ public static double parseDouble(Object data, double dft) { if (data == null) { return dft; } else if (data instanceof Number) { return ((Number)data).doubleValue(); } else { return StringTools.parseDouble(data.toString(), dft); } } /** *** Parse the specified String into a double value *** @param data The String to parse *** @param dft The default double value if unable to parse the specified object *** @return The parsed double value **/ public static double parseDouble(String data, double dft) { return StringTools.parseDouble(new FilterNumber(data, Double.class), dft); } /** *** Parse the specified FilterNumber into a double value *** @param num The FilterNumber to parse *** @param dft The default double value if unable to parse the specified object *** @return The parsed double value **/ public static double parseDouble(FilterNumber num, double dft) { if ((num != null) && num.supportsType(Double.class)) { try { return Double.parseDouble(num.getValueString()); } catch (NumberFormatException nfe) { // ignore } } return dft; } /** *** Return true if the specified String contains a valid double value *** @param data The String to test *** @param strict True to test for a strict double value (ie. does not contain *** any other superfluous trailing characters), false to allow for *** other non-critical trailing characters. *** @return True if the specified String contains a valid double value **/ public static boolean isDouble(String data, boolean strict) { if (StringTools.isBlank(data)) { return false; } else { FilterNumber fn = new FilterNumber(data, Double.class); return fn.isValid(strict); } } // ------------------------------------------------------------------------ /** *** Parse the specified byte array, representing a IEEE 754 floating-point into a float value *** @param b The byte array to parse *** @param ofs The offset within the byte array to begin parsing *** @param isBigEndian True if the IEEE 754 with the byte array in in BigEndian order *** @param dft The default float returned if unable to parse a float value *** @return The parsed IEEE 754 float value **/ public static float parseFloat(byte b[], int ofs, boolean isBigEndian, float dft) { /* valid byte array */ if ((b == null) || ((ofs + 4) > b.length)) { return dft; } /* parse IEEE 754 float */ int i = ofs; int floatInt = 0; if (isBigEndian) { floatInt = (((int)b[i+0] & 0xFF) << (3*8)) + (((int)b[i+1] & 0xFF) << (2*8)) + (((int)b[i+2] & 0xFF) << (1*8)) + ((int)b[i+3] & 0xFF); } else { floatInt = (((int)b[i+3] & 0xFF) << (3*8)) + (((int)b[i+2] & 0xFF) << (2*8)) + (((int)b[i+1] & 0xFF) << (1*8)) + ((int)b[i+0] & 0xFF); } return Float.intBitsToFloat(floatInt); } /** *** Parse the specified object into a float value *** @param data The object to parse *** @param dft The default float value if unable to parse the specified object *** @return The parsed float value **/ public static float parseFloat(Object data, float dft) { if (data == null) { return dft; } else if (data instanceof Number) { return ((Number)data).floatValue(); } else { return StringTools.parseFloat(data.toString(), dft); } } /** *** Parse the specified String into a float value *** @param data The String to parse *** @param dft The default float value if unable to parse the specified object *** @return The parsed float value **/ public static float parseFloat(String data, float dft) { return StringTools.parseFloat(new FilterNumber(data, Float.class), dft); } /** *** Parse the specified FilterNumber into a float value *** @param num The FilterNumber to parse *** @param dft The default float value if unable to parse the specified object *** @return The parsed float value **/ public static float parseFloat(FilterNumber num, float dft) { if ((num != null) && num.supportsType(Float.class)) { try { return Float.parseFloat(num.getValueString()); } catch (NumberFormatException nfe) { // ignore } } return dft; } /** *** Return true if the specified String contains a valid float value *** @param data The String to test *** @param strict True to test for a strict float value (ie. does not contain *** any other superfluous trailing characters), false to allow for *** other non-critical trailing characters. *** @return True if the specified String contains a valid float value **/ public static boolean isFloat(String data, boolean strict) { if (StringTools.isBlank(data)) { return false; } else { FilterNumber fn = new FilterNumber(data, Float.class); return fn.isValid(strict); } } // ------------------------------------------------------------------------ /** *** Parse the specified object into a long value *** @param data The object to parse *** @param dft The default long value if unable to parse the specified object *** @return The parsed long value **/ public static long parseLong(Object data, long dft) { if (data == null) { return dft; } else if (data instanceof Number) { return ((Number)data).longValue(); } else { return StringTools.parseLong(data.toString(), dft); } } /** *** Parse the specified String into a long value *** @param data The String to parse *** @param dft The default long value if unable to parse the specified object *** @return The parsed long value **/ public static long parseLong(String data, long dft) { return StringTools.parseLong(new FilterNumber(data, Long.class), dft); } /** *** Parse the specified FilterNumber into a long value *** @param num The FilterNumber to parse *** @param dft The default long value if unable to parse the specified object *** @return The parsed long value **/ public static long parseLong(FilterNumber num, long dft) { if ((num != null) && num.supportsType(Long.class)) { if (num.isHex()) { return StringTools.parseHexLong(num.getValueString(), dft); } else { try { return Long.parseLong(num.getValueString()); } catch (NumberFormatException nfe) { // Since 'FilterNumber' makes sure that only digits are parsed, // this likely means that the specified digit string is too large // for this required data type. Our last ditch effort is to // attempt to convert it to a BigInteger and extract the lower // number of bits to match our data type. BigInteger bigLong = parseBigInteger(num, null); if (bigLong != null) { return bigLong.longValue(); } } } } return dft; } /** *** Return true if the specified String contains a valid long value *** @param data The String to test *** @param strict True to test for a strict long value (ie. does not contain *** any other superfluous trailing characters), false to allow for *** other non-critical trailing characters. *** @return True if the specified String contains a valid long value **/ public static boolean isLong(String data, boolean strict) { if (StringTools.isBlank(data)) { return false; } else { FilterNumber fn = new FilterNumber(data, Long.class); return fn.isValid(strict); } } // ------------------------------------------------------------------------ /** *** Parse the specified object into a int value *** @param data The object to parse *** @param dft The default int value if unable to parse the specified object *** @return The parsed int value **/ public static int parseInt(Object data, int dft) { if (data == null) { return dft; } else if (data instanceof Number) { return ((Number)data).intValue(); } else { return StringTools.parseInt(data.toString(), dft); } } /** *** Parse the specified String into a int value *** @param data The String to parse *** @param dft The default int value if unable to parse the specified object *** @return The parsed int value **/ public static int parseInt(String data, int dft) { return StringTools.parseInt(new FilterNumber(data, Integer.class), dft); } /** *** Parse the specified FilterNumber into a int value *** @param num The FilterNumber to parse *** @param dft The default int value if unable to parse the specified object *** @return The parsed int value **/ public static int parseInt(FilterNumber num, int dft) { if ((num != null) && num.supportsType(Integer.class)) { if (num.isHex()) { return (int)StringTools.parseHexLong(num.getValueString(), dft); } else { try { return Integer.parseInt(num.getValueString()); } catch (NumberFormatException nfe) { // Since 'FilterNumber' makes sure that only digits are parsed, // this likely means that the specified digit string is too large // for this required data type. Our last ditch effort is to // attempt to convert it to a BigInteger and extract the lower // number of bits to match our data type.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -