<commit_before>package fi.vincit.multiusertest.rule.expectation2;

import fi.vincit.multiusertest.rule.AuthorizationRule;
import fi.vincit.multiusertest.util.UserIdentifier;
import fi.vincit.multiusertest.util.UserIdentifiers;

import java.util.*;

public abstract class AbstractWhenThen<T extends TestExpectation> implements WhenThen<T> {

    private final Set<UserIdentifier> currentIdentifiers = new HashSet<>();
    private final Map<UserIdentifier, T> expectationsByIdentifier = new HashMap<>();

    private final UserIdentifier userIdentifier;
    private final AuthorizationRule authorizationRule;
    private T defaultExpectation;

    public AbstractWhenThen(UserIdentifier userIdentifier, AuthorizationRule authorizationRule) {
        this.userIdentifier = userIdentifier;
        this.authorizationRule = authorizationRule;
    }

    @Override
    public WhenThen<T> whenCalledWith(UserIdentifiers... userIdentifiers) {
        currentIdentifiers.clear();

        if (userIdentifiers.length == 0) {
            throw new IllegalArgumentException("At least one identifier must be defined");
        }

        for (UserIdentifiers identifiers : userIdentifiers) {
            identifiers.getIdentifiers().forEach(this::addCurrentUserIdentifiers);
        }
        return this;
    }

    @Override
    public WhenThen<T> whenCalledWith(UserIdentifier... userIdentifiers) {
        currentIdentifiers.clear();

        if (userIdentifiers.length == 0) {
            throw new IllegalArgumentException("At least one identifier must be defined");
        }

        addCurrentUserIdentifiers(userIdentifiers);

        return this;
    }

    private void addCurrentUserIdentifiers(UserIdentifier... userIdentifiers) {
        for (UserIdentifier identifier : userIdentifiers) {
            if (currentIdentifiers.contains(identifier)) {
                throw new IllegalStateException("User identifier " + identifier + " already set");
            }
            currentIdentifiers.add(identifier);
        }
    }

    @Override
    public WhenThen<T> then(final T testExpectation) {
        if (currentIdentifiers.isEmpty()) {
            throw new IllegalStateException("Call whenCalledWith before calling then method");
        }

        try {
            currentIdentifiers.forEach(identifier -> {
                if (expectationsByIdentifier.containsKey(identifier)) {
                    throw new IllegalStateException(
                            String.format("User identifier %s already has expectation",
                                    identifier.toString()
                            )
                    );
                } else {
                    expectationsByIdentifier.put(
                            identifier,
                            testExpectation
                    );
                }
            });
        } finally {
            currentIdentifiers.clear();
        }

        return this;
    }

    @Override
    public WhenThen<T> otherwise(T testExpectation) {
        this.defaultExpectation = testExpectation;
        return this;
    }
<commit_msg>Add null checks to AbstractWhenThen method arguments<commit_after>package fi.vincit.multiusertest.rule.expectation2;

import fi.vincit.multiusertest.rule.AuthorizationRule;
import fi.vincit.multiusertest.util.UserIdentifier;
import fi.vincit.multiusertest.util.UserIdentifiers;

import java.util.*;

public abstract class AbstractWhenThen<T extends TestExpectation> implements WhenThen<T> {

    private final Set<UserIdentifier> currentIdentifiers = new HashSet<>();
    private final Map<UserIdentifier, T> expectationsByIdentifier = new HashMap<>();

    private final UserIdentifier userIdentifier;
    private final AuthorizationRule authorizationRule;
    private T defaultExpectation;

    public AbstractWhenThen(UserIdentifier userIdentifier, AuthorizationRule authorizationRule) {
        this.userIdentifier = userIdentifier;
        this.authorizationRule = authorizationRule;
    }

    @Override
    public WhenThen<T> whenCalledWith(UserIdentifiers... userIdentifiers) {
        currentIdentifiers.clear();

        if (userIdentifiers.length == 0) {
            throw new IllegalArgumentException("At least one identifier must be defined");
        }

        for (UserIdentifiers identifiers : userIdentifiers) {
            identifiers.getIdentifiers().forEach(this::addCurrentUserIdentifiers);
        }
        return this;
    }

    @Override
    public WhenThen<T> whenCalledWith(UserIdentifier... userIdentifiers) {
        currentIdentifiers.clear();

        if (userIdentifiers.length == 0) {
            throw new IllegalArgumentException("At least one identifier must be defined");
        }

        addCurrentUserIdentifiers(userIdentifiers);

        return this;
    }

    private void addCurrentUserIdentifiers(UserIdentifier... userIdentifiers) {
        for (UserIdentifier identifier : userIdentifiers) {
            if (currentIdentifiers.contains(identifier)) {
                throw new IllegalStateException("User identifier " + identifier + " already set");
            }
            currentIdentifiers.add(identifier);
        }
    }

    @Override
    public WhenThen<T> then(final T testExpectation) {
        Objects.requireNonNull(testExpectation, "testExpectation must not be null");

        if (currentIdentifiers.isEmpty()) {
            throw new IllegalStateException("Call whenCalledWith before calling then method");
        }

        try {
            currentIdentifiers.forEach(identifier -> {
                if (expectationsByIdentifier.containsKey(identifier)) {
                    throw new IllegalStateException(
                            String.format("User identifier %s already has expectation",
                                    identifier.toString()
                            )
                    );
                } else {
                    expectationsByIdentifier.put(
                            identifier,
                            testExpectation
                    );
                }
            });
        } finally {
            currentIdentifiers.clear();
        }

        return this;
    }

    @Override
    public WhenThen<T> otherwise(T testExpectation) {
        Objects.requireNonNull(testExpectation, "testExpectation must not be null");
        this.defaultExpectation = testExpectation;
        return this;
    }

{'fraction_non_alphanumeric': 0.07673190431811122, 'fraction_numerical': 0.0009319664492078285, 'mean_word_length': 2.295291709314227, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6038159', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1470, 'n_words': 342}