From 2599ca24114fa3b0d1fcd33aa5deefcf784738cb Mon Sep 17 00:00:00 2001 From: Denis Karmyshakov Date: Fri, 12 Oct 2018 11:58:55 +0300 Subject: [PATCH] Removed optional wrappers --- Java/Class.java.twig | 101 +++---- Java/Enum.java.twig | 4 +- Java/Methods.java.twig | 2 +- Java/blocks/class/converters.twig | 18 +- Java/blocks/class/fields-equals.twig | 5 +- Java/blocks/class/fields-getters-setters.twig | 43 +-- Java/blocks/class/fields-initialization.twig | 3 +- Java/blocks/class/fields-read-object.twig | 18 +- Java/blocks/class/fields-validate.twig | 42 ++- Java/blocks/class/fields-write-object.twig | 19 +- Java/blocks/class/fields.twig | 25 +- Java/blocks/class/init-parameters-fields.twig | 6 +- Java/blocks/class/type-parameters.twig | 4 +- Java/blocks/enum/converters.twig | 3 + Java/blocks/method/method-func.twig | 2 +- Java/utils.twig | 263 +----------------- 16 files changed, 194 insertions(+), 364 deletions(-) diff --git a/Java/Class.java.twig b/Java/Class.java.twig index 8fdd5ca..c3ab5bf 100644 --- a/Java/Class.java.twig +++ b/Java/Class.java.twig @@ -1,17 +1,15 @@ -{%- set hasParent = parent is not null -%} -/** +/* * This code is autogenerated by Touch Instinct tools */ package {{ packageName }}.api; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.util.ObjectsCompat; import com.bluelinelabs.logansquare.LoganSquare; import com.bluelinelabs.logansquare.annotation.JsonField; import com.bluelinelabs.logansquare.annotation.JsonObject; -import com.bluelinelabs.logansquare.JsonOptional; -import com.bluelinelabs.logansquare.NonNullJsonOptional; import java.io.IOException; import java.io.ObjectInputStream; @@ -55,7 +53,7 @@ import android.arch.persistence.room.TypeConverter; {%- if attributeWas -%}, {% endif %} {%- if (storageAttributes.primaryKeys is not empty) -%} {%- set attributeWas = true -%} - primaryKeys = { {%- for key in storageAttributes.primaryKeys -%} {%- if loop.index0 != 0 %}, {% endif -%} "{{- key.name -}}" {%- endfor -%} } + primaryKeys = { {%- for key in storageAttributes.primaryKeys -%} {%- if not (loop.last) %}, {% endif -%} "{{- key.name -}}" {%- endfor -%} } {%- endif -%} ) @TypeConverters({{ type.baseTypeName }}.class) @@ -68,67 +66,70 @@ public class {% include 'blocks/class/classtype.twig' with { type: type } %} ext {%- endif -%} {% include 'blocks/class/fields.twig' with { fields: fields } %} + public {{ type.baseTypeName }}() { super(); } -{% if (allFieldsOrdered is not empty) %} +{%- 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 } -%}) { + 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, object: "this" } %} } -{% endif %} -{%- include 'blocks/class/fields-getters-setters.twig' with { fields: fields } %} - //TODO: add check for collection if TypeParameter - //TODO: do not validate enums as ApiModel - @Override - public void validate() throws ValidationException { - super.validate(); - {%- include 'blocks/class/fields-validate.twig' with { fields: fields } %} - } +{%- endif %} +{% include 'blocks/class/fields-getters-setters.twig' with { fields: fields } %} + //TODO: add check for collection if TypeParameter + //TODO: do not validate enums as ApiModel + @Override + public void validate() throws ValidationException { + super.validate(); + {%- include 'blocks/class/fields-validate.twig' with { fields: fields } %} + } - 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" } %} - } + 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; - } + @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; + } {%- if (fields is not empty) %} - 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 int hashCode() { + return ObjectUtils.hashCode({%- if parent is not null -%}super.hashCode(), {% endif -%}{%- include 'blocks/class/fields-super-initialization.twig' with { fields: fields } -%}); + } - @SuppressWarnings("unchecked") 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 } -%}; - } + 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 } -%}; + } - private void writeObject(@NonNull final ObjectOutputStream outputStream) throws IOException { - {%- include 'blocks/class/fields-write-object.twig' with { fields: fields } %} - } + private void writeObject(@NonNull final ObjectOutputStream outputStream) throws IOException { + {%- include 'blocks/class/fields-write-object.twig' with { fields: fields } %} + } - @SuppressWarnings("unchecked") - private void readObject(@NonNull final ObjectInputStream inputStream) throws IOException, ClassNotFoundException { - {%- include 'blocks/class/fields-read-object.twig' with { fields: fields } %} - } + @SuppressWarnings("unchecked") + private void readObject(@NonNull final ObjectInputStream inputStream) throws IOException, ClassNotFoundException { + {%- include 'blocks/class/fields-read-object.twig' with { fields: fields } %} + } {%- endif %} diff --git a/Java/Enum.java.twig b/Java/Enum.java.twig index 810a2cf..ac3ef1f 100644 --- a/Java/Enum.java.twig +++ b/Java/Enum.java.twig @@ -1,4 +1,4 @@ -/** +/* * This code is autogenerated by Touch Instinct tools */ package {{ packageName }}.api; @@ -32,7 +32,5 @@ import android.arch.persistence.room.TypeConverter; public enum {{ name }} { {%- include 'blocks/enum/cases.twig' with { values: values } %} - {%- if storable %} {%- include 'blocks/enum/converters.twig' %} - {%- endif %} } diff --git a/Java/Methods.java.twig b/Java/Methods.java.twig index 89d1c71..86fcdab 100644 --- a/Java/Methods.java.twig +++ b/Java/Methods.java.twig @@ -1,4 +1,4 @@ -/** +/* * This code is autogenerated by Touch Instinct tools */ package {{ packageName }}.api; diff --git a/Java/blocks/class/converters.twig b/Java/blocks/class/converters.twig index 6b10d1f..87fb1e8 100644 --- a/Java/blocks/class/converters.twig +++ b/Java/blocks/class/converters.twig @@ -5,7 +5,7 @@ @TypeConverter @Nullable - public static String serialize{{ utils.capitalize(field.name) }}(@Nullable final List<{{ utils.formatNonOptionalValueType(field.type.type.itemsType, false) }}> value) { + public static String serialize{{ capitalize(field.name) }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { if (value == null) { return null; } @@ -18,12 +18,12 @@ @TypeConverter @Nullable - public static List<{{ utils.formatNonOptionalValueType(field.type.type.itemsType, false) }}> deserialize{{ utils.capitalize(field.name) }}(@Nullable final String value) { + public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ capitalize(field.name) }}(@Nullable final String value) { if (value == null) { return null; } try { - return LoganSquare.parseList(value, {{ utils.formatNonOptionalValueType(field.type.type.itemsType, false) }}.class); + return LoganSquare.parseList(value, {{ utils.formatValueType(field.type.type.itemsType, true) }}.class); } catch (final IOException exception) { return null; } @@ -32,7 +32,7 @@ @TypeConverter @Nullable - public static String serialize{{ utils.capitalize(field.name) }}(@Nullable final Map value) { + public static String serialize{{ capitalize(field.name) }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { if (value == null) { return null; } @@ -45,12 +45,12 @@ @TypeConverter @Nullable - public static Map deserialize{{ utils.capitalize(field.name) }}(@Nullable final String value) { + public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ capitalize(field.name) }}(@Nullable final String value) { if (value == null) { return null; } try { - return LoganSquare.parseMap(value, {{ utils.formatNonOptionalValueType(field.type.type.valuesType, false) }}.class); + return LoganSquare.parseMap(value, {{ utils.formatValueType(field.type.type.valuesType, true) }}.class); } catch (final IOException exception) { return null; } @@ -64,7 +64,7 @@ @TypeConverter @Nullable - public static String serialize{{ utils.capitalize(field.name) }}(@Nullable final {{ utils.formatNonOptionalValueType(field.type.type, false) }} value) { + public static String serialize{{ capitalize(field.name) }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { if (value == null) { return null; } @@ -77,12 +77,12 @@ @TypeConverter @Nullable - public static {{ utils.formatNonOptionalValueType(field.type.type, false) }} deserialize{{ utils.capitalize(field.name) }}(@Nullable final String value) { + public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ capitalize(field.name) }}(@Nullable final String value) { if (value == null) { return null; } try { - return LoganSquare.parse(value, {{ utils.formatNonOptionalValueType(field.type.type, false) }}.class); + return LoganSquare.parse(value, {{ utils.formatValueType(field.type.type, true) }}.class); } catch (final IOException exception) { return null; } diff --git a/Java/blocks/class/fields-equals.twig b/Java/blocks/class/fields-equals.twig index 6bd2dd9..53f3096 100644 --- a/Java/blocks/class/fields-equals.twig +++ b/Java/blocks/class/fields-equals.twig @@ -1,5 +1,4 @@ -{%- import '../../utils.twig' as utils -%} {%- for field in fields -%} - {{ utils.formatEquals(field.name, field.type.type.baseTypeName, field.optional) }} {%- if not (loop.last) %} - && {% endif %} + ObjectsCompat.equals(this.{{ field.name }}, that.{{ field.name }}) {%- if not (loop.last) %} + && {% endif %} {%- endfor -%} diff --git a/Java/blocks/class/fields-getters-setters.twig b/Java/blocks/class/fields-getters-setters.twig index ecd3e3d..a88c58d 100644 --- a/Java/blocks/class/fields-getters-setters.twig +++ b/Java/blocks/class/fields-getters-setters.twig @@ -1,24 +1,33 @@ {%- import '../../utils.twig' as utils -%} -{%- if fields is not empty -%} {%- for field in fields %} - {% if (field.description is not empty) %} + {%- if (field.description is not empty) %} /** - * {{ field.description }} - */ - {% endif -%} - {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable, field.optional) }} - public {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {% if (field.type.type.baseTypeName == "Bool") and (field.name matches "^is[A-Z,0-9].*") -%}{{ field.name }}{%- else -%}get{{ utils.capitalize(field.name) }}{%- endif -%}() { - {{ utils.formatValueGetter(field.name, field.type.type, field.nullable) }} - } + * {{ field.description }} + */ + {%- endif %} + {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }} + public {{ utils.formatValueType(field.type.type, field.nullable) }} {% if (field.type.type.baseTypeName == "Bool") and (field.name matches "^is[A-Z,0-9].*") -%}{{ field.name }}{%- else -%}get{{ capitalize(field.name) }}{%- endif -%}() { + {%- if field.nullable %} + if ({{ fieldName }} == null) { + return null; + } + {%- endif %} + {%- if field.type.type == "List" %} + return Collections.unmodifiableList(this.{{ field.name }}); + {%- elseif field.type.type == "Map" %} + return Collections.unmodifiableMap(this.{{ field.name }}); + {%- else %} + return this.{{ field.name }}; + {%- endif %} + } + {%- if (field.description is not empty) %} - {% if (field.description is not empty) %} /** - * {{ field.description }} - */ - {% endif -%} - 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("this", field.name, field.type.type.baseTypeName, field.nullable ) }} - } + * {{ field.description }} + */ + {%- endif %} + public void set{{ capitalize(field.name) }}({{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }} final {{ utils.formatValueType(field.type.type, field.nullable) }} {{ field.name }}) { + this.{{ field.name }} = {{ field.name }}; + } {% endfor -%} -{%- endif -%} diff --git a/Java/blocks/class/fields-initialization.twig b/Java/blocks/class/fields-initialization.twig index fe31d4a..779323e 100644 --- a/Java/blocks/class/fields-initialization.twig +++ b/Java/blocks/class/fields-initialization.twig @@ -1,4 +1,3 @@ -{%- import '../../utils.twig' as utils -%} {%- for field in fields %} - {{ utils.formatValueSetter(object, field.name, field.type.type.baseTypeName, field.nullable ) }} + {{ object }}.{{ field.name }} = {{ field.name }}; {%- endfor -%} diff --git a/Java/blocks/class/fields-read-object.twig b/Java/blocks/class/fields-read-object.twig index 866a9b6..e0d994c 100644 --- a/Java/blocks/class/fields-read-object.twig +++ b/Java/blocks/class/fields-read-object.twig @@ -1,4 +1,18 @@ {%- import '../../utils.twig' as utils -%} {% for field in fields %} - this.{{ field.name }} = {{ utils.formatReadObject(field.type.type, field.nullable, field.optional) }}; -{%- endfor -%} \ No newline at end of file + {%- if field.nullable %} + this.{{ field.name }} = ({{ utils.formatValueType(field.type.type, field.nullable) }}) inputStream.readObject(); + {%- elseif field.type.type.baseTypeName == "Int" %} + this.{{ field.name }} = inputStream.readInt(); + {%- elseif field.type.type.baseTypeName == "Long" %} + this.{{ field.name }} = inputStream.readLong(); + {%- elseif field.type.type.baseTypeName == "Double" %} + this.{{ field.name }} = inputStream.readDouble(); + {%- elseif field.type.type.baseTypeName == "String" %} + this.{{ field.name }} = inputStream.readUTF(); + {%- elseif field.type.type.baseTypeName == "Bool" %} + this.{{ field.name }} = inputStream.readBoolean(); + {%- else %} + this.{{ field.name }} = ({{ utils.formatValueType(field.type.type, field.nullable) }}) inputStream.readObject(); + {%- endif -%} +{%- endfor -%} diff --git a/Java/blocks/class/fields-validate.twig b/Java/blocks/class/fields-validate.twig index 4dff2f9..bdd245e 100644 --- a/Java/blocks/class/fields-validate.twig +++ b/Java/blocks/class/fields-validate.twig @@ -1,4 +1,42 @@ {%- import '../../utils.twig' as utils -%} -{% for field in fields -%} - {{ utils.formatValidateObject(concat("this.", field.name), field.type, field.type.type, field.nullable, field.optional) }} +{%- for field in fields %} + {%- if not (field.nullable) + and field.type.type.baseTypeName != "Int" + and field.type.type.baseTypeName != "Long" + and field.type.type.baseTypeName != "Double" + and field.type.type.baseTypeName != "Bool" %} + validateNotNull(this.{{ field.name }}); + {%- endif %} + {%- if field.type.type.baseTypeName == "Map" %} + {%- if field.nullable %} + if (this.{{ field.name }} != null) { + validateCollection(this.{{ field.name }}.values(), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); + } + {%- else %} + validateCollection(this.{{ field.name }}.values(), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); + {%- endif %} + {%- elseif field.type.type.baseTypeName == "Array" %} + {%- if field.nullable %} + if (this.{{ field.name }} != null) { + validateCollection(this.{{ field.name }}, CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); + } + {%- else %} + validateCollection(this.{{ field.name }}, CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); + {%- endif %} + {%- elseif field.type.type.baseTypeName != "Int" + and field.type.type.baseTypeName != "Long" + and field.type.type.baseTypeName != "Double" + and field.type.type.baseTypeName != "Bool" + and field.type.type.baseTypeName != "String" + and field.type.type.baseTypeName != "Decimal" + and field.type.type.baseTypeName != "DateTime" + and (field.type.values is empty) %} + {%- if field.nullable %} + if (this.{{ field.name }} != null) { + this.{{ field.name }}.validate(); + } + {%- else %} + this.{{ field.name }}.validate(); + {%- endif -%} + {%- endif -%} {%- endfor -%} diff --git a/Java/blocks/class/fields-write-object.twig b/Java/blocks/class/fields-write-object.twig index 3c19a2a..1810561 100644 --- a/Java/blocks/class/fields-write-object.twig +++ b/Java/blocks/class/fields-write-object.twig @@ -1,4 +1,17 @@ -{%- import '../../utils.twig' as utils -%} {% for field in fields %} - outputStream.{{ utils.formatWriteObject(field.name, field.type.type, field.nullable, field.optional) }}; -{%- endfor -%} \ No newline at end of file + {%- if field.nullable %} + outputStream.writeObject(this.{{ field.name }}); + {%- elseif field.type.type.baseTypeName == "Int" %} + outputStream.writeInt(this.{{ field.name }}); + {%- elseif field.type.type.baseTypeName == "Long" %} + outputStream.writeLong(this.{{ field.name }}); + {%- elseif field.type.type.baseTypeName == "Double" %} + outputStream.writeDouble(this.{{ field.name }}); + {%- elseif field.type.type.baseTypeName == "String" %} + outputStream.writeUTF(this.{{ field.name }}); + {%- elseif field.type.type.baseTypeName == "Bool" %} + outputStream.writeBoolean(this.{{ field.name }}); + {%- else %} + outputStream.writeObject(this.{{ field.name }}); + {%- endif -%} +{%- endfor -%} diff --git a/Java/blocks/class/fields.twig b/Java/blocks/class/fields.twig index 6656acb..efdd7f6 100644 --- a/Java/blocks/class/fields.twig +++ b/Java/blocks/class/fields.twig @@ -1,17 +1,16 @@ {%- import '../../utils.twig' as utils -%} -{%- if fields is not empty -%} -{% for field in fields %} - {% if (field.description is not empty) %} - /** - * {{ field.description }} - */ - {% endif -%} - {% if (storageAttributes is not null) and field.type.storable %} +{%- for field in fields %} + {%- if (field.description is not empty) %} + + /** + * {{ field.description }} + */ + {%- endif %} + {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }} + {%- if (storageAttributes is not null) and field.type.storable %} @TypeConverters({{ field.type.type.baseTypeName }}.class) - {% endif -%} + {%- endif %} @JsonField(name = "{{ field.jsonName }}") - {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable, field.optional) }} - private {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {{ field.name }} {%- if field.optional %} = {{ utils.formatOptionalValueType(field.type.type, field.nullable) }}.empty(){% endif %}; -{% endfor -%} -{% endif %} + private {{ utils.formatValueType(field.type.type, field.nullable) }} {{ field.name }}; +{%- endfor -%} diff --git a/Java/blocks/class/init-parameters-fields.twig b/Java/blocks/class/init-parameters-fields.twig index c65bd3c..977caae 100644 --- a/Java/blocks/class/init-parameters-fields.twig +++ b/Java/blocks/class/init-parameters-fields.twig @@ -1,7 +1,7 @@ {%- import '../../utils.twig' as utils -%} {%- if fields is not empty -%} -{%- for field in fields -%} - {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable, field.optional) }} final {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {{ field.name }} {%- if not (loop.last) %}, {% endif %} +{%- for field in fields %} + {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }} final {{ utils.formatValueType(field.type.type, field.nullable) }} {{ field.name }} {%- if not (loop.last) %}, {% endif %} {%- endfor -%} -{%- endif -%} \ No newline at end of file +{%- endif -%} diff --git a/Java/blocks/class/type-parameters.twig b/Java/blocks/class/type-parameters.twig index 2668317..c1eefc9 100644 --- a/Java/blocks/class/type-parameters.twig +++ b/Java/blocks/class/type-parameters.twig @@ -2,7 +2,7 @@ {%- if typeParameters is not empty -%} < {%- for typeParameter in typeParameters %} -{{- utils.formatSimpleValueType(typeParameter) -}}{%- if not (loop.last) %}, {% endif %} +{{- utils.formatValueType(typeParameter, true) -}}{%- if not (loop.last) %}, {% endif %} {%- endfor -%} > -{%- endif -%} \ No newline at end of file +{%- endif -%} diff --git a/Java/blocks/enum/converters.twig b/Java/blocks/enum/converters.twig index b22afae..60d8b5e 100644 --- a/Java/blocks/enum/converters.twig +++ b/Java/blocks/enum/converters.twig @@ -1,4 +1,5 @@ +{%- if storable %} @TypeConverter @Nullable @@ -17,3 +18,5 @@ } return {{ name }}.valueOf(value); } + +{%- endif %} diff --git a/Java/blocks/method/method-func.twig b/Java/blocks/method/method-func.twig index bb8c8c6..1b497ec 100644 --- a/Java/blocks/method/method-func.twig +++ b/Java/blocks/method/method-func.twig @@ -4,4 +4,4 @@ */ @NonNull @POST("{{ method.url }}") - Single> {{ utils.decapitalize(method.name) }}(@NonNull @Body {{ method.bodyType.type.typeName }} {{ utils.decapitalize(method.bodyType.type.typeName) }}); \ No newline at end of file + Single> {{ lower(method.name) }}(@NonNull @Body {{ method.bodyType.type.typeName }} {{ lower(method.bodyType.type.typeName) }}); diff --git a/Java/utils.twig b/Java/utils.twig index 223f066..072da38 100644 --- a/Java/utils.twig +++ b/Java/utils.twig @@ -1,19 +1,3 @@ -{% macro decapitalize(text) %} -{{- concat(slice(text, 0, 1) | lower, slice(text, 1, text | length)) -}} -{% endmacro %} - -{% macro capitalize(text) %} -{{- concat(slice(text, 0, 1) | upper, slice(text, 1, text | length)) -}} -{% endmacro %} - -{% macro enumType(valuesTypes) %} -{%- if valuesTypes == "STRING" -%} -String -{%- elseif valuesTypes == "INT" -%} -Int -{%- endif -%} -{% endmacro %} - {% macro parentClassType(parent) %} {%- if parent is not null %} {{- parent.type.baseTypeName -}} @@ -22,258 +6,31 @@ LoganSquareJsonModel {%- endif -%} {% endmacro %} -{% macro writeNullCheckAnnotation(baseTypeName, nullable, optional) %} -{%- if optional -%} -@NonNull -{%- elseif nullable -%} +{% macro writeNullCheckAnnotation(baseTypeName, nullable) %} +{%- if nullable -%} @Nullable {%- elseif not (baseTypeName == "Int" or baseTypeName == "Long" or baseTypeName == "Double" or baseTypeName == "Bool") -%} @NonNull {%- endif -%} {% endmacro %} -{% macro formatNonOptionalValueType(valueType, tryUsePrimitive) %} +{% macro formatValueType(valueType, nullable) %} {% import _self as self %} {%- if valueType.baseTypeName == "Int" -%} -{%- if tryUsePrimitive -%}int{%- else -%}Integer{%- endif -%} +{%- if not nullable -%}int{%- else -%}Integer{%- endif -%} {%- elseif valueType.baseTypeName == "Long" -%} -{%- if tryUsePrimitive -%}long{%- else -%}Long{%- endif -%} +{%- if not nullable -%}long{%- else -%}Long{%- endif -%} {%- elseif valueType.baseTypeName == "Double" -%} -{%- if tryUsePrimitive -%}double{%- else -%}Double{%- endif -%} +{%- if not nullable -%}double{%- else -%}Double{%- endif -%} +{%- elseif valueType.baseTypeName == "Bool" -%} +{%- if not nullable -%}boolean{%- else -%}Boolean{%- endif -%} {%- elseif valueType.baseTypeName == "Decimal" -%} BigDecimal -{%- elseif valueType.baseTypeName == "Bool" -%} -{%- if tryUsePrimitive -%}boolean{%- else -%}Boolean{%- endif -%} {%- elseif valueType.baseTypeName == "Map" -%} -Map<{{ self.formatNonOptionalValueType(valueType.keysType, false) }}, {{ self.formatNonOptionalValueType(valueType.valuesType, false) }}> +Map<{{ self.formatValueType(valueType.keysType, true) }}, {{ self.formatValueType(valueType.valuesType, true) }}> {%- elseif valueType.baseTypeName == "Array" -%} -List<{{ self.formatNonOptionalValueType(valueType.itemsType, false) }}> +List<{{ self.formatValueType(valueType.itemsType, true) }}> {%- else -%} {{ valueType.baseTypeName }} {%- endif -%} {% endmacro %} - -{% macro formatOptionalValueType(valueType, nullable) %} - {% import _self as self %} - {%- if nullable -%} JsonOptional {%- else -%} NonNullJsonOptional {%- endif -%} -{% endmacro %} - -{% macro formatValueType(valueType, nullable, optional) %} - {% import _self as self %} - {%- if optional -%} - {{ self.formatOptionalValueType(valueType, nullable) }}<{{ self.formatNonOptionalValueType(valueType, false) }}> - {%- else -%} - {{ self.formatNonOptionalValueType(valueType, not nullable) }} - {%- endif -%} -{% endmacro %} - -{% macro formatSimpleValueType(valueType) %} -{%- import _self as self -%} -{{- self.formatValueType(valueType, true, false) -}} -{% endmacro %} - -{% macro formatNullableValueSetter(fieldName, valueType, nullable) %} -{%- if valueType == "List" -%} -{%- if nullable -%} -{{ fieldName }} != null ? new ArrayList<>({{ fieldName }}) : null -{%- else -%} -new ArrayList<>({{ fieldName }}) -{%- endif -%} -{%- elseif valueType == "Map" -%} -{%- if nullable -%} -{{ fieldName }} != null ? new HashMap<>({{ fieldName }}) : null -{%- else -%} -new HashMap<>({{ fieldName }}) -{%- endif -%} -{%- else -%} -{{ fieldName }} -{%- endif -%} -{% endmacro %} - -{% macro formatValueSetter(object, fieldName, valueType, nullable) %} - {% import _self as self %} - {%- if nullable -%} - {{ object }}.{{ fieldName }} = {{ self.formatNullableValueSetter(fieldName, valueType, nullable)}}; - {%- else -%} - {{ object }}.{{ fieldName }} = {{ fieldName }}; - {%- endif -%} -{% endmacro %} - -{% macro formatNullableValueGetter(fieldName, valueType, nullable) %} -{%- if valueType == "List" -%} -{%- if nullable -%} -{{ fieldName }} != null ? Collections.unmodifiableList({{ fieldName }}) : null -{%- else -%} -Collections.unmodifiableList({{ fieldName }}) -{%- endif -%} -{%- elseif valueType == "Map" -%} -{%- if nullable -%} -{{ fieldName }} != null ? Collections.unmodifiableMap({{ fieldName }}) : null -{%- else -%} -Collections.unmodifiableMap({{ fieldName }}) -{%- endif -%} -{%- else -%} -{{ fieldName }} -{%- endif -%} -{% endmacro %} - -{% macro formatValueGetter(fieldName, valueType, nullable) %} -{% import _self as self %} - {%- if nullable -%} - return {{ self.formatNullableValueGetter(fieldName, valueType, nullable) }}; - {%- else -%} - return this.{{ fieldName }}; - {%- endif -%} -{% endmacro %} - -{% macro formatEquals(fieldName, valueType, optional) %} - {%- if valueType == "List" -%} - ObjectUtils.isCollectionsEquals({{ fieldName }}{%- if optional -%} .get() {%- endif -%}, that.{{ fieldName }}{%- if optional -%} .get() {%- endif -%}) - {%- elseif valueType == "Map" -%} - ObjectUtils.isMapsEquals({{ fieldName }}{%- if optional -%} .get() {%- endif -%}, that.{{ fieldName }}{%- if optional -%} .get() {%- endif -%}) - {%- else -%} - ObjectUtils.equals({{ fieldName }}, that.{{ fieldName }}) - {%- endif -%} -{% endmacro %} - -{% macro escapeIfNeeded(expr) %} -{%- if expr == "default" -%} -`{{ expr }}` -{%- else -%} -{{ expr }} -{%- endif -%} -{% endmacro %} - -{% macro mappingFromMapForField(field) %} -{%- if field.type.type.baseTypeName == "DateTime" -%} -map.value("{{ field.jsonName }}", using: {{ field.name -}}Transform) -{%- else -%} -map.value("{{ field.jsonName }}") -{%- endif -%} -{% endmacro %} - -{% macro mappingToMapForField(field) %} -{%- if field.type.type.baseTypeName == "DateTime" -%} -(map["{{ field.jsonName }}"], {{ field.name -}}Transform) -{%- else -%} -map["{{ field.jsonName }}"] -{%- endif -%} -{% endmacro %} - -{% macro formatWriteObject(fieldName, valueType, nullable, optional) %} -{%- if nullable or optional -%} -writeObject({{ fieldName }}) -{%- else -%} -{%- if valueType.baseTypeName == "Int" -%} -writeInt({{ fieldName }}) -{%- elseif valueType.baseTypeName == "Long" -%} -writeLong({{ fieldName }}) -{%- elseif valueType.baseTypeName == "Double" -%} -writeDouble({{ fieldName }}) -{%- elseif valueType.baseTypeName == "String" -%} -writeUTF({{ fieldName }}) -{%- elseif valueType.baseTypeName == "Bool" -%} -writeBoolean({{ fieldName }}) -{%- else -%} -writeObject({{ fieldName }}) -{%- endif -%} -{%- endif -%} -{% endmacro %} - -{% macro formatReadObject(valueType, nullable, optional) %} -{% import _self as self %} -{%- if nullable or optional -%} -({{ self.formatValueType(valueType, nullable, optional) }}) inputStream.readObject() -{%- else -%} -{%- if valueType.baseTypeName == "Int" -%} -inputStream.readInt() -{%- elseif valueType.baseTypeName == "Long" -%} -inputStream.readLong() -{%- elseif valueType.baseTypeName == "Double" -%} -inputStream.readDouble() -{%- elseif valueType.baseTypeName == "String" -%} -inputStream.readUTF() -{%- elseif valueType.baseTypeName == "Bool" -%} -inputStream.readBoolean() -{%- else -%} -({{ self.formatValueType(valueType, nullable, optional) }}) inputStream.readObject() -{%- endif -%} -{%- endif -%} -{% endmacro %} - -{% macro formatValidateObject(object, valueTopType, valueType, nullable, optional) %} - {% import _self as self %} - {%- if not nullable - and valueType.baseTypeName != "Int" - and valueType.baseTypeName != "Long" - and valueType.baseTypeName != "Double" - and valueType.baseTypeName != "Bool" %} - {%- if optional %} - if (!{{ object }}.isEmpty()) { - validateNotNull({{ object }}.get()); - } - {%- else %} - validateNotNull({{ object }}); - {%- endif -%} - {%- endif -%} - {%- if valueType.baseTypeName == "Map" %} - {%- if optional %} - if ({{ object }}.get() != null) { - validateCollection({{ object }}.get().values(), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); - } - {%- else -%} - - {%- if nullable %} - if ({{ object }} != null) { - validateCollection({{ object }}.values(), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); - } - {%- else %} - validateCollection({{ object }}.values(), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); - {%- endif -%} - - {%- endif -%} - {%- elseif valueType.baseTypeName == "Array" %} - {%- if optional %} - if ({{ object }}.get() != null) { - validateCollection({{ object }}.get(), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); - } - {%- else %} - - {%- if nullable %} - if ({{ object }} != null) { - validateCollection({{ object }}, CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); - } - {%- else %} - validateCollection({{ object }}, CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); - {%- endif -%} - - {%- endif -%} - {%- elseif valueType.baseTypeName != "Int" - and valueType.baseTypeName != "Long" - and valueType.baseTypeName != "Double" - and valueType.baseTypeName != "Bool" - and valueType.baseTypeName != "String" - and valueType.baseTypeName != "Decimal" - and valueType.baseTypeName != "DateTime" - and (valueTopType.values is empty) %} - {%- if valueTopType.allFieldsOrdered is empty %} - if ({{ object }}{%- if optional -%} .get() {% endif %} instanceof ApiModel) { - ((ApiModel) {{ object }}{%- if optional -%} .get() {%- endif -%}).validate(); - } else if ({{ object }} instanceof List) { - validateCollection(((List) {{ object }}), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); - } else if ({{ object }} instanceof Map) { - validateCollection(((Map) {{ object }}).values(), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); - } - {%- elseif optional %} - if ({{ object }}.get() != null) { - {{ object }}.get().validate(); - } - {%- elseif nullable %} - if ({{ object }} != null) { - {{ object }}.validate(); - } - {%- else -%} - {{ object }}.validate(); - {%- endif -%} - {%- endif -%} -{% endmacro %}