From b8fa8f3f5250e75014bc3ca208f150bf7b2b2cf7 Mon Sep 17 00:00:00 2001 From: Gavriil Sitnikov Date: Thu, 29 Jun 2017 19:06:56 +0300 Subject: [PATCH] equals and copy added --- Java/Class.java.twig | 36 ++++++++++++++++++- Java/blocks/class/fields-equals.twig | 5 +++ Java/blocks/class/fields-getters-setters.twig | 2 +- Java/blocks/class/fields-initialization.twig | 2 +- Java/utils.twig | 19 +++++++--- 5 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 Java/blocks/class/fields-equals.twig diff --git a/Java/Class.java.twig b/Java/Class.java.twig index 6f463fa..ead28a2 100644 --- a/Java/Class.java.twig +++ b/Java/Class.java.twig @@ -18,11 +18,45 @@ public class {% include 'blocks/class/classtype.twig' with { type: type } %} ext super(); } {% if (allFieldsOrdered is not empty) %} + //TODO: if TItems instance of arrayList then new ArrayList public {{ type.baseTypeName }}({%- include 'blocks/class/init-parameters-fields.twig' with { fields: allFieldsOrdered } -%}) { super({%- include 'blocks/class/fields-super-initialization.twig' with { fields: superclassesFields } -%}); - {%- include 'blocks/class/fields-initialization.twig' with { fields: fields } %} + {%- include 'blocks/class/fields-initialization.twig' with { fields: fields, object: "this" } %} } {% endif %} {%- include 'blocks/class/fields-getters-setters.twig' with { fields: fields } %} +{%- if parent is not null %} + @Override +{%- endif %} + protected void copyTo(@NonNull final {% include 'blocks/class/classtype.twig' with { type: type } %} destination) { + {%- if parent is not null %} + super.copyTo(destination); + {%- endif %} + {%- include 'blocks/class/fields-initialization.twig' with { fields: fields, object: "destination" } %} + } + + @NonNull + public {% include 'blocks/class/classtype.twig' with { type: type } %} copy() { + final {% include 'blocks/class/classtype.twig' with { type: type } %} result = new {% include 'blocks/class/classtype.twig' with { type: type } %}(); + this.copyTo(result); + return result; + } + + public int hashCode() { + return ObjectUtils.hashCode({%- if parent is not null -%}super.hashCode(), {% endif -%}{%- include 'blocks/class/fields-super-initialization.twig' with { fields: fields } -%}); + } + + public boolean equals(@Nullable final Object object) { + if (this == object) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } + final {% include 'blocks/class/classtype.twig' with { type: type } %} that = ({% include 'blocks/class/classtype.twig' with { type: type } %}) object; + return {% if parent is not null -%}super.equals(that) + && {% endif -%} + {%- include 'blocks/class/fields-equals.twig' with { fields: fields } -%}; + } } \ No newline at end of file diff --git a/Java/blocks/class/fields-equals.twig b/Java/blocks/class/fields-equals.twig new file mode 100644 index 0000000..17a5b91 --- /dev/null +++ b/Java/blocks/class/fields-equals.twig @@ -0,0 +1,5 @@ +{%- import '../../utils.twig' as utils -%} +{%- for field in fields -%} + {{ utils.formatEquals(field.name, field.type.type.baseTypeName, field.optional) }} {%- if not (loop.last) %} + && {% endif %} +{%- endfor -%} \ No newline at end of file diff --git a/Java/blocks/class/fields-getters-setters.twig b/Java/blocks/class/fields-getters-setters.twig index bbdb748..6f0b295 100644 --- a/Java/blocks/class/fields-getters-setters.twig +++ b/Java/blocks/class/fields-getters-setters.twig @@ -16,7 +16,7 @@ */ //TODO: setter with is**** public void set{{ utils.capitalize(field.name) }}({{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable, field.optional) }} final {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {{ field.name }}) { - {{ utils.formatValueSetter(field.name, field.type.type.baseTypeName, fiel.nullable, field.optional ) }} + {{ utils.formatValueSetter("this", field.name, field.type.type.baseTypeName, fiel.nullable, field.optional ) }} } {% endfor -%} {%- endif -%} \ No newline at end of file diff --git a/Java/blocks/class/fields-initialization.twig b/Java/blocks/class/fields-initialization.twig index 1414f84..186b726 100644 --- a/Java/blocks/class/fields-initialization.twig +++ b/Java/blocks/class/fields-initialization.twig @@ -1,4 +1,4 @@ {%- import '../../utils.twig' as utils -%} {%- for field in fields %} - {{ utils.formatValueSetter(field.name, field.type.type.baseTypeName, field.nullable, field.optional ) }} + {{ utils.formatValueSetter(object, field.name, field.type.type.baseTypeName, field.nullable, field.optional ) }} {%- endfor -%} \ No newline at end of file diff --git a/Java/utils.twig b/Java/utils.twig index 7f2bf5d..dae4470 100644 --- a/Java/utils.twig +++ b/Java/utils.twig @@ -85,14 +85,14 @@ new HashMap<>({{ fieldName }}) {%- endif -%} {% endmacro %} -{% macro formatValueSetter(fieldName, valueType, nullable, optional) %} +{% macro formatValueSetter(object, fieldName, valueType, nullable, optional) %} {% import _self as self %} {%- if optional -%} -this.{{ fieldName }} = new JsonOptional({{- self.formatNullableValueSetter(fieldName, valueType, nullable) -}}); +{{ object }}.{{ fieldName }} = new JsonOptional({{- self.formatNullableValueSetter(fieldName, valueType, nullable) -}}); {%- elseif nullable -%} -this.{{ fieldName }} = self.formatNullableValueSetter(fieldName, valueType, nullable) -}}; +{{ object }}.{{ fieldName }} = self.formatNullableValueSetter(fieldName, valueType, nullable) -}}; {%- else -%} -this.{{ fieldName }} = {{ fieldName }}; +{{ object }}.{{ fieldName }} = {{ fieldName }}; {%- endif -%} {% endmacro %} @@ -125,6 +125,17 @@ return this.{{ fieldName }}; {%- endif -%} {% endmacro %} +{% macro formatEquals(fieldName, valueType, optional) %} +{%- if valueType == "List" -%} +ObjectUtils.isCollectionsEquals({%- if optional -%}{{ fieldName }}.get(){%- else -%}{{ fieldName }}{%- endif -%}, that.{%- if optional -%}{{ fieldName }}.get(){%- else -%}{{ fieldName }}{%- endif -%}) +{%- elseif valueType == "Map" -%} +ObjectUtils.isMapsEquals({%- if optional -%}{{ fieldName }}.get(){%- else -%}{{ fieldName }}{%- endif -%}, that.{%- if optional -%}{{ fieldName }}.get(){%- else -%}{{ fieldName }}{%- endif -%}) +{%- else -%} +ObjectUtils.equals({%- if optional -%}{{ fieldName }}.get(){%- else -%}{{ fieldName }}{%- endif -%}, that.{%- if optional -%}{{ fieldName }}.get(){%- else -%}{{ fieldName }} +{%- endif -%}) +{%- endif -%} +{% endmacro %} + {% macro escapeIfNeeded(expr) %} {%- if expr == "default" -%} `{{ expr }}`