equals and copy added

This commit is contained in:
Gavriil Sitnikov 2017-06-29 19:06:56 +03:00
parent 84044c3271
commit b8fa8f3f52
5 changed files with 57 additions and 7 deletions

View File

@ -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 } -%};
}
}

View File

@ -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 -%}

View File

@ -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 -%}

View File

@ -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 -%}

View File

@ -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 }}`