I have a calling method as genJsonPayload(). Inside this method I am calling a replace() method to replace some string with randomly generated values. I want to call the replace() by passing another method as parameter. My sample code below.
public static StringBuffer replace(String jsonparam, StringBuffer jsondata, String replacedWith) {
int start = 0;
int last = 0;
while (jsondata.indexOf(jsonparam)!= jsondata.lastIndexOf(jsonparam)) {
start = jsondata.indexOf(jsonparam);
last = start + jsonparam.length();
jsondata = jsondata.replace(start,last,replacedwith);
}
start = jsondata.indexOf(jsonparam);
last = start + jsonparam.length();
jsondata = jsondata.replace(start,last,replacedwith);
return jsondata;
}
public String genJSONPayload(String jsonpayload) {
StringBuffer sbuffer = new StringBuffer(jsonpayload);
try {
jsondata = replace("expectedAreaCode",jsondata, CreateRandom.createAreaCDRandom());
}
catch (Exception e) {
....
}
try {
jsondata = replace("expectedextensionNo",jsondata, CreateRandom.createExtensionNumberRandom());
}
catch (Exception e) {
....
}
}
Json payload as below:
{
...
...
"phonenumber": [
{
"areaCode": "expectedAreaCode",
"extensionNo": "expectedextensionNo",
...
}
{
"areaCode": "expectedAreaCode",
"extensionNo": "expectedextensionNo",
...
}
{
"areaCode": "expectedAreaCode",
"extensionNo": "expectedextensionNo",
...
}],
...
...
}
Note: CreateRandom.createAreaCDRandom() return a random String.
Problem statement: My requirement is to replace "expectedAreaCode" of json file with randomly generated area code using the replace() method. But as I am executing the CreateRandom.createAreaCDRandom() from genJSONPayload() only once so replacedWith in replace() method is getting store with fixed area code and for all the area code in the json array phonenumber, I am getting the same areacode.
Question:
Instead of passing the replacedWith String to the replace() method, can I pass the createAreaCDRandom() static method as parameter and instead of executing createAreaCDRandom() method from genJSONPayload, then I will execute it inside replace method multiple times inside the while loop. I have more than 5 such random mentods, basically for different json parameters. How can I pass the createAreaCDRandom(), createExtensionNumberRandom(), etc methods as the parameter in the repace() method?
Use Supplier class as a method parameter ( https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html ).
Then You can pass lambda or method reference to provide value generating logic.
public static StringBuffer replace(String jsonparam, StringBuffer jsondata, Supplier<String> supplier) {
int start = 0;
int last = 0;
while (jsondata.indexOf(jsonparam) != jsondata.lastIndexOf(jsonparam)) {
start = jsondata.indexOf(jsonparam);
last = start + jsonparam.length();
jsondata = jsondata.replace(start, last, supplier.get());
}
start = jsondata.indexOf(jsonparam);
last = start + jsonparam.length();
jsondata = jsondata.replace(start, last, supplier.get());
return jsondata;
}
public String genJSONPayload(String jsonpayload) {
StrignBuffer sbuffer = new StrignBuffer(jsonpayload);
try{
jsondata = replace("expectedAreaCode",jsondata, CreateRandom::createAreaCDRandom);
}
catch(Exception e){
....
}
try{
jsondata = replace("expectedextensionNo",jsondata, CreateRandom::createExtensionNumberRandom);
}
catch(Exception e){
....
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.