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", "[email protected]", false, "6456453"));
invites.add(new Invite("John", "[email protected]", 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"));
forEach