-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add equals and hashCode methods to Microprofile (#20011)
* Add equals and hashCode methods to microprofile * Add missing import * Update samples * Add missing dependency for reflection equals
- Loading branch information
Showing
52 changed files
with
884 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
modules/openapi-generator/src/main/resources/Java/libraries/microprofile/model.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...s/openapi-generator/src/main/resources/Java/libraries/microprofile/pojoOverrides.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
@Override | ||
public boolean equals(Object o) { | ||
{{#useReflectionEqualsHashCode}} | ||
return EqualsBuilder.reflectionEquals(this, o, false, null, true); | ||
{{/useReflectionEqualsHashCode}} | ||
{{^useReflectionEqualsHashCode}} | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
}{{#hasVars}} | ||
{{classname}} {{classVarName}} = ({{classname}}) o; | ||
return {{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}equalsNullable(this.{{name}}, {{classVarName}}.{{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#isByteArray}}Arrays{{/isByteArray}}{{^isByteArray}}Objects{{/isByteArray}}.equals(this.{{name}}, {{classVarName}}.{{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}} && | ||
{{/-last}}{{/vars}}{{#parent}} && | ||
super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}} | ||
return {{#parent}}super.equals(o){{/parent}}{{^parent}}true{{/parent}};{{/hasVars}} | ||
{{/useReflectionEqualsHashCode}} | ||
}{{#vendorExtensions.x-jackson-optional-nullable-helpers}} | ||
|
||
private static <T> boolean equalsNullable(JsonNullable<T> a, JsonNullable<T> b) { | ||
return a == b || (a != null && b != null && a.isPresent() && b.isPresent() && Objects.deepEquals(a.get(), b.get())); | ||
}{{/vendorExtensions.x-jackson-optional-nullable-helpers}} | ||
|
||
@Override | ||
public int hashCode() { | ||
{{#useReflectionEqualsHashCode}} | ||
return HashCodeBuilder.reflectionHashCode(this); | ||
{{/useReflectionEqualsHashCode}} | ||
{{^useReflectionEqualsHashCode}} | ||
return Objects.hash({{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}hashCodeNullable({{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}}); | ||
{{/useReflectionEqualsHashCode}} | ||
}{{#vendorExtensions.x-jackson-optional-nullable-helpers}} | ||
|
||
private static <T> int hashCodeNullable(JsonNullable<T> a) { | ||
if (a == null) { | ||
return 1; | ||
} | ||
return a.isPresent() ? Arrays.deepHashCode(new Object[]{a.get()}) : 31; | ||
}{{/vendorExtensions.x-jackson-optional-nullable-helpers}} | ||
|
||
/** | ||
* Create a string representation of this pojo. | ||
*/ | ||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("class {{classname}} {\n"); | ||
{{#parent}}sb.append(" ").append(toIndentedString(super.toString())).append("\n");{{/parent}} | ||
{{#vars}}sb.append(" {{name}}: ").append({{#isPassword}}"*"{{/isPassword}}{{^isPassword}}toIndentedString({{name}}){{/isPassword}}).append("\n"); | ||
{{/vars}}sb.append("}"); | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* Convert the given object to string with each line indented by 4 spaces | ||
* (except the first line). | ||
*/ | ||
private static String toIndentedString(Object o) { | ||
if (o == null) { | ||
return "null"; | ||
} | ||
return o.toString().replace("\n", "\n "); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.