Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Aug 3, 2021
1 parent bda8052 commit 0081a87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import static net.jbock.common.Constants.STRING;

@ValidateScope
public class AutoMappings {
public class AutoMapper {

private static final String NEW = "new";
private static final String CREATE = "create";
Expand All @@ -42,7 +42,7 @@ public class AutoMappings {
private final List<AutoConversion> conversions;

@Inject
AutoMappings(
AutoMapper(
TypeTool tool,
Util util) {
this.tool = tool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
@ValidateScope
public class AutoValidator {

private final AutoMappings autoConverter;
private final AutoMapper autoMapper;
private final Util util;
private final MatchFinder matchFinder;

@Inject
AutoValidator(
AutoMappings autoConverter,
AutoMapper autoMapper,
Util util,
MatchFinder matchFinder) {
this.autoConverter = autoConverter;
this.autoMapper = autoMapper;
this.util = util;
this.matchFinder = matchFinder;
}
Expand All @@ -42,36 +42,36 @@ public class AutoValidator {
Either<ValidationFailure, Mapping<M>> findMapping(
M sourceMethod) {
return matchFinder.findMatch(sourceMethod)
.flatMap(m -> findMapping(sourceMethod, m));
.flatMap(m -> findEnumOrAutoMapping(sourceMethod, m));
}

private <M extends AnnotatedMethod>
Either<ValidationFailure, Mapping<M>> findMapping(
Either<ValidationFailure, Mapping<M>> findEnumOrAutoMapping(
M sourceMethod,
ValidMatch<M> match) {
return autoConverter.findAutoMapping(sourceMethod, match)
.flatMapLeft(message -> findEnumMapping(sourceMethod, match.baseType()));
return autoMapper.findAutoMapping(sourceMethod, match)
.flatMapLeft(message -> findEnumMapping(sourceMethod, match));
}

private <M extends AnnotatedMethod>
Either<ValidationFailure, Mapping<M>> findEnumMapping(
M sourceMethod,
TypeMirror baseType) {
ValidMatch<M> match) {
TypeMirror baseType = match.baseType();
return TypeTool.AS_DECLARED.visit(baseType)
.map(DeclaredType::asElement)
.flatMap(TypeTool.AS_TYPE_ELEMENT::visit)
.filter(element -> element.getKind() == ElementKind.ENUM)
.map(TypeElement::asType)
.<Either<ValidationFailure, TypeMirror>>map(Either::right)
.orElseGet(() -> left(sourceMethod.fail(util.noMatchError(baseType))))
.flatMap(enumType -> matchFinder.findMatch(sourceMethod)
.map(match -> {
CodeBlock code = enumConvertBlock(enumType);
return Mapping.create(code, match, true);
}));
.map(enumType -> {
CodeBlock code = enumConversionCode(enumType);
return Mapping.create(code, match, true);
});
}

private CodeBlock enumConvertBlock(TypeMirror enumType) {
private CodeBlock enumConversionCode(TypeMirror enumType) {
ParameterSpec token = ParameterSpec.builder(STRING, "token").build();
ParameterSpec e = ParameterSpec.builder(IllegalArgumentException.class, "e").build();
ParameterSpec values = ParameterSpec.builder(STRING, "values").build();
Expand Down

0 comments on commit 0081a87

Please sign in to comment.