
br.com.objectos.way.flat.FlatEntityConstructor Maven / Gradle / Ivy
/*
* Copyright 2016 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.way.flat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import br.com.objectos.way.code.AccessInfo;
import br.com.objectos.way.code.ConstructorInfo;
import br.com.objectos.way.flat.FlatReader;
import br.com.objectos.way.pojo.plugin.PojoInfo;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.MethodSpec;
/**
* @author [email protected] (Marcio Endo)
*/
class FlatEntityConstructor {
private final List methodList;
private final int lastIndex;
private final List skipList = new ArrayList<>();
private int index;
private String propertyName;
private FlatEntityConstructor(List methodList, int lastIndex) {
this.methodList = methodList;
this.lastIndex = lastIndex;
}
public static FlatEntityConstructor of(PojoInfo pojoInfo, int size) {
return new FlatEntityConstructor(
pojoInfo.constructorInfoStream()
.map(Method::new)
.collect(Collectors.toList()),
size - 1);
}
public void add(String format, Object... args) {
prefix();
methodAdd(format, args);
methodAdd(";\n");
}
public List build() {
return methodList.stream()
.map(Method::build)
.collect(Collectors.toList());
}
public void propertyName(String name) {
propertyName = name;
}
public void setIndex(int index) {
this.index = index;
}
public void skip(int length) {
if (index != lastIndex) {
skipList.add(length);
}
}
private void prefix() {
methodAdd("$L = reader", propertyName);
for (Integer skip : skipList) {
methodAdd(".skip($L)", skip);
}
skipList.clear();
}
private void methodAdd(String format, Object... args) {
methodList.forEach(m -> m.add(format, args));
}
private static class Method {
private final ConstructorInfo constructor;
private final CodeBlock.Builder body = CodeBlock.builder();
public Method(ConstructorInfo constructor) {
this.constructor = constructor;
}
public void add(String format, Object... args) {
body.add(format, args);
}
public MethodSpec build() {
return constructor.constructorWriter()
.accessInfo(AccessInfo.PUBLIC)
.addParameterList()
.addParameter(FlatReader.class, "reader")
.addStandardSuperStatement()
.addCode(body.build())
.write();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy