Last active
August 10, 2017 20:21
-
-
Save berezovskyi/e7f025bfbb88308e1a992702a5058297 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
public class MyClass { | |
@Autowired | |
private ConvertAToB convertAToB; | |
@Autowired | |
private ConvertBToC convertBToC; | |
@Autowired | |
private ConvertCToD convertCToD; | |
private <T,R> convertObject(T inputObject, Function<T, Optional<R>> resultConvertor) { | |
try { | |
Optional<R> destOptional = resultConvertor.apply(inputObject); | |
if (destOptional == null || !destOptional.isPresent()) { | |
// need to pass Class<R> clazz variable to be able to get the dest type | |
// at the runtime in Java | |
throw new ConversionException("Failed to convert object", inputObject); | |
// or return Optional.empty(); | |
} | |
return destOptional.get(); | |
} catch (...Exception | ...Exception e) { | |
throw new ConversionException("Failed to convert object", inputObject, e); | |
} | |
} | |
public Optional convertAToD(ObjectA objectA) throws ConversionException { | |
if(objectA == null) { | |
throw new IllegalArgumentException("objectA shall not be null"); | |
} | |
try { | |
TypeB objectB = convertObject(objectA, convertAToB::convertAtoB); | |
TypeC objectC = convertObject(objectB, convertBToC::convertBtoC); | |
TypeD objectD = convertObject(objectC, convertCToD::convertCtoD); | |
} catch (ConversionException e) { | |
return Optional.empty(); | |
// or throw e; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment