<commit_before>/*
 * Copyright 2009-2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.jqno.equalsverifier;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import nl.jqno.equalsverifier.internal.FieldIterable;
import nl.jqno.equalsverifier.internal.Formatter;
import nl.jqno.equalsverifier.internal.ObjectAccessor;

import java.lang.reflect.Field;
import java.util.List;

import static nl.jqno.equalsverifier.internal.Assert.*;

class ExamplesChecker<T> implements Checker {
    private final Class<T> type;
    private final List<T> equalExamples;
    private final List<T> unequalExamples;
    private final CachedHashCodeInitializer<T> cachedHashCodeInitializer;

    public ExamplesChecker(Configuration<T> config, List<T> equalExamples, List<T> unequalExamples) {
        this.type = config.getType();
        this.equalExamples = equalExamples;
        this.unequalExamples = unequalExamples;
        this.cachedHashCodeInitializer = config.getCachedHashCodeInitializer();
    }

    @Override
    public void check() {
        for (int i = 0; i < equalExamples.size(); i++) {
            T reference = equalExamples.get(i);
            checkSingle(reference);

            for (int j = i + 1; j < equalExamples.size(); j++) {
                T other = equalExamples.get(j);
                checkEqualButNotIdentical(reference, other);
                checkHashCode(reference, other);
            }
        }

        for (T reference : unequalExamples) {
            checkSingle(reference);
        }
    }

    private void checkEqualButNotIdentical(T reference, T other) {
        assertFalse(Formatter.of("Precondition: the same object appears twice:\n  %%", reference),
                reference == other);
        assertFalse(Formatter.of("Precondition: two identical objects appear:\n  %%", reference),
                isIdentical(reference, other));
        assertTrue(Formatter.of("Precondition: not all equal objects are equal:\n  %%\nand\n  %%", reference, other),
                reference.equals(other));
    }

    private void checkSingle(T reference) {
        final T copy = ObjectAccessor.of(reference, type).copy();

        checkReflexivity(reference);
        checkNonNullity(reference);
        checkTypeCheck(reference);
        checkHashCode(reference, copy);
    }

    private void checkReflexivity(T reference) {
        assertEquals(Formatter.of("Reflexivity: object does not equal itself:\n  %%", reference),
                reference, reference);
    }

    @SuppressFBWarnings(value = "EC_NULL_ARG", justification = "Check what happens when null is passed into equals.")
    private void checkNonNullity(T reference) {
        try {
            boolean nullity = reference.equals(null);
            assertFalse(Formatter.of("Non-nullity: true returned for null value"), nullity);
        }
        catch (NullPointerException e) {
            fail(Formatter.of("Non-nullity: NullPointerException thrown"), e);
        }
    }

    private void checkTypeCheck(T reference) {
        class SomethingElse {}
        SomethingElse somethingElse = new SomethingElse();
        try {
            reference.equals(somethingElse);
        }
        catch (ClassCastException e) {
            fail(Formatter.of("Type-check: equals throws ClassCastException.\nAdd an instanceof or getClass() check."), e);
        }
        catch (Exception e) {
            fail(Formatter.of("Type-check: equals throws %%.\nAdd an instanceof or getClass() check.", e.getClass().getSimpleName()), e);
        }
    }

    private void checkHashCode(T reference, T copy) {
        int referenceHashCode = cachedHashCodeInitializer.getInitializedHashCode(reference);
        assertEquals(Formatter.of("hashCode: hashCode should be consistent:\n  %% (%%)", reference, referenceHashCode),
                referenceHashCode, cachedHashCodeInitializer.getInitializedHashCode(reference));

        if (!reference.equals(copy)) {
            return;
        }

        int copyHashCode = cachedHashCodeInitializer.getInitializedHashCode(copy);
        Formatter f = Formatter.of("hashCode: hashCodes should be equal:\n  %% (%%)\nand\n  %% (%%)",
                reference, referenceHashCode, copy, copyHashCode);
        assertEquals(f, referenceHashCode, copyHashCode);
    }

    private boolean isIdentical(T reference, T other) {
        for (Field field : FieldIterable.of(reference.getClass())) {
            try {
                field.setAccessible(true);
                if (!nullSafeEquals(field.get(reference), field.get(other))) {
                    return false;
                }
            }
            catch (IllegalArgumentException | IllegalAccessException e) {
                return false;
            }
        }

        return true;
    }

    private boolean nullSafeEquals(Object x, Object y) {
        return x == null ? y == null : x.equals(y);
    }
}
<commit_msg>PIT: replace custom nullSafeEquals method with Objects.equals<commit_after>/*
 * Copyright 2009-2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.jqno.equalsverifier;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import nl.jqno.equalsverifier.internal.FieldIterable;
import nl.jqno.equalsverifier.internal.Formatter;
import nl.jqno.equalsverifier.internal.ObjectAccessor;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

import static nl.jqno.equalsverifier.internal.Assert.*;

class ExamplesChecker<T> implements Checker {
    private final Class<T> type;
    private final List<T> equalExamples;
    private final List<T> unequalExamples;
    private final CachedHashCodeInitializer<T> cachedHashCodeInitializer;

    public ExamplesChecker(Configuration<T> config, List<T> equalExamples, List<T> unequalExamples) {
        this.type = config.getType();
        this.equalExamples = equalExamples;
        this.unequalExamples = unequalExamples;
        this.cachedHashCodeInitializer = config.getCachedHashCodeInitializer();
    }

    @Override
    public void check() {
        for (int i = 0; i < equalExamples.size(); i++) {
            T reference = equalExamples.get(i);
            checkSingle(reference);

            for (int j = i + 1; j < equalExamples.size(); j++) {
                T other = equalExamples.get(j);
                checkEqualButNotIdentical(reference, other);
                checkHashCode(reference, other);
            }
        }

        for (T reference : unequalExamples) {
            checkSingle(reference);
        }
    }

    private void checkEqualButNotIdentical(T reference, T other) {
        assertFalse(Formatter.of("Precondition: the same object appears twice:\n  %%", reference),
                reference == other);
        assertFalse(Formatter.of("Precondition: two identical objects appear:\n  %%", reference),
                isIdentical(reference, other));
        assertTrue(Formatter.of("Precondition: not all equal objects are equal:\n  %%\nand\n  %%", reference, other),
                reference.equals(other));
    }

    private void checkSingle(T reference) {
        final T copy = ObjectAccessor.of(reference, type).copy();

        checkReflexivity(reference);
        checkNonNullity(reference);
        checkTypeCheck(reference);
        checkHashCode(reference, copy);
    }

    private void checkReflexivity(T reference) {
        assertEquals(Formatter.of("Reflexivity: object does not equal itself:\n  %%", reference),
                reference, reference);
    }

    @SuppressFBWarnings(value = "EC_NULL_ARG", justification = "Check what happens when null is passed into equals.")
    private void checkNonNullity(T reference) {
        try {
            boolean nullity = reference.equals(null);
            assertFalse(Formatter.of("Non-nullity: true returned for null value"), nullity);
        }
        catch (NullPointerException e) {
            fail(Formatter.of("Non-nullity: NullPointerException thrown"), e);
        }
    }

    private void checkTypeCheck(T reference) {
        class SomethingElse {}
        SomethingElse somethingElse = new SomethingElse();
        try {
            reference.equals(somethingElse);
        }
        catch (ClassCastException e) {
            fail(Formatter.of("Type-check: equals throws ClassCastException.\nAdd an instanceof or getClass() check."), e);
        }
        catch (Exception e) {
            fail(Formatter.of("Type-check: equals throws %%.\nAdd an instanceof or getClass() check.", e.getClass().getSimpleName()), e);
        }
    }

    private void checkHashCode(T reference, T copy) {
        int referenceHashCode = cachedHashCodeInitializer.getInitializedHashCode(reference);
        assertEquals(Formatter.of("hashCode: hashCode should be consistent:\n  %% (%%)", reference, referenceHashCode),
                referenceHashCode, cachedHashCodeInitializer.getInitializedHashCode(reference));

        if (!reference.equals(copy)) {
            return;
        }

        int copyHashCode = cachedHashCodeInitializer.getInitializedHashCode(copy);
        Formatter f = Formatter.of("hashCode: hashCodes should be equal:\n  %% (%%)\nand\n  %% (%%)",
                reference, referenceHashCode, copy, copyHashCode);
        assertEquals(f, referenceHashCode, copyHashCode);
    }

    private boolean isIdentical(T reference, T other) {
        for (Field field : FieldIterable.of(reference.getClass())) {
            try {
                field.setAccessible(true);
                if (!Objects.equals(field.get(reference), field.get(other))) {
                    return false;
                }
            }
            catch (IllegalArgumentException | IllegalAccessException e) {
                return false;
            }
        }

        return true;
    }
}

{'fraction_non_alphanumeric': 0.10134636665437108, 'fraction_numerical': 0.0025820730357801547, 'mean_word_length': 2.7178608159067537, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, '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': '11844', 'n_tokens_mistral': 2896, 'n_tokens_neox': 2779, 'n_words': 754}