crossconverters.java

来自「derby database source code.good for you.」· Java 代码 · 共 1,410 行 · 第 1/4 页

JAVA
1,410
字号
        return source ? (long) 1 : (long) 0;    }    final long getLongFromString(String source) throws SqlException {        try {            return parseLong(source);        } catch (java.lang.NumberFormatException e) {            throw new SqlException(agent_.logWriter_, e, "Invalid data conversion:" +                    " Parameter instance " + source +                    " is invalid for requested conversion to " + "long.");        }    }    //---------------------------- getFloat*() methods ---------------------------    final float getFloatFromDouble(double source) throws SqlException {        if (Configuration.rangeCheckCrossConverters &&                Float.isInfinite((float)source)) {            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));        }        return (float) source;    }    final float getFloatFromBigDecimal(java.math.BigDecimal source) throws SqlException {        if (Configuration.rangeCheckCrossConverters &&                (source.compareTo(bdMaxFloatValue__) == 1 || source.compareTo(bdMinFloatValue__) == -1)) {            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));        }        return source.floatValue();    }    final float getFloatFromBoolean(boolean source) throws SqlException {        return source ? (float) 1 : (float) 0;    }    final float getFloatFromString(String source) throws SqlException {        try {            return Float.parseFloat(source.trim());        } catch (java.lang.NumberFormatException e) {            throw new SqlException(agent_.logWriter_, e, "Invalid data conversion:" +                    " Parameter instance " + source +                    " is invalid for requested conversion to " + "float.");        }    }    //---------------------------- getDouble*() methods --------------------------    final double getDoubleFromBigDecimal(java.math.BigDecimal source) throws SqlException {        if (Configuration.rangeCheckCrossConverters &&                (source.compareTo(bdMaxDoubleValue__) == 1 || source.compareTo(bdMinDoubleValue__) == -1)) {            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));        }        return source.doubleValue();    }    final double getDoubleFromBoolean(boolean source) throws SqlException {        return source ? (double) 1 : (double) 0;    }    final double getDoubleFromString(String source) throws SqlException {        try {            return Double.parseDouble(source.trim());        } catch (java.lang.NumberFormatException e) {            throw new SqlException(agent_.logWriter_, e, "Invalid data conversion:" +                    " Parameter instance " + source +                    " is invalid for requested conversion to " + "double.");        }    }    //---------------------------- getBigDecimal*() methods ----------------------    final java.math.BigDecimal getBigDecimalFromBoolean(boolean source) throws SqlException {        return source ? bdOne__ : bdZero__;    }    final java.math.BigDecimal getBigDecimalFromString(String source) throws SqlException {        try {            // Unfortunately, the big decimal constructor calls java.lang.Long.parseLong(),            // which doesn't like spaces, so we have to call trim() to get rid of the spaces from CHAR columns.            return new java.math.BigDecimal(source.trim());        } catch (java.lang.NumberFormatException e) {            throw new SqlException(agent_.logWriter_, e, "Invalid data conversion:" +                    " Parameter instance " + source +                    " is invalid for requested conversion to " + "java.math.BigDecimal.");        }    }    //---------------------------- getString*() methods --------------------------    final String getStringFromBoolean(boolean source) throws SqlException {        return source ? "1" : "0";    }    final String getStringFromBytes(byte[] bytes) throws SqlException {        StringBuffer stringBuffer = new StringBuffer(bytes.length * 2);        for (int i = 0; i < bytes.length; i++) {            String hexForByte = Integer.toHexString(bytes[i] & 0xff);            // If the byte is x0-F, prepend a "0" in front to ensure 2 char representation            if (hexForByte.length() == 1) {                stringBuffer.append('0');            }            stringBuffer.append(hexForByte);        }        return stringBuffer.toString();    }    // All Numeric, and Date/Time types use String.valueOf (source)    //---------------------------- getDate*() methods ----------------------------    final java.sql.Date getDateFromString(String source) throws SqlException {        try {            return date_valueOf(source);        } catch (java.lang.IllegalArgumentException e) { // subsumes NumberFormatException            throw new SqlException(agent_.logWriter_, e, "Invalid data conversion:" +                    " Parameter instance " + source +                    " is invalid for requested conversion to " + "java.sql.Date.");        }    }    final java.sql.Date getDateFromTime(java.sql.Time source) throws SqlException {        return new java.sql.Date(source.getTime());    }    final java.sql.Date getDateFromTimestamp(java.sql.Timestamp source) throws SqlException {        return new java.sql.Date(source.getTime());    }    //---------------------------- getTime*() methods ----------------------------    final java.sql.Time getTimeFromString(String source) throws SqlException {        try {            return time_valueOf(source);        } catch (java.lang.IllegalArgumentException e) { // subsumes NumberFormatException            throw new SqlException(agent_.logWriter_, e, "Invalid data conversion:" +                    " Parameter instance " + source +                    " is invalid for requested conversion to " + "java.sql.Time.");        }    }    final java.sql.Time getTimeFromTimestamp(java.sql.Timestamp source) throws SqlException {        return new java.sql.Time(source.getTime());    }    //---------------------------- getTimestamp*() methods -----------------------    final java.sql.Timestamp getTimestampFromString(String source) throws SqlException {        try {            return timestamp_valueOf(source);        } catch (java.lang.IllegalArgumentException e) {  // subsumes NumberFormatException            throw new SqlException(agent_.logWriter_, e,                    "Invalid data conversion:" +                    " Parameter instance " + source +                    " is invalid for requested conversion to " + "java.sql.Timestamp.");        }    }    final java.sql.Timestamp getTimestampFromTime(java.sql.Time source) throws SqlException {        return new java.sql.Timestamp(source.getTime());    }    final java.sql.Timestamp getTimestampFromDate(java.sql.Date source) throws SqlException {        return new java.sql.Timestamp(source.getTime());    }    final java.sql.Date date_valueOf(String s) throws java.lang.IllegalArgumentException {        String formatError = "JDBC Date format must be yyyy-mm-dd";        if (s == null) {            throw new java.lang.IllegalArgumentException(formatError);        }        s = s.trim();        return java.sql.Date.valueOf(s);    }    final java.sql.Time time_valueOf(String s) throws java.lang.IllegalArgumentException, NumberFormatException {        String formatError = "JDBC Time format must be hh:mm:ss";        if (s == null) {            throw new java.lang.IllegalArgumentException();        }        s = s.trim();        return java.sql.Time.valueOf(s);    }    final java.sql.Timestamp timestamp_valueOf(String s) throws java.lang.IllegalArgumentException, NumberFormatException {        String formatError = "JDBC Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff";        if (s == null) {            throw new java.lang.IllegalArgumentException();        }        s = s.trim();        return java.sql.Timestamp.valueOf(s);    }    private final byte parseByte(String s) throws NumberFormatException {        int i = parseInt(s);        if (i < Byte.MIN_VALUE || i > Byte.MAX_VALUE) {            throw new NumberFormatException();        }        return (byte) i;    }    private final short parseShort(String s) throws NumberFormatException {        int i = parseInt(s);        if (i < Short.MIN_VALUE || i > Short.MAX_VALUE) {            throw new NumberFormatException();        }        return (short) i;    }    // Custom version of java.lang.parseInt() that allows for space padding of char fields.    private final int parseInt(String s) throws NumberFormatException {        if (s == null) {            throw new NumberFormatException("null");        }        int result = 0;        boolean negative = false;        int i = 0;        int max = s.length();        int limit;        int multmin;        int digit;        if (max == 0) {            throw new NumberFormatException(s);        }        if (s.charAt(0) == '-') {            negative = true;            limit = Integer.MIN_VALUE;            i++;        } else {            limit = -Integer.MAX_VALUE;        }        multmin = limit / 10;        // Special handle the first digit to get things started.        if (i < max) {            digit = Character.digit(s.charAt(i++), 10);            if (digit < 0) {                throw new NumberFormatException(s);            } else {                result = -digit;            }        }        // Now handle all the subsequent digits or space padding.        while (i < max) {            char c = s.charAt(i++);            if (c == ' ') {                skipPadding(s, i, max);                break;            }            // Accumulating negatively avoids surprises near MAX_VALUE            digit = Character.digit(c, 10);            if (digit < 0) {                throw new NumberFormatException(s);            }            if (result < multmin) {                throw new NumberFormatException(s);            }            result *= 10;            if (result < limit + digit) {                throw new NumberFormatException(s);            }            result -= digit;        }        if (negative) {            if (i > 1) {                return result;            } else { // Only got "-"                throw new NumberFormatException(s);            }        } else {            return -result;        }    }    private final long parseLong(String s) throws NumberFormatException {        if (s == null) {            throw new NumberFormatException("null");        }        long result = 0;        boolean negative = false;        int i = 0, max = s.length();        long limit;        long multmin;        int digit;        if (max == 0) {            throw new NumberFormatException(s);        }        if (s.charAt(0) == '-') {            negative = true;            limit = Long.MIN_VALUE;            i++;        } else {            limit = -Long.MAX_VALUE;        }        multmin = limit / 10;        if (i < max) {            digit = Character.digit(s.charAt(i++), 10);            if (digit < 0) {                throw new NumberFormatException(s);            } else {                result = -digit;            }        }        while (i < max) {            char c = s.charAt(i++);            if (c == ' ') {                skipPadding(s, i, max);                break;            }            // Accumulating negatively avoids surprises near MAX_VALUE            digit = Character.digit(c, 10);            if (digit < 0) {                throw new NumberFormatException(s);            }            if (result < multmin) {                throw new NumberFormatException(s);            }            result *= 10;            if (result < limit + digit) {                throw new NumberFormatException(s);            }            result -= digit;        }        if (negative) {            if (i > 1) {                return result;            } else {	// Only got "-"                throw new NumberFormatException(s);            }        } else {            return -result;        }    }    private final void skipPadding(String s, int i, int length) throws NumberFormatException {        while (i < length) {            if (s.charAt(i++) != ' ') {                throw new NumberFormatException(s);            }        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?