Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.
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

Description

@Nicklas2751

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.

Activity

  1. hauner commented on Apr 25, 2022

    @hauner
    Member

    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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions