All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString Maven / Gradle / Ivy

There is a newer version: 5.15.0
Show newest version
package org.mockserver.model;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * @author jamesdbloom
 */
public abstract class ObjectWithReflectiveEqualsHashCodeToString {

    private static final String[] IGNORE_KEY_FIELD = {};

    protected String[] fieldsExcludedFromEqualsAndHashCode() {
        return IGNORE_KEY_FIELD;
    }

    @Override
    public String toString() {
        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE, null, ObjectWithReflectiveEqualsHashCodeToString.class, false, false).setExcludeFieldNames(fieldsExcludedFromEqualsAndHashCode()).toString();
    }

    @Override
    @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (other == null) {
            return false;
        }
        return new EqualsBuilder()
                    .setExcludeFields(fieldsExcludedFromEqualsAndHashCode())
                    .setReflectUpToClass(ObjectWithReflectiveEqualsHashCodeToString.class)
                    .setTestTransients(false)
                    .setTestRecursive(false)
                    .reflectionAppend(this, other)
                    .isEquals();
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this, fieldsExcludedFromEqualsAndHashCode());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy