Skip to content

Instantly share code, notes, and snippets.

@fedotxxl
Created April 12, 2016 10:45
Show Gist options
  • Save fedotxxl/8b8abf712b9dcf7e65b32314ecd82a15 to your computer and use it in GitHub Desktop.
Save fedotxxl/8b8abf712b9dcf7e65b32314ecd82a15 to your computer and use it in GitHub Desktop.
@Component
@Provider
public class SiteWithParamsConverterProvider implements ParamConverterProvider {
@Autowired
private Converter converter;
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if (genericType.equals(SiteWithParams.class)) {
return (ParamConverter<T>) converter;
}
return null;
}
@Component
public static class Converter implements ParamConverter<SiteWithParams> {
private Map<Site, SiteWithParams> siteWithParamsBySites;
@Autowired
public Converter(List<SiteWithParams> sitesWithParams) {
this.siteWithParamsBySites = sitesWithParams
.stream()
.collect(Collectors.toMap(SiteWithParams::getSite, item -> item));
}
@Override
public SiteWithParams fromString(String siteCode) {
Site site = (siteCode == null) ? null : Site.getSiteByCode(siteCode.toLowerCase());
SiteWithParams siteWithParams = (site == null) ? null : siteWithParamsBySites.get(site);
if (siteWithParams == null) {
throw new SiteWithParamsBuilder.UnknownSiteException(siteCode);
}
return siteWithParams;
}
@Override
public String toString(SiteWithParams value) {
return value.getSite().getCode();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment