
br.com.objectos.code.pojo.BuilderInterface Maven / Gradle / Ivy
The newest version!
/*
* Copyright 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.code.pojo;
import static com.google.common.collect.Lists.newArrayListWithCapacity;
import java.util.List;
import javax.lang.model.element.Modifier;
import com.google.common.collect.ImmutableList;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
/**
* @author [email protected] (Marcio Endo)
*/
class BuilderInterface extends AbstractHasPojoInfo {
public BuilderInterface(PojoInfo info) {
super(info);
}
public JavaFile generate(AnnotationSpec annotationSpec) {
return generate(type(annotationSpec));
}
List firstMethod() {
List methodList = builderMethodList();
return methodList.isEmpty()
? build()
: middle(methodList.get(0));
}
List inner() {
List methodList = builderMethodList();
int index = 0;
int size = methodList.size();
List typeSpecList = newArrayListWithCapacity(size);
for (AttributeMethod method : methodList) {
String innerSimpleName = naming().builderInnerSimpleName(method);
TypeSpec.Builder inner = TypeSpec.interfaceBuilder(innerSimpleName)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addMethods(methodSpec(methodList, ++index, size));
naming().typeVariableNameUnboundedListTo(inner);
typeSpecList.add(inner.build());
}
return typeSpecList;
}
TypeSpec type(AnnotationSpec annotationSpec) {
TypeSpec.Builder type = TypeSpec.interfaceBuilder(naming().builderInterfaceSimpleName())
.addModifiers(accessInfo().modifiers())
.addAnnotation(annotationSpec);
naming().typeVariableNameListTo(type);
return type
.addMethods(firstMethod())
.addTypes(inner())
.build();
}
private List build() {
MethodSpec methodSpec = MethodSpec.methodBuilder("build")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.returns(naming().superClassTypeNameUnbounded())
.build();
return ImmutableList.of(methodSpec);
}
private List methodSpec(List methodList, int index, int size) {
return index == size
? build()
: middle(methodList.get(index));
}
private List middle(AttributeMethod method) {
return method.builderInterfaceMiddle();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy