Created
April 12, 2016 10:45
-
-
Save fedotxxl/8b8abf712b9dcf7e65b32314ecd82a15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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