net.zerobuilder.compiler.generate.DtoProjectionInfo Maven / Gradle / Ivy
package net.zerobuilder.compiler.generate;
import com.squareup.javapoet.TypeName;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import static java.util.Collections.emptyList;
public final class DtoProjectionInfo {
public interface ProjectionInfo {
R accept(ProjectionInfoCases cases, P p);
}
public interface ProjectionInfoCases {
R projectionMethod(ProjectionMethod projection, P p);
R fieldAccess(FieldAccess projection, P p);
}
static Function asFunction(ProjectionInfoCases cases) {
return projectionInfo -> projectionInfo.accept(cases, null);
}
static BiFunction asBiFunction(ProjectionInfoCases cases) {
return (projectionInfo, p) -> projectionInfo.accept(cases, p);
}
static Function projectionInfoCases(
Function projectionMethod,
Function fieldAccess) {
return asFunction(new ProjectionInfoCases() {
@Override
public R projectionMethod(ProjectionMethod projection, Void aVoid) {
return projectionMethod.apply(projection);
}
@Override
public R fieldAccess(FieldAccess projection, Void aVoid) {
return fieldAccess.apply(projection);
}
});
}
public static BiFunction projectionInfoCases(
BiFunction projectionMethod,
BiFunction fieldAccess) {
return asBiFunction(new ProjectionInfoCases() {
@Override
public R projectionMethod(ProjectionMethod projection, P p) {
return projectionMethod.apply(projection, p);
}
@Override
public R fieldAccess(FieldAccess projection, P p) {
return fieldAccess.apply(projection, p);
}
});
}
public static final class ProjectionMethod implements ProjectionInfo {
public final String methodName;
final List thrownTypes;
private ProjectionMethod(String methodName, List thrownTypes) {
this.methodName = methodName;
this.thrownTypes = thrownTypes;
}
public static ProjectionMethod create(String methodName, List thrownTypes) {
return new ProjectionMethod(methodName, thrownTypes);
}
public static ProjectionMethod create(String methodName) {
return new ProjectionMethod(methodName, emptyList());
}
@Override
public R accept(ProjectionInfoCases cases, P p) {
return cases.projectionMethod(this, p);
}
}
public static final class FieldAccess implements ProjectionInfo {
public final String fieldName;
private FieldAccess(String fieldName) {
this.fieldName = fieldName;
}
public static FieldAccess create(String fieldName) {
return new FieldAccess(fieldName);
}
@Override
public R accept(ProjectionInfoCases cases, P p) {
return cases.fieldAccess(this, p);
}
}
public static final Function> thrownTypes =
projectionInfoCases(
projection -> projection.thrownTypes,
projection -> emptyList());
private DtoProjectionInfo() {
throw new UnsupportedOperationException("no instances");
}
}