
br.com.objectos.flat.ReadMethodSpec 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.flat;
import java.util.List;
import java.util.stream.Collectors;
import javax.lang.model.element.Modifier;
import br.com.objectos.code.ConstructorInfo;
import br.com.objectos.code.ParameterInfo;
import br.com.objectos.core.util.MoreCollectors;
import br.com.objectos.pojo.plugin.Naming;
import br.com.objectos.pojo.plugin.PojoInfo;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.MethodSpec;
/**
* @author [email protected] (Marcio Endo)
*/
class ReadMethodSpec {
private final List methodList;
private boolean first = true;
private boolean lastWasSkip = false;
private ReadMethodSpec(List methodList) {
this.methodList = methodList;
}
public static ReadMethodSpec of(PojoInfo pojoInfo) {
Naming naming = pojoInfo.naming();
return new ReadMethodSpec(
pojoInfo.constructorInfoStream()
.map(c -> new Method(naming, c))
.collect(Collectors.toList()));
}
public void add(String format, Object... args) {
prefix();
methodAdd(format, args);
lastWasSkip = false;
}
public List build() {
return methodList.stream()
.map(Method::build)
.collect(Collectors.toList());
}
public void skip(int length) {
prefix();
methodAdd(".skip($L)", length);
lastWasSkip = true;
}
private void prefix() {
if (!first) {
if (!lastWasSkip) {
methodAdd(",");
}
} else {
first = false;
}
if (!lastWasSkip) {
methodAdd("\n reader");
}
}
private void methodAdd(String format, Object... args) {
methodList.forEach(m -> m.add(format, args));
}
private static class Method {
private final Naming naming;
private final ConstructorInfo constructor;
private final CodeBlock.Builder body = CodeBlock.builder();
public Method(Naming naming, ConstructorInfo constructor) {
this.naming = naming;
this.constructor = constructor;
}
public void add(String format, Object... args) {
body.add(format, args);
}
public MethodSpec build() {
return MethodSpec.methodBuilder("read")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameters(constructor.parameterInfoStream()
.map(ParameterInfo::parameterSpec)
.collect(MoreCollectors.toImmutableList()))
.addParameter(FlatReader.class, "reader")
.returns(naming.superClass())
.addCode("return new $T(", naming.pojo())
.addCode(param())
.addCode(body.build())
.addStatement(")")
.build();
}
private CodeBlock param() {
CodeBlock.Builder param = CodeBlock.builder();
constructor.parameterInfoStream()
.forEach(p -> param.add("\n $L,", p.name()));
return param.build();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy