reggar / auto-value-ignore-hash-equals Goto Github PK
View Code? Open in Web Editor NEWAn extension for Google's AutoValue that omits fields from hashCode() & equals().
License: Apache License 2.0
An extension for Google's AutoValue that omits fields from hashCode() & equals().
License: Apache License 2.0
Same idea, but the other way around, something like @OnlyHashEquals or @HashEqualsId to only mark the fields to appear in the hashCode and equals. This would be really useful specially when dealing with models that already have an id field so you wouldn't have to put "@IgnoreHashEquals" in all other fields.
@REggar If you agree to include it here I could make a PR today :)
When you have a nested class annotated with @AutoValue
and @IgnoreHashEquals
or @IncludeHashEquals
the generated code doesn't compile.
Example:
public class Parent {
@AutoValue
static abstract class Test {
abstract String a();
@IgnoreHashEquals
abstract String b();
}
}
generates
final class AutoValue_Parent_Test extends $AutoValue_Parent_Test {
AutoValue_Parent_Test(String a, String b) {
super(a, b);
}
@Override
public final boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof Test) {
Test that = (Test) o;
return (this.a().equals(that.a()));
}
return false;
}
@Override
public final int hashCode() {
int h = 1;
h *= 1000003;
h ^= this.a().hashCode();
return h;
}
}
The problematic part is ... if (o instanceof Test) { Test that = (Test) o; ...
which has to be ... if (o instanceof Parent.Test) { Parent.Test that = (Parent.Test) o; ...
.
@REggar I will make a pull request fixing this issue if you don't object.
@AutoValue
public abstract class Request {
@IgnoreHashEquals public abstract int getId();
public static Builder builder() {
return new AutoValue_Request.Builder();
}
public abstract Builder toBuilder();
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setId(int newId);
public abstract Request build();
}
}
The above example generates an incorrect class. The toBuilder
method is mistaken for a field, but should be ignored.
abstract class $AutoValue_AgendaKey extends $$AutoValue_AgendaKey {
$AutoValue_AgendaKey(int tabIndex) {
super(tabIndex);
}
@Override
public final boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof AgendaKey) {
AgendaKey that = (AgendaKey) o;
return; // <-- this is the error
}
return false;
}
@Override
public final int hashCode() {
int h = 1;
return h;
}
}
using @IgnoreHashEquals
annotation, this is the generated file, and it does not compile. I have only 1 property in this object and it is ignored.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.