Sometimes we need to convert or cast or transform an object from one type to another class. There are many ways, by which we can do it.
Let's say we have two objects ExternalPeopleInfo and InternalPeopleInfo. Assume we get ExternalPeopleInfo by calling some external API, but to save the details in our system we need to convert it in to InternalPeopleInfo.
Solution 1). We can write a constructor in InternalPeopleInfo, which takes ExternalPeopleInfo object from which it get the desired values and set it in InternalPeopleInfo object.
Solution 2). We can use Java 8's java.util.function.Function, which accepts one argument and produces a result.
To convert an object into another we need to define a function, which returns the required object (InternalPeopleInfo) and take the object which need to be transformed (ExternalPeopleInfo).
-K Himaanshu Shuklaa..
Let's say we have two objects ExternalPeopleInfo and InternalPeopleInfo. Assume we get ExternalPeopleInfo by calling some external API, but to save the details in our system we need to convert it in to InternalPeopleInfo.
Solution 1). We can write a constructor in InternalPeopleInfo, which takes ExternalPeopleInfo object from which it get the desired values and set it in InternalPeopleInfo object.
This file contains 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
class ExternalPeopleInfo{ | |
private int externalId; | |
private String peopleName; | |
private String peopleAge; | |
//getters and setters | |
} | |
class InternalPeopleInfo{ | |
private int internalId; | |
private String peopleName; | |
private String peopleAge; | |
public InternalPeopleInfo() {} | |
public InternalPeopleInfo(ExternalPeopleInfo externalPeopleInfo) { | |
this.internalId=externalPeopleInfo.getExternalId(); | |
this.peopleName=externalPeopleInfo.getPeopleName(); | |
this.peopleAge=externalPeopleInfo.getPeopleAge(); | |
} | |
//getters and setters | |
} |
Solution 2). We can use Java 8's java.util.function.Function, which accepts one argument and produces a result.
This file contains 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
Function<T, R> myFunction = new Function<T, R>() { | |
public R apply(T t) { | |
return null; | |
} | |
}; |
This file contains 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 InternalPeopleInfo getInternalPeopleInfo(ExternalPeopleInfo externalPeopleInfo) { | |
Function<ExternalPeopleInfo, InternalPeopleInfo> externalToTransactionDetails = | |
new Function<ExternalPeopleInfo, InternalPeopleInfo>() { | |
public InternalPeopleInfo apply(ExternalPeopleInfo txnRequest) { | |
InternalPeopleInfo internalPeopleInfo = new InternalPeopleInfo(); | |
internalPeopleInfo.setInternalId(externalPeopleInfo.getExternalId()); | |
internalPeopleInfo.setPeopleName(externalPeopleInfo.getPeopleName()); | |
internalPeopleInfo.setPeopleAge(externalPeopleInfo.getPeopleAge()); | |
return internalPeopleInfo; | |
} | |
}; | |
return externalToTransactionDetails.apply(externalPeopleInfo); | |
} |
No comments:
Post a Comment