All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.zerobuilder.compiler.generate.DtoProjectionInfo Maven / Gradle / Ivy

There is a newer version: 1.603
Show newest version
package net.zerobuilder.compiler.generate;

import com.squareup.javapoet.TypeName;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

import static java.util.Collections.emptyList;

public final class DtoProjectionInfo {

  public interface ProjectionInfo {
     R accept(ProjectionInfoCases cases);
  }

  public interface ProjectionInfoCases {
    R projectionMethod(ProjectionMethod projection);
    R fieldAccess(FieldAccess projection);
    R none();
  }

  interface ProjectionInfoRequiredCases {
    R projectionMethod(ProjectionMethod projection);
    R fieldAccess(FieldAccess projection);
  }

  static  Function asFunction(ProjectionInfoCases cases) {
    return projectionInfo -> projectionInfo.accept(cases);
  }

  static Predicate asPredicate(ProjectionInfoCases cases) {
    return projectionInfo -> projectionInfo.accept(cases);
  }

  static  Function asFunction(ProjectionInfoRequiredCases cases) {
    return asFunction(new ProjectionInfoCases() {
      @Override
      public R projectionMethod(ProjectionMethod projectionMethod) {
        return cases.projectionMethod(projectionMethod);
      }
      @Override
      public R fieldAccess(FieldAccess fieldAccess) {
        return cases.fieldAccess(fieldAccess);
      }
      @Override
      public R none() {
        // should never happen
        throw new IllegalStateException("ProjectionInfo required");
      }
    });
  }

  static final class ProjectionMethod implements ProjectionInfo {
    final String methodName;
    final List thrownTypes;

    private ProjectionMethod(String methodName, List thrownTypes) {
      this.methodName = methodName;
      this.thrownTypes = thrownTypes;
    }

    @Override
    public  R accept(ProjectionInfoCases cases) {
      return cases.projectionMethod(this);
    }
  }

  static final class FieldAccess implements ProjectionInfo {
    final String fieldName;

    private FieldAccess(String fieldName) {
      this.fieldName = fieldName;
    }

    @Override
    public  R accept(ProjectionInfoCases cases) {
      return cases.fieldAccess(this);
    }
  }

  static final class None implements ProjectionInfo {
    private None() {
    }

    @Override
    public  R accept(ProjectionInfoCases cases) {
      return cases.none();
    }
  }

  public static ProjectionInfo method(String methodName, List thrownTypes) {
    return new ProjectionMethod(methodName, thrownTypes);
  }

  public static ProjectionInfo method(String methodName) {
    return new ProjectionMethod(methodName, emptyList());
  }

  public static ProjectionInfo fieldAccess(String fieldName) {
    return new FieldAccess(fieldName);
  }

  public static ProjectionInfo none() {
    return new None();
  }

  static final Predicate isPresent
      = asPredicate(new ProjectionInfoCases() {
    @Override
    public Boolean projectionMethod(ProjectionMethod projection) {
      return true;
    }
    @Override
    public Boolean fieldAccess(FieldAccess projection) {
      return true;
    }
    @Override
    public Boolean none() {
      return false;
    }
  });

  static final Function> thrownTypes
      = asFunction(new ProjectionInfoCases>() {
    @Override
    public List projectionMethod(ProjectionMethod projection) {
      return projection.thrownTypes;
    }
    @Override
    public List fieldAccess(FieldAccess projection) {
      return emptyList();
    }
    @Override
    public List none() {
      return emptyList();
    }
  });

  private DtoProjectionInfo() {
    throw new UnsupportedOperationException("no instances");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy