I'am using spring and I defined bean with ArrayList
. invites
it is a list with Invite
objects.
@Getter
public class Invite {
private String invitee;
private String email;
private boolean confirm;
private String token;
}
This is my data privider class :
@Getter
public class InvitationsData {
private List<Invite> invites = new ArrayList<>();
@PostConstruct
private void initInvites(){
invites.add(new Invite("John", "john@john.com", false, "6456453"));
invites.add(new Invite("John", "john@john.com", false, "3252352"));
}
}
In configuration class I created @Bean
from InvitationsData
- it works.
In the service I would like to modify one object from list which matches to token string and have set confirm
to false
.
invitationsData.getInvites()
.stream()
.filter(i -> token.equals(i.getToken()))
.filter(i -> !i.isConfirm())
.forEach(i -> {
i.setConfirm(true);
});
This stream works fine. Now, when someone call method twice for confirmed object I would like to throw CustomException
. How can I do this with this stream? Where can I put orElseThrow
?
EDIT:
My current solution. I use peek
instead of forEach
invitationsData
.getInvites()
.stream()
.filter(i -> token.equals(i.getToken()))
.filter(i -> !i.isConfirm())
.peek(i -> i.setConfirm(true))
.findFirst()
.orElseThrow(() -> new InvitationConfirmedException("Error"));
If the token
is unique you can do :
getInvites().stream()
.filter(i -> token.equals(i.getToken()))
.filter(i -> !i.isConfirm())
.findAny()
.orElseThrow(IllegalArgumentException::new)
.setConfirm(true);
If not :
getInvites().stream()
.filter(i -> token.equals(i.getToken()))
.forEach(i -> {
if (i.isConfirm())
throw new CustomException();
else
i.setConfirm(true);
});