@SuppressWarnings("unchecked") private Object toObject(Class<?> type, String pattern) { if (type == String.class) { return toString(); return toBoolean(); return toByte(); return toShort(); return toInt(); return toLong(); return toFloat(); return toDouble(); return toCharacter(); return toBigDecimal(); return toUUID(); return toEnum((Class<? extends Enum>) type); return toSqlDate(); return toSqlTime();
public Object objectFromString(String value, Class<?> objectClass) throws ParseException { if (value == null) { return null; } else if (objectClass.isAssignableFrom(value.getClass())) { return value; } // re-use the infinitely useful ParameterValue class ParameterValue pv = new ParameterValue(value); if (objectClass.isEnum()) { return pv.toEnum((Class) objectClass); } else if (java.sql.Date.class.isAssignableFrom(objectClass)) { return pv.toSqlDate(); } else if (java.sql.Time.class.isAssignableFrom(objectClass)) { return pv.toSqlTime(); } else if (java.sql.Timestamp.class.isAssignableFrom(objectClass)) { return pv.toSqlTimestamp(); } else if (Date.class.isAssignableFrom(objectClass)) { return pv.toDate(datePattern); } return pv.to(objectClass); }
/** * Converts a string value(s) to a Set of the target type. You may optionally specify * a string pattern to assist in the type conversion. * * @param classOfT * @param pattern optional pattern for interpreting the underlying request * string value. (used for date & time conversions) * @return a set of the values */ public <T> Set<T> toSet(Class<T> classOfT, String pattern) { return (Set<T>) toCollection(HashSet.class, classOfT, pattern); }
public float toFloat(float defaultValue) { if (isNull() || StringUtils.isNullOrEmpty(values[0])) { return defaultValue; } try { Number number = getDecimalFormat().parse(values[0]); return number.floatValue(); } catch (ParseException e) { throw new PippoRuntimeException(e, "Failed to parse '{}'", values[0]); } }
@Override public String toString() { return toString(null); }
@Override public Object extract(Context context) { ParameterValue pv = context.getParameter(name); if (suffixes != null) { // automatically strip the suffix, if we can String value = pv.toString(); if (value != null) { String ext = Files.getFileExtension(value); if (!Strings.isNullOrEmpty(ext) && suffixes.contains(ext.toLowerCase())) { pv = new ParameterValue(Files.getNameWithoutExtension(value)); } } } if (collectionType == null) { Object o = pv.to(objectType, pattern); return o; } Object o = pv.toCollection(collectionType, objectType, pattern); return o; } }
@Override @SuppressWarnings("unchecked") public Object extract(MethodParameter parameter, RouteContext routeContext) { Header annotation = parameter.getAnnotation(Header.class); String name = getParameterName(parameter, annotation); String pattern = annotation.pattern(); ParameterValue parameterValue = new ParameterValue(routeContext.getRequest().getLocale(), routeContext.getHeader(name)); Class<?> parameterType = parameter.getParameterType(); if (Collection.class.isAssignableFrom(parameterType)) { Class<? extends Collection> collectionType = (Class<? extends Collection>) parameterType; Class<?> objectType = parameter.getParameterGenericType(); return parameterValue.toCollection(collectionType, objectType, pattern); } return parameterValue.to(parameterType, pattern); }
@Override public void setObjectType(Class<?> objectType) { ParameterValue testValue = new ParameterValue(); testValue.to(objectType); this.objectType = objectType; }
value = getParameters().get(parameterName).toSet(genericClass, pattern); } else if (List.class == fieldClass) { value = getParameters().get(parameterName).toList(genericClass, pattern); } else if (fieldClass.isInterface()) { throw new PippoRuntimeException("Field '{}' collection '{}' is not a supported type!", } else { Class<? extends Collection> collectionClass = (Class<? extends Collection>) fieldClass; value = getParameters().get(parameterName).toCollection(collectionClass, genericClass, pattern); value = getParameters().get(parameterName).to(fieldClass, pattern); value = getParameters().get(parameterName).convert(converterClass, pattern);
String contentType = new ParameterValue(context.getRequest().getLocale(), context.getHeader("Content-Type")).toString("").toLowerCase(); contentType = StringUtils.getPrefix(contentType, ';').trim(); if (!guardedTypes.contains(contentType)) { requestToken = context.getParameter(TOKEN).toString();
@Override @SuppressWarnings("unchecked") public Object extract(MethodParameter parameter, RouteContext routeContext) { Param annotation = parameter.getAnnotation(Param.class); String name = getParameterName(parameter, annotation); String pattern = annotation.pattern(); ParameterValue parameterValue = routeContext.getParameter(name); Class<?> parameterType = parameter.getParameterType(); if (Collection.class.isAssignableFrom(parameterType)) { Class<? extends Collection> collectionType = (Class<? extends Collection>) parameterType; Class<?> objectType = parameter.getParameterGenericType(); return parameterValue.toCollection(collectionType, objectType, pattern); } return parameterValue.to(parameterType, pattern); }
private ParameterValue newParameterValuePlaceHolder() { return new ParameterValue(locale, "PLACEHOLDER"); }
public String toString(String defaultValue) { if (isNull()) { return defaultValue; } return values[0]; }
public boolean toBoolean() { return toBoolean(false); }
public int toInt() { return toInt(0); }
public <T> T to(Class<T> classOfT) { return to(classOfT, null); }
public <T extends Enum<?>> T toEnum(Class<T> classOfT) { return toEnum(classOfT, null, true); }
public Date toDate(String pattern) { return toDate(null, pattern); }