Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

[Feature] Generate hashcode & equals methods for model classes #94

Open
Nicklas2751 opened this issue Apr 24, 2022 · 1 comment
Open

Comments

@Nicklas2751
Copy link

The generated model classes has neither a hash code nor an equals method. This leads to that when I wan't to test my endpoint implementations I always have to use a recursive comparison.

So it would be nice to be able to get the hashcode and equals method generated.

@hauner
Copy link
Member

hauner commented Apr 25, 2022

Not sure I want to generate equals() & hasCode(). Depends on the number of cases it needs to handle. If the array is the only special case it looks manageable. :)

having Objects only:

public boolean equals (Object o) {
    if (this == o)
        return true;

    if (o == null || getClass () != o.getClass ())
        return false;

    Reference reference = (Reference) o;
    return Objects.equals (ref, reference.ref)
        && Objects.equals (value, reference.value);
}

public int hashCode () {
    return Objects.hash (ref, value);
}

having an array:

public boolean equals (Object o) {
    if (this == o)
        return true;

    if (o == null || getClass () != o.getClass ())
        return false;

    Book book = (Book) o;
    return Objects.equals (id, book.id)
        && Objects.equals (title, book.title)
        && Arrays.equals (authors, book.authors);
}

public int hashCode () {
    int result = Objects.hash (id, title);
    result = 31 * result + Arrays.hashCode (authors);
    return result;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants