public class LocaleParamConverter extends Object implements ParamConverter
ParamConverter.Context
Constructor and Description |
---|
LocaleParamConverter() |
Modifier and Type | Method and Description |
---|---|
Object |
convert(com.google.inject.TypeLiteral<?> toType,
Object[] values,
ParamConverter.Context ctx)
Convert one or more values to the required type.
|
public Object convert(com.google.inject.TypeLiteral<?> toType, Object[] values, ParamConverter.Context ctx) throws Exception
ParamConverter
Convert one or more values to the required type. If the converter doesn't support the
required type a call to ParamConverter.Context.convert(TypeLiteral, Object[])
must be done.
ParamConverter converter = (type, values, next) -> { if (type.getRawType() == MyType.class) { // convert to MyType return ...; } // no luck! move next return next.convert(type, values); }It's also possible to create generic/parameterized types too:
public class MyType<T> {} ParamConverter converter = (type, values, ctx) -> { if (type.getRawType() == MyType.class) { // Creates a new type from current generic type TypeLiterale<?> paramType = TypeLiteral .get(((ParameterizedType) toType.getType()).getActualTypeArguments()[0]); // Ask param converter to resolve the new type. Object result = next.convert(paramType, values); return new MyType(result); } // no luck! move next return ctx.convert(type, values); }
convert
in interface ParamConverter
toType
- Requested type.values
- Raw values. Types is one of two: String
or Upload
.ctx
- Execution context.Exception
- If conversion fails.Copyright © 2015. All rights reserved.