/** * Return the first character from given string. * * @throws ConverterException if given string has more than one single character. */ @Override public <T> T asObject(String string, Class<T> valueType) throws BugError { // at this point value type is guaranteed to be char or Character if (string.length() > 1) { throw new ConverterException("Trying to convert a larger string into a single character."); } return (T) (Character) string.charAt(0); }
/** * Convert URL string representation into URL instance. * * @throws ConverterException if given string is not a valid URL. */ @Override public <T> T asObject(String string, Class<T> valueType) throws ConverterException { // at this point value type is guaranteed to be URL try { return (T) new URL(string); } catch (MalformedURLException e) { throw new ConverterException(e.getMessage()); } }
@Override public String asString(Object object) { if(object == null) { return null; } if(object instanceof String) { return (String)object; } Converter converter = getConverter(object.getClass()); if(converter == null) { throw new ConverterException("No registered converter for |%s|.", object.getClass()); } try { return converter.asString(object); } catch(ConverterException e) { throw e; } catch(Throwable t) { throw new ConverterException(t); } }
Date date = df.parse(string, parsePosition); if (date == null) { throw new ConverterException("Cannot parse ISO8601 date from |%s| at position |%d|.", string, parsePosition.getErrorIndex());
@SuppressWarnings("unchecked") @Override public <T> T asObject(String string, Class<T> valueType) { Params.notNull(valueType, "Value type"); if(string == null) { return null; } if(valueType == Object.class) { return (T)string; } if(valueType == String.class) { return (T)string; } Converter converter = getConverter(valueType); if(converter == null) { throw new ConverterException("No registered converter for |%s|.", valueType); } try { return converter.asObject(string, valueType); } catch(ConverterException e) { throw e; } catch(Throwable t) { throw new ConverterException(t); } }