net.zerobuilder.compiler.generate.ZeroUtil Maven / Gradle / Ivy
package net.zerobuilder.compiler.generate;
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.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import javax.lang.model.element.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
import static com.squareup.javapoet.MethodSpec.constructorBuilder;
import static java.lang.Character.isUpperCase;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
public final class ZeroUtil {
public static final class ClassNames {
public static final ClassName COLLECTION = ClassName.get(Collection.class);
public static final ClassName LIST = ClassName.get(List.class);
public static final ClassName SET = ClassName.get(Set.class);
public static final ClassName ITERABLE = ClassName.get(Iterable.class);
public static final ClassName THREAD_LOCAL = ClassName.get(ThreadLocal.class);
private ClassNames() {
throw new UnsupportedOperationException("no instances");
}
}
private static final Set reservedWords = new HashSet<>(Arrays.asList(
"abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package",
"synchronized", "boolean", "do", "goto", "private", "this", "break", "double", "implements",
"protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum",
"instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final",
"interface", "static", "void", "class", "finally", "long", "strictfp", "volatile", "const",
"float", "native", "super", "while"));
public static final CodeBlock emptyCodeBlock = CodeBlock.of("");
public static String upcase(String s) {
if (s.isEmpty() || Character.isUpperCase(s.charAt(0))) {
return s;
}
return Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
public static String downcase(String s) {
if (s.length() >= 2 && isUpperCase(s.charAt(1))) {
return s;
}
String lowered = Character.toLowerCase(s.charAt(0)) + s.substring(1);
if (reservedWords.contains(lowered)) {
return s;
}
return lowered;
}
public static CodeBlock statement(String format, Object... args) {
return CodeBlock.builder().addStatement(format, args).build();
}
public static ParameterSpec parameterSpec(TypeName type, String name) {
return ParameterSpec.builder(type, name).build();
}
public static FieldSpec fieldSpec(TypeName type, String name, Modifier... modifiers) {
return FieldSpec.builder(type, name, modifiers).build();
}
public static CodeBlock nullCheck(String varName, String message) {
return CodeBlock.builder()
.beginControlFlow("if ($N == null)", varName)
.addStatement("throw new $T($S)", NullPointerException.class, message)
.endControlFlow().build();
}
public static CodeBlock nullCheck(ParameterSpec parameterSpec) {
return nullCheck(parameterSpec.name, parameterSpec.name);
}
public static CodeBlock nullCheck(ParameterSpec parameterSpec, String message) {
return nullCheck(parameterSpec.name, message);
}
public static String distinctFrom(String string, String other) {
if (string.equals(other)) {
return 'a' + upcase(string);
}
return string;
}
public static Optional rawClassName(TypeName typeName) {
if (typeName instanceof ClassName) {
return Optional.of((ClassName) typeName);
}
if (typeName instanceof ParameterizedTypeName) {
ParameterizedTypeName parameterized = (ParameterizedTypeName) typeName;
return Optional.of(parameterized.rawType);
}
return Optional.empty();
}
public static List typeArguments(TypeName typeName) {
if (typeName instanceof ParameterizedTypeName) {
ParameterizedTypeName parameterized = (ParameterizedTypeName) typeName;
return parameterized.typeArguments;
}
return emptyList();
}
/**
* @param typeName type
* @return first type argument, if any
* @throws IllegalArgumentException if type has multiple type arguments
*/
public static Optional onlyTypeArgument(TypeName typeName) {
List types = typeArguments(typeName);
switch (types.size()) {
case 0:
return Optional.empty();
case 1:
return Optional.of(types.get(0));
default:
throw new IllegalArgumentException("multiple type arguments");
}
}
public static List transform(Collection extends X> input, Function function) {
return input.stream().map(function).collect(toList());
}
public static List
presentInstances(Optional
optional) {
if (optional.isPresent()) {
return singletonList(optional.get());
}
return emptyList();
}
public static
List
reverse(List
list) {
ArrayList
reversed = new ArrayList<>(list.size());
reversed.addAll(list);
Collections.reverse(reversed);
return reversed;
}
public static
List
concat(P first, List
list) {
ArrayList
builder = new ArrayList<>(list.size() + 1);
builder.add(first);
builder.addAll(list);
return builder;
}
public static
List
concat(List
left, List
right) {
ArrayList
builder = new ArrayList<>(left.size() + right.size());
builder.addAll(left);
builder.addAll(right);
return builder;
}
public static final Collector joinCodeBlocks
= new Collector() {
@Override
public Supplier supplier() {
return CodeBlock::builder;
}
@Override
public BiConsumer accumulator() {
return (builder, block) -> builder.add(block);
}
@Override
public BinaryOperator combiner() {
return (left, right) -> {
left.add(right.build());
return left;
};
}
@Override
public Function finisher() {
return CodeBlock.Builder::build;
}
@Override
public Set characteristics() {
return emptySet();
}
};
public static Collector, List, List> flatList() {
return new Collector, List, List>() {
@Override
public Supplier> supplier() {
return ArrayList::new;
}
@Override
public BiConsumer, List> accumulator() {
return (left, right) -> left.addAll(right);
}
@Override
public BinaryOperator> combiner() {
return (left, right) -> {
left.addAll(right);
return left;
};
}
@Override
public Function, List> finisher() {
return Function.identity();
}
@Override
public Set characteristics() {
return emptySet();
}
};
}
public static Collector, R> listCollector(Function, R> finisher) {
return new Collector, R>() {
@Override
public Supplier> supplier() {
return ArrayList::new;
}
@Override
public BiConsumer, E> accumulator() {
return (left, right) -> left.add(right);
}
@Override
public BinaryOperator> combiner() {
return (left, right) -> {
left.addAll(right);
return left;
};
}
@Override
public Function, R> finisher() {
return finisher;
}
@Override
public Set characteristics() {
return emptySet();
}
};
}
public static Supplier memoize(Supplier supplier) {
List ref = new ArrayList<>(singletonList(null));
return () -> {
R element = ref.get(0);
if (element == null) {
element = supplier.get();
ref.set(0, element);
}
return element;
};
}
public static Predicate asPredicate(Function function) {
return r -> function.apply(r);
}
public static MethodSpec constructor(Modifier... modifiers) {
return constructorBuilder().addModifiers(modifiers).build();
}
private ZeroUtil() {
throw new UnsupportedOperationException("no instances");
}
}