Skip to content

Instantly share code, notes, and snippets.

@adumeige
Created February 17, 2015 11:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save adumeige/ae945724041964a5523e to your computer and use it in GitHub Desktop.
package com.psyclik;
import java.util.Locale;
import java.util.function.Function;
import com.vaadin.data.util.converter.Converter;
/**
* Generic converter class allowing the use of lambdas instead of having to
* redefine a full class for every converter. User needs to provide both the
* presentation and model concrete classes as well as the converter and reverse
* converter lambda at instantiation time.
*
* @author Antoine.
*
* @param <P>
* The presentation class.
* @param <M>
* The model class.
*/
public class LambdaConverter<P, M> implements Converter<P, M> {
/**
* Serialization ID.
*/
private static final long serialVersionUID = 1L;
/**
* The class to use as the presentation class.
*/
public final Class<P> presentationClass;
/**
* The class to use as the model class.
*/
public final Class<M> modelClass;
/**
* The converter lambda, converting from presentation to model.
*/
public final Function<P, M> presentationToModelLambda;
/**
* The reverse converter lambda, converting from model to presentation.
*/
public final Function<M, P> modelToPresentationLambda;
/**
* Standard constructor, no fancy stuff.
*
* @param presentationClass
* The presentation class to set.
* @param modelClass
* The model class to set.
* @param presentationToModelLambda
* The presentation to model lambda to set.
* @param modelToPresentationLambda
* The model to presentation lambda to set.
*/
public LambdaConverter(Class<P> presentationClass, Class<M> modelClass,
Function<P, M> presentationToModelLambda,
Function<M, P> modelToPresentationLambda) {
super();
this.presentationClass = presentationClass;
this.modelClass = modelClass;
this.presentationToModelLambda = presentationToModelLambda;
this.modelToPresentationLambda = modelToPresentationLambda;
}
@Override
public M convertToModel(P value, Class<? extends M> targetType,
Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
return presentationToModelLambda.apply(value);
}
@Override
public P convertToPresentation(M value, Class<? extends P> targetType,
Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
return modelToPresentationLambda.apply(value);
}
@Override
public Class<M> getModelType() {
return modelClass;
}
@Override
public Class<P> getPresentationType() {
return presentationClass;
}
}
@Koziolek
Copy link

http://koziolekweb.pl/2014/11/22/typy-nieprymitywne-w-tabelach-vaadin/ listings 5 and 6. Please look at publication date....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment