Guest User

Untitled

a guest
Apr 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package net.bjornoy.converter;
  2.  
  3. import org.springframework.beans.factory.InitializingBean;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.core.convert.TypeDescriptor;
  6. import org.springframework.core.convert.converter.Converter;
  7. import org.springframework.core.convert.support.ConfigurableConversionService;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. @SuppressWarnings({"WeakerAccess", "unused", "unchecked"})
  13. public abstract class AbstractConverter<S, T> implements Converter<S, T>, InitializingBean {
  14.  
  15. protected ConfigurableConversionService conversionService;
  16.  
  17. @Override
  18. public void afterPropertiesSet() {
  19. conversionService.addConverter(this);
  20. }
  21.  
  22. @Autowired
  23. public void setConversionService(ConfigurableConversionService conversionService) {
  24. this.conversionService = conversionService;
  25. }
  26.  
  27. protected <S1, T2> List<T2> convertList(List<S1> sourceList, Class<S1> sourceClass, Class<T2> targetClass) {
  28. if (sourceList == null) {
  29. return new ArrayList<>();
  30. }
  31. TypeDescriptor sourceType = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(sourceClass));
  32. TypeDescriptor targetType = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(targetClass));
  33. return (List<T2>) this.conversionService.convert(sourceList, sourceType, targetType);
  34. }
  35. }
Add Comment
Please, Sign In to add comment