private void checkEmail(User user, String newEmail) throws EmailIsAlreadyTakenException, InvalidEmailException { if (!Validator.isEmail(newEmail)) { throw new InvalidEmailException(); } Optional<User> otherUser = findUser(newEmail); if (otherUser.isPresent() && !user.equals(otherUser.get())) { throw new EmailIsAlreadyTakenException(); } }
@Override public void signup(User user, String rawPassword) throws InvalidEmailException, EmailIsAlreadyTakenException, ScreenNameIsAlreadyTakenException { Objects.requireNonNull(user, "user"); Objects.requireNonNull(rawPassword, "rawPassword"); String email = user.getEmail(); if (!Validator.isEmail(email)) { throw new InvalidEmailException(); } if (isEmailTaken(email)) { throw new EmailIsAlreadyTakenException(); } if (isScreenNameTaken(user.getScreenName())) { throw new ScreenNameIsAlreadyTakenException(); } Password password = passwordSecurity.ecrypt(rawPassword); user.setPassword(password); user = store(user); userEventEmitter.emit(new UserEvent(user.getId(), SIGNUP_REQUESTED)); }