I wanted to create an extension of java.text.Format but I do not know what to do with the Format.parseObject method. The javadoc states
Parses text from the beginning of the given string to produce an object. The method may not use the entire text of the given string.
So if you want to implement this method, how do you handle the situation where you can parse the whole input string ? Do you throw an exception or do you return the parsed value (or something else ) ? Note that the javadoc for the exception states that a ParseException is thrown
if the beginning of the specified string cannot be parsed.
IMO, in case where I can parse the whole String, I certainly can parse the beginning so no reason to throw an exception. Looking at the implementation of this method in the JDK, they do not seem to respect their own documentation
public Object parseObject(String source) throws ParseException {
ParsePosition pos = new ParsePosition(0);
Object result = parseObject(source, pos);
if (pos.index == 0) {
throw new ParseException("Format.parseObject(String) failed",
pos.errorIndex);
}
return result;
}
“The method may not use the entire text of the given string.”
You should interpret this as “may or may not…”
BTW – There are a bunch of built-in Formatters already in java. Aside from NumberFormat & DecimalFormat which are for numbers, the one I like the most (haven’t used in probably 10 yrs – just haven’t had a reason to use it) is
java.text.MessageFormat. Go have a look at that and see if it meet your needs