Skip to content

Commit 4a54e18

Browse files
author
DielN
committed
Add equals and hashCode methods to microprofile
1 parent 66c7b2f commit 4a54e18

File tree

40 files changed

+841
-23
lines changed

40 files changed

+841
-23
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -149,27 +149,5 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}}{{#vendorExtensi
149149

150150
{{/vars}}
151151

152-
/**
153-
* Create a string representation of this pojo.
154-
*/
155-
@Override
156-
public String toString() {
157-
StringBuilder sb = new StringBuilder();
158-
sb.append("class {{classname}} {\n");
159-
{{#parent}}sb.append(" ").append(toIndentedString(super.toString())).append("\n");{{/parent}}
160-
{{#vars}}sb.append(" {{name}}: ").append({{#isPassword}}"*"{{/isPassword}}{{^isPassword}}toIndentedString({{name}}){{/isPassword}}).append("\n");
161-
{{/vars}}sb.append("}");
162-
return sb.toString();
163-
}
164-
165-
/**
166-
* Convert the given object to string with each line indented by 4 spaces
167-
* (except the first line).
168-
*/
169-
private static String toIndentedString(Object o) {
170-
if (o == null) {
171-
return "null";
172-
}
173-
return o.toString().replace("\n", "\n ");
174-
}
152+
{{>pojoOverrides}}
175153
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
@Override
2+
public boolean equals(Object o) {
3+
{{#useReflectionEqualsHashCode}}
4+
return EqualsBuilder.reflectionEquals(this, o, false, null, true);
5+
{{/useReflectionEqualsHashCode}}
6+
{{^useReflectionEqualsHashCode}}
7+
if (this == o) {
8+
return true;
9+
}
10+
if (o == null || getClass() != o.getClass()) {
11+
return false;
12+
}{{#hasVars}}
13+
{{classname}} {{classVarName}} = ({{classname}}) o;
14+
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}} &&
15+
{{/-last}}{{/vars}}{{#parent}} &&
16+
super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
17+
return {{#parent}}super.equals(o){{/parent}}{{^parent}}true{{/parent}};{{/hasVars}}
18+
{{/useReflectionEqualsHashCode}}
19+
}{{#vendorExtensions.x-jackson-optional-nullable-helpers}}
20+
21+
private static <T> boolean equalsNullable(JsonNullable<T> a, JsonNullable<T> b) {
22+
return a == b || (a != null && b != null && a.isPresent() && b.isPresent() && Objects.deepEquals(a.get(), b.get()));
23+
}{{/vendorExtensions.x-jackson-optional-nullable-helpers}}
24+
25+
@Override
26+
public int hashCode() {
27+
{{#useReflectionEqualsHashCode}}
28+
return HashCodeBuilder.reflectionHashCode(this);
29+
{{/useReflectionEqualsHashCode}}
30+
{{^useReflectionEqualsHashCode}}
31+
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}});
32+
{{/useReflectionEqualsHashCode}}
33+
}{{#vendorExtensions.x-jackson-optional-nullable-helpers}}
34+
35+
private static <T> int hashCodeNullable(JsonNullable<T> a) {
36+
if (a == null) {
37+
return 1;
38+
}
39+
return a.isPresent() ? Arrays.deepHashCode(new Object[]{a.get()}) : 31;
40+
}{{/vendorExtensions.x-jackson-optional-nullable-helpers}}
41+
42+
/**
43+
* Create a string representation of this pojo.
44+
*/
45+
@Override
46+
public String toString() {
47+
StringBuilder sb = new StringBuilder();
48+
sb.append("class {{classname}} {\n");
49+
{{#parent}}sb.append(" ").append(toIndentedString(super.toString())).append("\n");{{/parent}}
50+
{{#vars}}sb.append(" {{name}}: ").append({{#isPassword}}"*"{{/isPassword}}{{^isPassword}}toIndentedString({{name}}){{/isPassword}}).append("\n");
51+
{{/vars}}sb.append("}");
52+
return sb.toString();
53+
}
54+
55+
/**
56+
* Convert the given object to string with each line indented by 4 spaces
57+
* (except the first line).
58+
*/
59+
private static String toIndentedString(Object o) {
60+
if (o == null) {
61+
return "null";
62+
}
63+
return o.toString().replace("\n", "\n ");
64+
}

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Category.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ public Category name(String name) {
102102
}
103103

104104

105+
@Override
106+
public boolean equals(Object o) {
107+
if (this == o) {
108+
return true;
109+
}
110+
if (o == null || getClass() != o.getClass()) {
111+
return false;
112+
}
113+
Category category = (Category) o;
114+
return Objects.equals(this.id, category.id) &&
115+
Objects.equals(this.name, category.name);
116+
}
117+
118+
@Override
119+
public int hashCode() {
120+
return Objects.hash(id, name);
121+
}
122+
105123
/**
106124
* Create a string representation of this pojo.
107125
*/

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/ModelApiResponse.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,25 @@ public ModelApiResponse message(String message) {
134134
}
135135

136136

137+
@Override
138+
public boolean equals(Object o) {
139+
if (this == o) {
140+
return true;
141+
}
142+
if (o == null || getClass() != o.getClass()) {
143+
return false;
144+
}
145+
ModelApiResponse _apiResponse = (ModelApiResponse) o;
146+
return Objects.equals(this.code, _apiResponse.code) &&
147+
Objects.equals(this.type, _apiResponse.type) &&
148+
Objects.equals(this.message, _apiResponse.message);
149+
}
150+
151+
@Override
152+
public int hashCode() {
153+
return Objects.hash(code, type, message);
154+
}
155+
137156
/**
138157
* Create a string representation of this pojo.
139158
*/

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Order.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,28 @@ public Order complete(Boolean complete) {
266266
}
267267

268268

269+
@Override
270+
public boolean equals(Object o) {
271+
if (this == o) {
272+
return true;
273+
}
274+
if (o == null || getClass() != o.getClass()) {
275+
return false;
276+
}
277+
Order order = (Order) o;
278+
return Objects.equals(this.id, order.id) &&
279+
Objects.equals(this.petId, order.petId) &&
280+
Objects.equals(this.quantity, order.quantity) &&
281+
Objects.equals(this.shipDate, order.shipDate) &&
282+
Objects.equals(this.status, order.status) &&
283+
Objects.equals(this.complete, order.complete);
284+
}
285+
286+
@Override
287+
public int hashCode() {
288+
return Objects.hash(id, petId, quantity, shipDate, status, complete);
289+
}
290+
269291
/**
270292
* Create a string representation of this pojo.
271293
*/

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Pet.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,28 @@ public Pet status(StatusEnum status) {
294294
}
295295

296296

297+
@Override
298+
public boolean equals(Object o) {
299+
if (this == o) {
300+
return true;
301+
}
302+
if (o == null || getClass() != o.getClass()) {
303+
return false;
304+
}
305+
Pet pet = (Pet) o;
306+
return Objects.equals(this.id, pet.id) &&
307+
Objects.equals(this.category, pet.category) &&
308+
Objects.equals(this.name, pet.name) &&
309+
Objects.equals(this.photoUrls, pet.photoUrls) &&
310+
Objects.equals(this.tags, pet.tags) &&
311+
Objects.equals(this.status, pet.status);
312+
}
313+
314+
@Override
315+
public int hashCode() {
316+
return Objects.hash(id, category, name, photoUrls, tags, status);
317+
}
318+
297319
/**
298320
* Create a string representation of this pojo.
299321
*/

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Tag.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ public Tag name(String name) {
102102
}
103103

104104

105+
@Override
106+
public boolean equals(Object o) {
107+
if (this == o) {
108+
return true;
109+
}
110+
if (o == null || getClass() != o.getClass()) {
111+
return false;
112+
}
113+
Tag tag = (Tag) o;
114+
return Objects.equals(this.id, tag.id) &&
115+
Objects.equals(this.name, tag.name);
116+
}
117+
118+
@Override
119+
public int hashCode() {
120+
return Objects.hash(id, name);
121+
}
122+
105123
/**
106124
* Create a string representation of this pojo.
107125
*/

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/User.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,30 @@ public User userStatus(Integer userStatus) {
291291
}
292292

293293

294+
@Override
295+
public boolean equals(Object o) {
296+
if (this == o) {
297+
return true;
298+
}
299+
if (o == null || getClass() != o.getClass()) {
300+
return false;
301+
}
302+
User user = (User) o;
303+
return Objects.equals(this.id, user.id) &&
304+
Objects.equals(this.username, user.username) &&
305+
Objects.equals(this.firstName, user.firstName) &&
306+
Objects.equals(this.lastName, user.lastName) &&
307+
Objects.equals(this.email, user.email) &&
308+
Objects.equals(this.password, user.password) &&
309+
Objects.equals(this.phone, user.phone) &&
310+
Objects.equals(this.userStatus, user.userStatus);
311+
}
312+
313+
@Override
314+
public int hashCode() {
315+
return Objects.hash(id, username, firstName, lastName, email, password, phone, userStatus);
316+
}
317+
294318
/**
295319
* Create a string representation of this pojo.
296320
*/

samples/client/petstore/java/microprofile-rest-client-3.0-jackson/src/main/java/org/openapitools/client/model/Category.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ public Category name(String name) {
8787
}
8888

8989

90+
@Override
91+
public boolean equals(Object o) {
92+
if (this == o) {
93+
return true;
94+
}
95+
if (o == null || getClass() != o.getClass()) {
96+
return false;
97+
}
98+
Category category = (Category) o;
99+
return Objects.equals(this.id, category.id) &&
100+
Objects.equals(this.name, category.name);
101+
}
102+
103+
@Override
104+
public int hashCode() {
105+
return Objects.hash(id, name);
106+
}
107+
90108
/**
91109
* Create a string representation of this pojo.
92110
*/

samples/client/petstore/java/microprofile-rest-client-3.0-jackson/src/main/java/org/openapitools/client/model/ModelApiResponse.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ public ModelApiResponse message(String message) {
117117
}
118118

119119

120+
@Override
121+
public boolean equals(Object o) {
122+
if (this == o) {
123+
return true;
124+
}
125+
if (o == null || getClass() != o.getClass()) {
126+
return false;
127+
}
128+
ModelApiResponse _apiResponse = (ModelApiResponse) o;
129+
return Objects.equals(this.code, _apiResponse.code) &&
130+
Objects.equals(this.type, _apiResponse.type) &&
131+
Objects.equals(this.message, _apiResponse.message);
132+
}
133+
134+
@Override
135+
public int hashCode() {
136+
return Objects.hash(code, type, message);
137+
}
138+
120139
/**
121140
* Create a string representation of this pojo.
122141
*/

0 commit comments

Comments
 (0)