Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Luis Mata
Added: Mar 8, 2019 2:52 PM
Modified: Oct 18, 2019 3:51 PM
Views: 19
Tags: converter java
  1. public abstract class Converter<P, R> {
  2.  
  3.     protected Function<P, R> fromPojo;
  4.     protected Function<R, P> fromDO;
  5.  
  6.     protected abstract void initFunction();
  7.    
  8.     protected void setFunction(final Function<P, R> fromPojo, final Function<R, P> fromDO) {
  9.         this.fromPojo = fromPojo;
  10.         this.fromDO = fromDO;
  11.     }
  12.  
  13.     public final P convertFromDataObject(final R dataObject) {
  14.         return fromDO.apply(dataObject);
  15.     }
  16.  
  17.     public final R convertFromPojo(final P pojo) {
  18.         return fromPojo.apply(pojo);
  19.     }
  20.  
  21.     public final List<P> createFromDataObject(final Collection<R> dataObjects) {
  22.         return dataObjects.stream().map(this::convertFromDataObject).collect(Collectors.toList());
  23.     }
  24.  
  25.     public final List<R> createFromPojo(final Collection<P> pojos) {
  26.         return pojos.stream().map(this::convertFromPojo).collect(Collectors.toList());
  27.     }
  28.  
  29. }
  30.