Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TDigest with custom equals and hashcode implementation #176

Open
nagarajatantry opened this issue Jul 1, 2021 · 2 comments
Open

TDigest with custom equals and hashcode implementation #176

nagarajatantry opened this issue Jul 1, 2021 · 2 comments

Comments

@nagarajatantry
Copy link

I have a use case to compare if 2 tdigest's are equal. Was wondering if it makes sense to override the equals and hashcode functions in MergingDigest class.

Sample code

 @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof MergingDigest)) {
            return false;
        }
        MergingDigest compare = (MergingDigest) o;

        if (compare.size() != this.size()){
            return false;
        }
        byte[] bBytes = new byte[compare.byteSize()];
        compare.asBytes(ByteBuffer.wrap(bBytes));

        byte[] aBytes = new byte[this.byteSize()];
        this.asBytes(ByteBuffer.wrap(aBytes));
        return Arrays.equals(aBytes,bBytes);
    }

    @Override
    public int hashCode() {
        byte[] aBytes = new byte[this.byteSize()];
        this.asBytes(ByteBuffer.wrap(aBytes));
        String sketch = Base64.getEncoder().encodeToString(aBytes);
        return new HashCodeBuilder(17, 37)
                .append(sketch)
                .toHashCode();
    }
@tdunning
Copy link
Owner

tdunning commented Jul 1, 2021 via email

@nagarajatantry
Copy link
Author

Thank you. I will send a PR

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

No branches or pull requests

2 participants