br.com.objectos.auto.IsPredicateStatic 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.auto;
import java.util.List;
import javax.lang.model.element.Modifier;
import com.google.common.collect.ImmutableList;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeSpec;
/**
* @author [email protected] (Marcio Endo)
*/
abstract class IsPredicateStatic extends AbstractHasIsPredicate {
IsPredicateStatic(IsPredicate predicate) {
super(predicate);
}
public static IsPredicateStatic wrap(IsPredicate predicate) {
List parameterSpecList = predicate.parameterSpecList;
return parameterSpecList.isEmpty()
? new ParameterLess(predicate)
: new WithParameter(predicate);
}
public abstract List fieldList();
public abstract List methodList();
void addFieldListTo(TypeSpec.Builder type) {
List fieldList = fieldList();
for (FieldSpec field : fieldList) {
type.addField(field);
}
}
void addMethodListTo(TypeSpec.Builder type) {
List methodList = methodList();
for (MethodSpec method : methodList) {
type.addMethod(method);
}
}
private static class ParameterLess extends IsPredicateStatic {
public ParameterLess(IsPredicate predicate) {
super(predicate);
}
@Override
public List fieldList() {
FieldSpec yes = field(className(), "INSTANCE");
FieldSpec not = field(classNameNot(), "NOT");
return ImmutableList.of(yes, not);
}
@Override
public List methodList() {
MethodSpec yes = method("get", "INSTANCE");
MethodSpec not = method("not", "NOT");
return ImmutableList.of(yes, not);
}
private FieldSpec field(ClassName className, String name) {
return FieldSpec.builder(predicateTypeName(), name)
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
.initializer("new $T()", className)
.build();
}
private MethodSpec method(String name, String fieldName) {
return MethodSpec.methodBuilder(name)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(predicateTypeName())
.addStatement("return $L", fieldName)
.build();
}
}
private static class WithParameter extends IsPredicateStatic {
public WithParameter(IsPredicate predicate) {
super(predicate);
}
@Override
public List fieldList() {
return ImmutableList.of();
}
@Override
public List methodList() {
MethodSpec yes = method("get", className());
MethodSpec not = method("not", classNameNot());
return ImmutableList.of(yes, not);
}
private MethodSpec method(String name, ClassName className) {
MethodSpec.Builder b = MethodSpec.methodBuilder(name)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(predicateTypeName());
for (ParameterSpec parameterSpec : parameterSpecList()) {
b.addParameter(parameterSpec);
}
CodeBlock returnStatement = methodInfo().statementWriter()
.add("return new $T(")
.add("$L").forEachParameter(", ")
.add(")")
.set(className)
.setParameterName()
.write();
return b.addCode(returnStatement)
.build();
}
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy