I am working on logic, in which I have to consume, List of PersonDto, and apply rules based on the rule value(private String rule;) present in the PersonDto.
I have an interface called Rules, which will have multiple implementations. But for this question, I have added only 1 implementation called Rule1.
I also have a RuleMapper class which is responsible for providing beans on demand.
Problem:
Based on the rule value present in the PersonDto, I am applying the rules. Inside Rule1 class I have a logic to modify the id field. I am setting new id which I am getting as a parameter.
And finally, I am storing the result into ArrayList.
But in the ArrayList, the value of all the PersonDto is coming as the last id value that I am passing while applying the rule.
For Example:
List<String> ids = Arrays.asList("10001", "100002");
List<PersonDto> result = new ArrayList<PersonDto>();
persons.stream().forEach(person -> {
ids.stream().forEach(id -> {
System.out.println(ruleMapper.getRule(person.getRule()).applyRule(person, id));
result.add(ruleMapper.getRule(person.getRule()).applyRule(person, id));
});
});
As you can see in the above code snippet, there are two ids 10001 & 10002, but when I am storing and then printing the result, then I am getting PersonDto with the id value as 10002 in all the elements. But when I am doing System.out.println(), inside the loop, then I am seeing the correct result.
For more information or to reproduce the problem, please refer to the complete code below:
PersonDto
package com.example.dto;
public class PersonDto {
protected int id;
protected String name;
private String rule;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRule() {
return rule;
}
public void setRule(String rule) {
this.rule = rule;
}
@Override
public String toString() {
return "PersonDto [id=" + id + ", name=" + name + ", rule=" + rule + "]";
}
}
Rules interface
package com.example.service;
import com.example.dto.PersonDto;
public interface Rules {
public PersonDto applyRule(PersonDto input, String newId);
}
Rules 1 implementation
package com.example.service;
import org.springframework.stereotype.Service;
import com.example.dto.PersonDto;
@Service
public class Rule1 implements Rules {
@Override
public PersonDto applyRule(PersonDto input, String newIdt) {
input.setId(Integer.parseInt(newIdt));
return input;
}
}
RuleMapper
package com.example.service;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RuleMapper {
@Autowired
private Map<String, Rules> beans;
public Rules getRule(String ruleName) {
return beans.get(ruleName);
}
public Map<String, Rules> gelAllBeans() {
return beans;
}
}
Controller
package com.example.demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.dto.PersonDto;
import com.example.service.RuleMapper;
@RestController
public class StudentContoller {
@Autowired
private RuleMapper ruleMapper;
@GetMapping(value = "/test")
public void saveStudent() throws Exception {
List<String> orders = Arrays.asList("order 1", "order 2");
List<PersonDto> persons = new ArrayList<PersonDto>();
for (int i = 0; i < 10; i++) {
PersonDto per = new PersonDto();
per.setId(i);
per.setName("John Doe_ ".concat(String.valueOf(i)));
per.setRule("rule" + getRandomRule());
persons.add(per);
}
List<String> ids = Arrays.asList("10001", "100002");
List<PersonDto> result = new ArrayList<PersonDto>();
persons.stream().forEach(person -> {
ids.stream().forEach(id -> {
System.out.println(ruleMapper.getRule(person.getRule()).applyRule(person, id));
result.add(ruleMapper.getRule(person.getRule()).applyRule(person, id));
});
});
for (PersonDto person : result) {
System.out.println(person);
}
}
private int getRandomRule() {
Random r = new Random();
int low = 1;
int high = 2;
int result = r.nextInt(high - low) + low;
return result;
}
}
Expected output
PersonDto [id=10001, name=John Doe_ 0, rule=rule1]
PersonDto [id=100002, name=John Doe_ 0, rule=rule1]
PersonDto [id=10001, name=John Doe_ 1, rule=rule1]
PersonDto [id=100002, name=John Doe_ 1, rule=rule1]
PersonDto [id=10001, name=John Doe_ 2, rule=rule1]
PersonDto [id=100002, name=John Doe_ 2, rule=rule1]
PersonDto [id=10001, name=John Doe_ 3, rule=rule1]
PersonDto [id=100002, name=John Doe_ 3, rule=rule1]
PersonDto [id=10001, name=John Doe_ 4, rule=rule1]
PersonDto [id=100002, name=John Doe_ 4, rule=rule1]
PersonDto [id=10001, name=John Doe_ 5, rule=rule1]
PersonDto [id=100002, name=John Doe_ 5, rule=rule1]
PersonDto [id=10001, name=John Doe_ 6, rule=rule1]
PersonDto [id=100002, name=John Doe_ 6, rule=rule1]
PersonDto [id=10001, name=John Doe_ 7, rule=rule1]
PersonDto [id=100002, name=John Doe_ 7, rule=rule1]
PersonDto [id=10001, name=John Doe_ 8, rule=rule1]
PersonDto [id=100002, name=John Doe_ 8, rule=rule1]
PersonDto [id=10001, name=John Doe_ 9, rule=rule1]
PersonDto [id=100002, name=John Doe_ 9, rule=rule1]
Actual Output
PersonDto [id=100002, name=John Doe_ 0, rule=rule1]
PersonDto [id=100002, name=John Doe_ 0, rule=rule1]
PersonDto [id=100002, name=John Doe_ 1, rule=rule1]
PersonDto [id=100002, name=John Doe_ 1, rule=rule1]
PersonDto [id=100002, name=John Doe_ 2, rule=rule1]
PersonDto [id=100002, name=John Doe_ 2, rule=rule1]
PersonDto [id=100002, name=John Doe_ 3, rule=rule1]
PersonDto [id=100002, name=John Doe_ 3, rule=rule1]
PersonDto [id=100002, name=John Doe_ 4, rule=rule1]
PersonDto [id=100002, name=John Doe_ 4, rule=rule1]
PersonDto [id=100002, name=John Doe_ 5, rule=rule1]
PersonDto [id=100002, name=John Doe_ 5, rule=rule1]
PersonDto [id=100002, name=John Doe_ 6, rule=rule1]
PersonDto [id=100002, name=John Doe_ 6, rule=rule1]
PersonDto [id=100002, name=John Doe_ 7, rule=rule1]
PersonDto [id=100002, name=John Doe_ 7, rule=rule1]
PersonDto [id=100002, name=John Doe_ 8, rule=rule1]
PersonDto [id=100002, name=John Doe_ 8, rule=rule1]
PersonDto [id=100002, name=John Doe_ 9, rule=rule1]
PersonDto [id=100002, name=John Doe_ 9, rule=rule1]