Skip to content

Commit

Permalink
Merge pull request #13 from Kaufland/bugfix/default-values-mapwrapper
Browse files Browse the repository at this point in the history
# added support for Default Values in MapWrapper Generation
  • Loading branch information
Sören Denninger authored Feb 15, 2019
2 parents 9b47315 + 8ae9e6d commit 359a2f4
Showing 1 changed file with 43 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import javax.lang.model.element.Modifier;

public class WrapperGeneration {

public JavaFile generateModel(WrapperEntityHolder holder) {

TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(holder.getEntitySimpleName()).
Expand Down Expand Up @@ -60,25 +60,29 @@ private List<MethodSpec> toMap(BaseEntityHolder holder) {
build();

return Arrays.asList(MethodSpec.methodBuilder("toMap").addModifiers(Modifier.PUBLIC, Modifier.STATIC).
addParameter(holder.getEntityTypeName(), "obj").
returns(TypeUtil.createMapStringObject()).
addCode(nullCheck).
addStatement("$T result = new $T()", TypeUtil.createHashMapStringObject(), TypeUtil.createHashMapStringObject()).
addStatement("result.putAll(obj.mDoc)").
addStatement("return result").
build(),

MethodSpec.methodBuilder("toMap").addModifiers(Modifier.PUBLIC, Modifier.STATIC).
addParameter(ParameterizedTypeName.get(ClassName.get(List.class), holder.getEntityTypeName()), "obj").
returns(TypeUtil.createListWithMapStringObject()).
addCode(nullCheck).
addStatement("$T result = new $T()", TypeUtil.createListWithMapStringObject(), TypeUtil.createArrayListWithMapStringObject()).
addCode(CodeBlock.builder().beginControlFlow("for($N entry : obj)", holder.getEntitySimpleName()).
addStatement("result.add((($N)entry).toMap(entry))", holder.getEntitySimpleName()).
endControlFlow().
build()).
addStatement("return result").
build());
addParameter(holder.getEntityTypeName(), "obj").
returns(TypeUtil.createMapStringObject()).
addCode(nullCheck).
addStatement("$T result = new $T()", TypeUtil.createHashMapStringObject(), TypeUtil.createHashMapStringObject())
.addStatement("result.putAll(mDocDefaults)").
addStatement("result.putAll(obj.mDoc)").
addStatement("return result").
build(),

MethodSpec.methodBuilder("toMap").addModifiers(Modifier.PUBLIC, Modifier.STATIC).
addParameter(ParameterizedTypeName.get(ClassName.get(List.class), holder.getEntityTypeName()), "obj").
returns(TypeUtil.createListWithMapStringObject()).
addCode(nullCheck).
addStatement("$T result = new $T()", TypeUtil.createListWithMapStringObject(), TypeUtil.createArrayListWithMapStringObject()).
addCode(CodeBlock.builder().beginControlFlow("for($N entry : obj)", holder.getEntitySimpleName()).
addStatement("$T temp = new $T()", TypeUtil.createHashMapStringObject(), TypeUtil.createHashMapStringObject()).
addStatement("temp.putAll(mDocDefaults)").
addStatement("temp.putAll((($N)entry).toMap(entry))", holder.getEntitySimpleName()).
addStatement("result.add(temp)", holder.getEntitySimpleName()).
endControlFlow().
build()).
addStatement("return result").
build());
}

private List<MethodSpec> fromMap(BaseEntityHolder holder) {
Expand All @@ -89,23 +93,23 @@ private List<MethodSpec> fromMap(BaseEntityHolder holder) {
build();

return Arrays.asList(MethodSpec.methodBuilder("fromMap").addModifiers(Modifier.PUBLIC, Modifier.STATIC).
addParameter(TypeUtil.createMapStringObject(), "obj").
returns(holder.getEntityTypeName()).
addCode(nullCheck).
addStatement("return new $T(obj)", holder.getEntityTypeName()).
build(),

MethodSpec.methodBuilder("fromMap").addModifiers(Modifier.PUBLIC, Modifier.STATIC).
addParameter(ParameterizedTypeName.get(ClassName.get(List.class), TypeUtil.createMapStringObject()), "obj").
returns(ParameterizedTypeName.get(ClassName.get(List.class), holder.getEntityTypeName())).
addCode(nullCheck).
addStatement("$T result = new $T()", ParameterizedTypeName.get(ClassName.get(List.class), holder.getEntityTypeName()), ParameterizedTypeName.get(ClassName.get(ArrayList.class), holder.getEntityTypeName())).
addCode(CodeBlock.builder().beginControlFlow("for($T entry : obj)", TypeUtil.createMapStringObject()).
addStatement("result.add(new $N(entry))", holder.getEntitySimpleName()).
endControlFlow().
build()).
addStatement("return result").
build()
addParameter(TypeUtil.createMapStringObject(), "obj").
returns(holder.getEntityTypeName()).
addCode(nullCheck).
addStatement("return new $T(obj)", holder.getEntityTypeName()).
build(),

MethodSpec.methodBuilder("fromMap").addModifiers(Modifier.PUBLIC, Modifier.STATIC).
addParameter(ParameterizedTypeName.get(ClassName.get(List.class), TypeUtil.createMapStringObject()), "obj").
returns(ParameterizedTypeName.get(ClassName.get(List.class), holder.getEntityTypeName())).
addCode(nullCheck).
addStatement("$T result = new $T()", ParameterizedTypeName.get(ClassName.get(List.class), holder.getEntityTypeName()), ParameterizedTypeName.get(ClassName.get(ArrayList.class), holder.getEntityTypeName())).
addCode(CodeBlock.builder().beginControlFlow("for($T entry : obj)", TypeUtil.createMapStringObject()).
addStatement("result.add(new $N(entry))", holder.getEntitySimpleName()).
endControlFlow().
build()).
addStatement("return result").
build()
);
}

Expand All @@ -123,13 +127,13 @@ private List<MethodSpec> create(WrapperEntityHolder holder) {
addModifiers(Modifier.PUBLIC, Modifier.STATIC).
addParameter(TypeUtil.createMapStringObject(), "doc").
addStatement("return new $N (doc)",
holder.getEntitySimpleName()).
holder.getEntitySimpleName()).
returns(holder.getEntityTypeName()).
build(),
MethodSpec.methodBuilder("create").
addModifiers(Modifier.PUBLIC, Modifier.STATIC).
addStatement("return new $N (new $T())",
holder.getEntitySimpleName(), TypeUtil.createHashMapStringObject()).
holder.getEntitySimpleName(), TypeUtil.createHashMapStringObject()).
returns(holder.getEntityTypeName()).
build()
);
Expand Down

0 comments on commit 359a2f4

Please sign in to comment.