Your second example:
getAllPerson(criteria)
.map(person -> {
person.setLastUpdated(new Date())
return person;
});
...won't compile, but only because it's missing a semicolon on the setLastUpdated()
line.
However, by far the best thing to do here (as suggested already) is to make your Person
class immutable. You can then use the "wither" pattern (either using lombok or your own implementation) to simply do:
getAllPerson(criteria)
.map(person -> person.withLastUpdated(new Date()));
If you can't make Person
immutable (either because it's an external library, or its mutability is simply too baked into existing code), then all is not lost. You can still produce an immutable wrapper quite easily, eg:
class ImmutablePerson {
Person person;
public ImmutablePerson(Person p) {
this.person = p;
}
public Person withFirstName(String name) {
return new Person(name, person.getLastName(), person.getLastUpdated());
}
public Person withLastName(String name) {
return new Person(person.getFirstName(), name, person.getLastUpdated());
}
public Person withLastUpdated(Date date) {
return new Person(person.getFirstName(), person.getLastName(), date);
}
public String getFirstName() {return person.getFirstName();}
public String getLastName() {return person.getLastName();}
public Date getLastUpdated() {return person.getLastUpdated();}
public Person toPerson() {return new Person(person.getFirstName(), person.getLastName(), person.getLastUpdated());}
}
You can then just map to ImmutablePerson
in the first step of your reactive chain like so:
getAllPerson(criteria)
.map(ImmutablePerson::new)
.map(p -> p.withLastUpdated(new Date()))
peek()
, but I am unsure whetherproject-reactor
offers something like that. – Fureeishpeek()
does that. – Fureeish