
br.com.objectos.way.pojo.plugin.ConstructorStatementPojoProperty Maven / Gradle / Ivy
/*
* Copyright 2014-2015 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.pojo.plugin;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import br.com.objectos.way.code.Code;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.ParameterSpec;
/**
* @author [email protected] (Marcio Endo)
*/
class ConstructorStatementPojoProperty
extends PojoProperty
implements
PojoPropertyConstructorStatementBuilder,
PojoPropertyConstructorStatementBuilder.BuilderStatement {
private final Property property;
private final CodeBlock.Builder body = CodeBlock.builder();
public ConstructorStatementPojoProperty(Property property) {
this.property = property;
}
@Override
public PojoProperty build() {
return this;
}
@Override
public Add add(String statement) {
Objects.requireNonNull(statement);
return new AddPojo(statement);
}
@Override
public BuilderStatement assignment() {
body.addStatement("$L = builder.___get___$L()", property.name(), property.name());
return this;
}
@Override
public BuilderStatement builderGetAssignment() {
body.addStatement("$L = builder.___get___$L()", property.name(), property.name());
return this;
}
@Override
public BuilderStatement nullCheck() {
Optional maybeNullCheck = Code.nullCheck(property.typeName(), property.name());
if (maybeNullCheck.isPresent()) {
body.add(maybeNullCheck.get());
}
return this;
}
@Override
public BuilderStatement parameterAssignment() {
body.addStatement("this.$L = $L", property.name(), property.name());
return this;
}
@Override
void acceptConstructor(PojoConstructor constructor) {
constructor.addParameter(ParameterSpec.builder(property.typeName(), property.name()).build());
constructor.addCode(body.build());
}
private class AddPojo implements Add {
private final String statement;
private final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy