<commit_before>/*
 * SportChef – Sports Competition Management Software
 * Copyright (C) 2015, 2016 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.sportchef.business.user.control;

import ch.sportchef.business.exception.ExpectationFailedException;
import ch.sportchef.business.user.entity.User;
import ch.sportchef.business.user.entity.UserBuilder;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.toList;

class UserRepository implements Serializable {

    private static final long serialVersionUID = 1L;

    private final Map<Long, User> users = new ConcurrentHashMap<>();

    private final AtomicLong userSeq = new AtomicLong(0);

    User create(@NotNull final User user) {
        if (findByEmail(user.getEmail()).isPresent()) {
            throw new ExpectationFailedException("Email address has to be unique");
        }
        final Long userId = userSeq.incrementAndGet();
        final User userToCreate = UserBuilder.fromUser(user).withUserId(userId).build();
        this.users.put(userId, userToCreate);
        return userToCreate;
    }

    User update(@NotNull final User user) {
        this.users.put(user.getUserId(), user);
        return user;
    }

    Optional<User> findByUserId(@NotNull final Long userId) {
        return Optional.ofNullable(this.users.get(userId));
    }

    Optional<User> findByEmail(@NotNull final String email) {
        return this.users.values().stream()
                .filter(user -> email.equals(user.getEmail()))
                .findAny();
    }

    List<User> findAll() {
        return this.users.values().stream()
                .sorted(comparingLong(User::getUserId))
                .collect(toList());
    }

    void delete(final Long userId) {<commit_msg>#209 The UserRepository should update the version and check for conflicting updates<commit_after>/*
 * SportChef – Sports Competition Management Software
 * Copyright (C) 2015, 2016 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.sportchef.business.user.control;

import ch.sportchef.business.exception.ExpectationFailedException;
import ch.sportchef.business.user.entity.User;
import ch.sportchef.business.user.entity.UserBuilder;

import javax.persistence.OptimisticLockException;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.toList;

class UserRepository implements Serializable {

    private static final long serialVersionUID = 1L;

    private final Map<Long, User> users = new ConcurrentHashMap<>();

    private final AtomicLong userSeq = new AtomicLong(0);

    User create(@NotNull final User user) {
        if (findByEmail(user.getEmail()).isPresent()) {
            throw new ExpectationFailedException("Email address has to be unique");
        }
        final Long userId = userSeq.incrementAndGet();
        final Long version = Long.valueOf(user.hashCode());
        final User userToCreate = UserBuilder.fromUser(user)
                .withUserId(userId)
                .withVersion(version)
                .build();
        this.users.put(userId, userToCreate);
        return userToCreate;
    }

    User update(@NotNull final User user) {
        final User previousUser = users.getOrDefault(user.getUserId(), user);
        if (!previousUser.getVersion().equals(user.getVersion())) {
            throw new OptimisticLockException("You tried to update an user that was modified concurrently!");
        }
        final Long version = Long.valueOf(user.hashCode());
        final User userToUpdate = UserBuilder.fromUser(user)
                .withVersion(version)
                .build();
        users.put(userToUpdate.getUserId(), userToUpdate);
        return userToUpdate;
    }

    Optional<User> findByUserId(@NotNull final Long userId) {
        return Optional.ofNullable(this.users.get(userId));
    }

    Optional<User> findByEmail(@NotNull final String email) {
        return this.users.values().stream()
                .filter(user -> email.equals(user.getEmail()))
                .findAny();
    }

    List<User> findAll() {
        return this.users.values().stream()
                .sorted(comparingLong(User::getUserId))
                .collect(toList());
    }

    void delete(final Long userId) {
{'fraction_non_alphanumeric': 0.09503311258278145, 'fraction_numerical': 0.0041390728476821195, 'mean_word_length': 3.2965860597439547, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2277300', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1589, 'n_words': 505}