List of usage examples for java.text ParsePosition getErrorIndex
public int getErrorIndex()
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to an int primitive. * //from w ww . j a va2s .com * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if * null) * @return the converted Integer value. */ public static Integer formatInt(String value, Locale locale) { Integer result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Integer.MIN_VALUE && num.doubleValue() <= Integer.MAX_VALUE) { result = new Integer(num.intValue()); } } } return result; }
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a long primitive. * //from www.j av a 2s.c om * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if * null) * @return the converted Long value. */ public static Long formatLong(String value, Locale locale) { Long result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Long.MIN_VALUE && num.doubleValue() <= Long.MAX_VALUE) { result = new Long(num.longValue()); } } } return result; }
From source file:com.flexive.shared.FxFormatUtils.java
/** * Convert a String to Integer/*w w w .ja v a 2 s . c om*/ * * @param value value to convert * @return Integer */ public static Integer toInteger(String value) { if (StringUtils.isBlank(value)) return null; final ParsePosition pos = new ParsePosition(0); final String _value = value.trim(); try { final Number parse = FxValueRendererFactory.getNumberFormatInstance().parse(unquote(_value), pos); if (pos.getErrorIndex() >= 0 || pos.getIndex() != _value.length() || parse.doubleValue() != (double) parse.intValue() /*truncation*/) throw new FxConversionException("ex.conversion.value.error", FxNumber.class.getCanonicalName(), value, "Failed to parse " + value).asRuntimeException(); return parse.intValue(); } catch (NumberFormatException e) { throw new FxConversionException("ex.conversion.value.error", FxLargeNumber.class.getCanonicalName(), value, "Failed to parse [" + value + "] using dec.sep. [" + FxContext.get().getDecimalSeparator() + "] and grouping sep. [" + FxContext.get().getDecimalSeparator() + "]") .asRuntimeException(); } }
From source file:com.flexive.shared.FxFormatUtils.java
/** * Convert a String to Long// w w w .j ava 2 s .c om * * @param value value to convert * @return Long */ public static Long toLong(String value) { if (StringUtils.isBlank(value)) return null; final ParsePosition pos = new ParsePosition(0); final String _value = value.trim(); try { final Number parse = FxValueRendererFactory.getNumberFormatInstance().parse(unquote(_value), pos); if (pos.getErrorIndex() >= 0 || pos.getIndex() != _value.length() || parse.doubleValue() != (double) parse.longValue() /*truncation*/) throw new FxConversionException("ex.conversion.value.error", FxLargeNumber.class.getCanonicalName(), value, "Failed to parse " + value).asRuntimeException(); return parse.longValue(); } catch (NumberFormatException e) { throw new FxConversionException("ex.conversion.value.error", FxLargeNumber.class.getCanonicalName(), value, "Failed to parse [" + value + "] using dec.sep. [" + FxContext.get().getDecimalSeparator() + "] and grouping sep. [" + FxContext.get().getDecimalSeparator() + "]") .asRuntimeException(); } }
From source file:com.flexive.shared.FxFormatUtils.java
/** * Convert a String to Float//from ww w .j ava 2 s . c o m * * @param value value to convert * @return Float */ public static Float toFloat(String value) { if (StringUtils.isBlank(value)) return null; final ParsePosition pos = new ParsePosition(0); final String _value = value.trim(); try { Float res = FxValueRendererFactory.getNumberFormatInstance().parse(unquote(_value), pos).floatValue(); if (pos.getErrorIndex() >= 0 || pos.getIndex() != _value.length()) throw new FxConversionException("ex.conversion.value.error", FxFloat.class.getCanonicalName(), value, "Failed to parse " + value).asRuntimeException(); return res; } catch (NumberFormatException e) { throw new FxConversionException("ex.conversion.value.error", FxLargeNumber.class.getCanonicalName(), value, "Failed to parse [" + value + "] using dec.sep. [" + FxContext.get().getDecimalSeparator() + "] and grouping sep. [" + FxContext.get().getDecimalSeparator() + "]") .asRuntimeException(); } }
From source file:com.flexive.shared.FxFormatUtils.java
/** * Convert a String to Double/*from w w w. j a v a 2s. co m*/ * * @param value value to convert * @return Double */ public static Double toDouble(String value) { if (StringUtils.isBlank(value)) return null; final ParsePosition pos = new ParsePosition(0); final String _value = value.trim(); try { Double res = FxValueRendererFactory.getNumberFormatInstance().parse(unquote(_value), pos).doubleValue(); if (pos.getErrorIndex() >= 0 || pos.getIndex() != _value.length()) throw new FxConversionException("ex.conversion.value.error", FxDouble.class.getCanonicalName(), value, "Failed to parse " + value).asRuntimeException(); return res; } catch (NumberFormatException e) { throw new FxConversionException("ex.conversion.value.error", FxLargeNumber.class.getCanonicalName(), value, "Failed to parse [" + value + "] using dec.sep. [" + FxContext.get().getDecimalSeparator() + "] and grouping sep. [" + FxContext.get().getDecimalSeparator() + "]") .asRuntimeException(); } }
From source file:com.anrisoftware.globalpom.format.latlong.LatitudeFormatLogger.java
ParseException errorParseLatitude(String source, ParsePosition pos) { ParseException ex = new ParseException(format(parse_error.toString(), source), pos.getErrorIndex()); return logException(ex, parse_error_message, source); }
From source file:com.haulmont.chile.core.datatypes.impl.NumberDatatype.java
protected Number parse(String value, NumberFormat format) throws ParseException { ParsePosition pos = new ParsePosition(0); Number res = format.parse(value.trim(), pos); if (pos.getIndex() != value.length()) { throw new ParseException(String.format("Unparseable number: \"%s\"", value), pos.getErrorIndex()); }//from ww w . j av a 2 s.c o m return res; }
From source file:com.anrisoftware.globalpom.format.latlong.LongitudeFormatLogger.java
void checkLongitude(String source, ParsePosition pos) throws ParseException { if (WORD_PATTERN.matcher(source).matches() && !containsAny(source, EAST, WEST)) { ParseException ex = new ParseException(format(longitude_error.toString(), source), pos.getErrorIndex()); throw logException(ex, longitude_error_message, source); }/* w w w . j a v a 2s . co m*/ }
From source file:com.anrisoftware.globalpom.format.latlong.LatitudeFormatLogger.java
void checkLatitude(String source, ParsePosition pos) throws ParseException { if (WORD_PATTERN.matcher(source).matches() && !containsAny(source, SOUTH, NORTH)) { ParseException ex = new ParseException(format(latitude_error.toString(), source), pos.getErrorIndex()); throw logException(ex, latitude_error_message, source); }/* w ww.j a v a 2 s . c o m*/ }