Skip to content

Commit

Permalink
feat: add generation of equals and hashCode to the model (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenischev authored May 25, 2020
1 parent f45b69f commit 5b5df5b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ components:

|Name|Description|Required|Default|
|---|---|---|---|
|disableEqualsHashCode|Disable generation of equals and hashCode methods for model classes.|No|`false`|
|inverseOperations|Generate an application that will publish messages to `publish` operation of channels and read messages from `subscribe` operation of channels. Literally this flag will simply swap `publish` and `subscribe` operations in the channels. <br> This flag will be useful when you want to generate a code of mock for your main application. Be aware, generation could be incomplete and manual changes will be required e.g. if bindings are defined only for case of main application.|No|`false`|
|listenerPollTimeout|Only for Kafka. Timeout in ms to use when polling the consumer.|No|`3000`|
|listenerConcurrency|Only for Kafka. Number of threads to run in the listener containers.|No|`3`|
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
"default": false,
"required": false
},
"disableEqualsHashCode": {
"description": "Disable generation of equals and hashCode methods for model classes.",
"default": "false",
"required": false
},
"listenerPollTimeout": {
"description": "Only for Kafka. Timeout to use when polling the consumer.",
"default": 3000,
Expand Down
19 changes: 19 additions & 0 deletions template/src/main/java/com/asyncapi/model/$$message$$.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package {{ params['userJavaPackage'] }}.model;

import java.util.Objects;

{% if message.description() or message.examples()%}/**{% for line in message.description() | splitByLines %}
* {{ line | safe}}{% endfor %}{% if message.examples() %}
* Examples: {{message.examples() | examplesToString | safe}}{% endif %}
Expand All @@ -16,6 +18,23 @@ public void setPayload({{payloadName}} payload) {
this.payload = payload;
}

{% if params.disableEqualsHashCode === 'false' %}@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
{{messageName | camelCase | upperFirst}} event = ({{messageName | camelCase | upperFirst}}) o;
return Objects.equals(this.payload, event.payload);
}

@Override
public int hashCode() {
return Objects.hash(payload);
}{% endif %}

@Override
public String toString() {
return "class {{messageName | camelCase | upperFirst}} {\n" +
Expand Down
18 changes: 18 additions & 0 deletions template/src/main/java/com/asyncapi/model/$$schema$$.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Arrays;
import java.util.Objects;

{% if schema.description() or schema.examples() %}/**{% for line in schema.description() | splitByLines %}
Expand Down Expand Up @@ -109,6 +110,23 @@ public String toString() {
this.{{varName}} = {{varName}};
}
{% endfor %}
{% if params.disableEqualsHashCode === 'false' %}@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
{{schemaName | camelCase | upperFirst}} {{schemaName | camelCase}} = ({{schemaName | camelCase | upperFirst}}) o;
return {% for propName, prop in schema.properties() %}{% set varName = propName | camelCase %}{% if prop.type() === 'array' %}{% set varName = propName | camelCase + 'Array' %}{% endif %}
{% if prop.type() === 'array' %}Arrays{% else %}Objects{% endif %}.equals(this.{{varName}}, {{schemaName | camelCase}}.{{varName}}){% if not loop.last %} &&{% else %};{% endif %}{% endfor %}
}

@Override
public int hashCode() {
return Objects.hash({% for propName, prop in schema.properties() %}{{propName | camelCase}}{% if prop.type() === 'array' %}Array{% endif %}{% if not loop.last %}, {% endif %}{% endfor %});
}{% endif %}

@Override
public String toString() {
Expand Down

0 comments on commit 5b5df5b

Please sign in to comment.