CastUtils { public static long parseInRange(String st, long max, long min, TExecutionContext context) { Object truncated; try { return CastUtils.getInRange(max, min, Long.parseLong(st), context); } catch (NumberFormatException e) { truncated = CastUtils.truncateNonDigits(st, context); } if (truncated instanceof String) { String truncatedStr = (String)truncated; try { return CastUtils.getInRange(max, min, Long.parseLong(truncatedStr), context); } catch (NumberFormatException e) { context.reportOverflow(e.getMessage()); char first = truncatedStr.charAt(0); if (first == '-') return getInRange(max, min, Long.MIN_VALUE, context); else return getInRange(max, min, Long.MAX_VALUE, context); } } else { BigDecimal num = (BigDecimal)truncated; if (num.compareTo(MAX_LONG) > 0) { context.reportTruncate(st, Long.toString(max)); return max; } else if (num.compareTo(MIN_LONG) < 0) { context.reportTruncate(st, Long.toString(min)); return min; } try { return getInRange(max, min, num.longValueExact(), context); } catch (ArithmeticException e) { long ret = num.setScale(0, RoundingMode.HALF_UP).longValue(); context.reportTruncate(st, Long.toString(ret)); return getInRange(max, min, ret, context); } } }  static long round(long max, long min, double val, TExecutionContext context); static long getInRange(long max, long min, long val, TExecutionContext context); static long getInRange(long max, long min, long val, long defaultVal, TExecutionContext context); static String getNum(int scale, int precision); static void doCastDecimal(TExecutionContext context,
                            BigDecimalWrapper num,
                            ValueTarget out); static BigDecimalWrapperImpl parseDecimalString(String st, TExecutionContext context); static double parseDoubleString(String st, TExecutionContext context); static long parseInRange(String st, long max, long min, TExecutionContext context); static long parseUnsignedLong(String st, TExecutionContext context); static Object truncateNonDigits(String st, TExecutionContext context); static String truncateNonDigitPlainString(String st, TExecutionContext context); static short adjustYear(long raw, TExecutionContext context); static final long MAX_TINYINT; static final long MAX_UNSIGNED_TINYINT; static final long MIN_TINYINT; static final long MAX_SMALLINT; static final long MAX_UNSIGNED_SMALLINT; static final long MIN_SMALLINT; static final long MAX_MEDINT; static final long MAX_UNSIGNED_MEDINT; static final long MIN_MEDINT; static final long MAX_INT; static final long MAX_UNSIGNED_INT; static final long MIN_INT; static final long MAX_BIGINT; static final long MIN_BIGINT; }
@Test public void testTruncate() { TExecutionContext context = new TExecutionContext(null, null, null, null, ErrorHandlingMode.IGNORE, ErrorHandlingMode.IGNORE, ErrorHandlingMode.IGNORE); long actual = CastUtils.parseInRange(input, Long.MAX_VALUE, Long.MIN_VALUE, context); assertEquals(input, expected, actual); }