private static <S, T> void add(Configuration<CoercionTuple> configuration, Class<S> sourceType, Class<T> targetType, Coercion<S, T> coercion) { configuration.add(CoercionTuple.create(sourceType, targetType, coercion)); }
/** * Convenience constructor to help with generics. * * @since 5.2.0 */ public static <S, T> CoercionTuple<S, T> create(Class<S> sourceType, Class<T> targetType, Coercion<S, T> coercion) { return new CoercionTuple<S, T>(sourceType, targetType, coercion); }
@Override public String toString() { return coercion.toString(); }
/** * Coercion from null is special; we match based on the target type and its not a spanning * search. In many cases, we * return a pass-thru that leaves the value as null. * * @param targetType * desired type * @return the coercion */ private Coercion searchForNullCoercion(Class targetType) { List<CoercionTuple> tuples = getTuples(Void.class, targetType); for (CoercionTuple tuple : tuples) { Class tupleTargetType = tuple.getTargetType(); if (targetType.equals(tupleTargetType)) return tuple.getCoercion(); } // Typical case: no match, this coercion passes the null through // as null. return COERCION_NULL_TO_OBJECT; }
Set<CoercionTuple> consideredTuples, LinkedList<CoercionTuple> queue) Class intermediateType = intermediateTuple.getTargetType(); Class newIntermediateType = tuple.getTargetType(); Coercion compoundCoercer = new CompoundCoercion(intermediateTuple.getCoercion(), tuple.getCoercion()); CoercionTuple compoundTuple = new CoercionTuple(sourceType, newIntermediateType, compoundCoercer, false);
public TypeCoercerImpl(Collection<CoercionTuple> tuples) { for (CoercionTuple tuple : tuples) { Class key = tuple.getSourceType(); InternalCommonsUtils.addToMapList(sourceTypeToTuple, key, tuple); } }
Class tupleTargetType = tuple.getTargetType(); return tuple.getCoercion();
/** * <ul> * <li>{@link String} to {@link org.apache.tapestry5.json.JSONObject}</li> * <li>{@link String} to {@link org.apache.tapestry5.json.JSONArray}</li> * </ul> */ @Contribute(TypeCoercer.class) public static void provideCoercions(Configuration<CoercionTuple> configuration) { configuration.add(CoercionTuple.create(String.class, JSONObject.class, new StringToJSONObject())); configuration.add(CoercionTuple.create(String.class, JSONArray.class, new StringToJSONArray())); } }
configuration.add(new CoercionTuple(String.class, WriteConcern.class, new Coercion<String, WriteConcern>() { public WriteConcern coerce(String input) configuration.add(new CoercionTuple(String.class, ReadPreference.class, new Coercion<String, ReadPreference>() { public ReadPreference coerce(String input)
/** * <ul> * <li>{@link String} to {@link org.apache.tapestry5.json.JSONObject}</li> * <li>{@link String} to {@link org.apache.tapestry5.json.JSONArray}</li> * </ul> */ @Contribute(TypeCoercer.class) public static void provideCoercions(Configuration<CoercionTuple> configuration) { configuration.add(CoercionTuple.create(String.class, JSONObject.class, new StringToJSONObject())); configuration.add(CoercionTuple.create(String.class, JSONArray.class, new StringToJSONArray())); } }
tuples = extend(tuples, new CoercionTuple(sourceType, targetType, new StringToEnumCoercion(targetType))); tuples = extend(tuples, new CoercionTuple(sourceType, targetType, (value)->((Enum) value).name()));
configuration.add(CoercionTuple.create(ComponentResources.class, PropertyOverrides.class, new Coercion<ComponentResources, PropertyOverrides>() configuration.add(CoercionTuple.create(String.class, SelectModel.class, new Coercion<String, SelectModel>() configuration.add(CoercionTuple.create(Map.class, SelectModel.class, new Coercion<Map, SelectModel>() configuration.add(CoercionTuple.create(Collection.class, GridDataSource.class, new Coercion<Collection, GridDataSource>() configuration.add(CoercionTuple.create(void.class, GridDataSource.class, new Coercion<Void, GridDataSource>() configuration.add(CoercionTuple.create(List.class, SelectModel.class, new Coercion<List, SelectModel>() configuration.add(CoercionTuple.create(String.class, Pattern.class, new Coercion<String, Pattern>() configuration.add(CoercionTuple.create(ComponentResourcesAware.class, ComponentResources.class, new Coercion<ComponentResourcesAware, ComponentResources>() configuration.add(CoercionTuple.create(String.class, Renderable.class, new Coercion<String, Renderable>() configuration.add(CoercionTuple.create(Renderable.class, Block.class, new Coercion<Renderable, Block>() configuration.add(CoercionTuple.create(String.class, DateFormat.class, new Coercion<String, DateFormat>() configuration.add(CoercionTuple.create(String.class, Resource.class, new Coercion<String, Resource>() configuration.add(CoercionTuple.create(Renderable.class, RenderCommand.class, new Coercion<Renderable, RenderCommand>()
CoercionTuple<Stooge, String> stoogeToString = CoercionTuple.create(Stooge.class, String.class, new Coercion<Stooge, String>(){ @Override public String coerce(Stooge input) { CoercionTuple<String, Stooge> stringToStooge = CoercionTuple.create(String.class, Stooge.class, new Coercion<String, Stooge>(){