<commit_before>package name.mlnkrishnan.shouldJ.asserter;

import name.mlnkrishnan.shouldJ.failure.ActualValueIsNull;
import name.mlnkrishnan.shouldJ.failure.ExpectationMismatch;
import org.hamcrest.Matcher;

import java.lang.reflect.Array;

public class ObjectAsserter<T> {
    final protected T actual;

    public ObjectAsserter(T actual) {
        this.actual = actual;
    }

    public ObjectAsserter<T> shouldBe(T expected) {

        if (isArray(expected) && isArray(actual)) {
            if (! lengthsEqual(expected) || ! elementsEqual(expected))
                throw new ExpectationMismatch(String.format("expected array <%s> to be <%s>", toArrayString(actual), toArrayString(expected)));
            return this;
        }

        if(!actual.equals(expected))
           throw new ExpectationMismatch(String.format("expected <%s>, but got <%s>", expected, actual));
        return this;
    }

    public ObjectAsserter<T> shouldNotBe(T notExpected) {
        if(actual.equals(notExpected))
            throw new ExpectationMismatch(String.format("expected not same"));
        return this;
    }

    public ObjectAsserter<T> shouldBeNull() {
        if (actual != null)
            throw new ExpectationMismatch(String.format("expected <null>, but was <%s>", actual));
        return this;
    }

    public ObjectAsserter<T> shouldNotBeNull() {
        if (actual == null)
            throw new ExpectationMismatch("expected not null");
        return this;
    }
    
    public ObjectAsserter<T> shouldBeOfType(Class<?> expectedType){
        if(actual == null)
            throw new ActualValueIsNull();
        Class<? extends Object> actualType = actual.getClass();
        if(!actualType.equals(expectedType))
            throw new ExpectationMismatch(String.format("expected type <%s>, but was <%s>", expectedType, actualType));
        return this;
    }

    public ObjectAsserter<T> shouldMatch(Matcher<T> matcher) {
        boolean matches = matcher.matches(actual);
        if (!matches) {
            throw new ExpectationMismatch(matcher.toString() + ", but was <" + actual + ">");
        }
        return this;
    }

    private boolean isArray(Object obj) {
        return obj.getClass().isArray();
    }

    private boolean lengthsEqual(Object expected) {
        return Array.getLength(expected) == Array.getLength(actual);
    }

    private boolean elementsEqual(Object expected) {
        for(int i = 0; i < Array.getLength(expected); ++i) {
            if (! Array.get(expected, i).equals(Array.get(actual, i))) {
                return false;
            }
        }
        return true;
    }

    private String toArrayString(Object expected) {
        String begin = "[";
        String delimiter = ",";
        String end = "]";

        StringBuilder arrayString = new StringBuilder(begin);
        boolean separate = false;
        for(int i = 0; i < Array.getLength(expected); ++i) {
            if (separate) {
                arrayString.append(delimiter);
            }
            arrayString.append(Array.get(expected, i));
            separate = true;
        }

        arrayString.append(end);

        return arrayString.toString();
    }
}<commit_msg>null checking<commit_after>package name.mlnkrishnan.shouldJ.asserter;

import name.mlnkrishnan.shouldJ.failure.ActualValueIsNull;
import name.mlnkrishnan.shouldJ.failure.ExpectationMismatch;
import org.hamcrest.Matcher;

import java.lang.reflect.Array;

public class ObjectAsserter<T> {
    final protected T actual;

    public ObjectAsserter(T actual) {
        this.actual = actual;
    }

    public ObjectAsserter<T> shouldBe(T expected) {
        if (actual == null)
            throw new ActualValueIsNull();

        if (isArray(expected) && isArray(actual)) {
            if (!lengthsEqual(expected) || !elementsEqual(expected))
                throw new ExpectationMismatch(String.format("expected array <%s> to be <%s>", toArrayString(actual), toArrayString(expected)));
            return this;
        }

        if (!actual.equals(expected))
            throw new ExpectationMismatch(String.format("expected <%s>, but got <%s>", expected, actual));
        return this;
    }

    public ObjectAsserter<T> shouldNotBe(T notExpected) {
        if (actual.equals(notExpected))
            throw new ExpectationMismatch(String.format("expected not same"));
        return this;
    }

    public ObjectAsserter<T> shouldBeNull() {
        if (actual != null)
            throw new ExpectationMismatch(String.format("expected <null>, but was <%s>", actual));
        return this;
    }

    public ObjectAsserter<T> shouldNotBeNull() {
        if (actual == null)
            throw new ExpectationMismatch("expected not null");
        return this;
    }

    public ObjectAsserter<T> shouldBeOfType(Class<?> expectedType) {
        if (actual == null)
            throw new ActualValueIsNull();
        Class<? extends Object> actualType = actual.getClass();
        if (!actualType.equals(expectedType))
            throw new ExpectationMismatch(String.format("expected type <%s>, but was <%s>", expectedType, actualType));
        return this;
    }

    public ObjectAsserter<T> shouldMatch(Matcher<T> matcher) {
        boolean matches = matcher.matches(actual);
        if (!matches) {
            throw new ExpectationMismatch(matcher.toString() + ", but was <" + actual + ">");
        }
        return this;
    }

    private boolean isArray(Object obj) {
        if (obj == null) {
            return false;
        }
        return obj.getClass().isArray();
    }

    private boolean lengthsEqual(Object expected) {
        return Array.getLength(expected) == Array.getLength(actual);
    }

    private boolean elementsEqual(Object expected) {
        for (int i = 0; i < Array.getLength(expected); ++i) {
            if (!Array.get(expected, i).equals(Array.get(actual, i))) {
                return false;
            }
        }
        return true;
    }

    private String toArrayString(Object expected) {
        String begin = "[";
        String delimiter = ",";
        String end = "]";

        StringBuilder arrayString = new StringBuilder(begin);
        boolean separate = false;
        for (int i = 0; i < Array.getLength(expected); ++i) {
            if (separate) {
                arrayString.append(delimiter);
            }
            arrayString.append(Array.get(expected, i));
            separate = true;
        }

        arrayString.append(end);

        return arrayString.toString();
    }
}
{'fraction_non_alphanumeric': 0.11651669971023333, 'fraction_numerical': 0.0006100350770169285, 'mean_word_length': 2.44794952681388, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2276985', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1817, 'n_words': 428}