Skip to content

Commit

Permalink
feat: create implementation to operation annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Dec 27, 2023
1 parent d6b1006 commit ac4689a
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public enum RepositoryType {
*/
CUSTOM_REPOSITORY(""),
/**
* Method that has {@link Query} annotation
* Method that has {@link jakarta.data.repository.OrderBy} annotation
*/
QUERY("", Query.class),
ORDER_BY(""),
/**
* Method that has {@link jakarta.data.repository.OrderBy} annotation
* Method that has {@link Query} annotation
*/
ORDER_BY("", OrderBy.class),
QUERY("", Query.class),
/**
* Method that has {@link jakarta.data.repository.Save} annotation
*/
Expand All @@ -104,6 +104,8 @@ public enum RepositoryType {
.or(Predicate.isEqual(BasicRepository.class));

private static final Set<RepositoryType> KEY_WORLD_METHODS = EnumSet.of(FIND_BY, DELETE_BY, COUNT_BY, EXISTS_BY);

private static final Set<RepositoryType> OPERATION_ANNOTATIONS = EnumSet.of(INSERT, SAVE, DELETE, UPDATE);
private final String keyword;

private final Class<? extends Annotation> annotation;
Expand Down Expand Up @@ -140,16 +142,20 @@ public static RepositoryType of(Method method, Class<?> repositoryType) {
if (method.getAnnotationsByType(OrderBy.class).length > 0) {
return ORDER_BY;
}
if (Objects.nonNull(method.getAnnotation(Query.class))) {
return QUERY;
}

if (!repositoryType.equals(declaringClass) && isCustomRepository(declaringClass)) {
return CUSTOM_REPOSITORY;
}
String methodName = method.getName();
if (FIND_ALL.keyword.equals(methodName)) {
return FIND_ALL;
}
Predicate<RepositoryType> hasAnnotation = a -> method.getAnnotation(a.annotation) != null;
if (OPERATION_ANNOTATIONS.stream().anyMatch(hasAnnotation)) {
return OPERATION_ANNOTATIONS.stream()
.filter(hasAnnotation)
.findFirst().orElseThrow();
}
return KEY_WORLD_METHODS.stream()
.filter(k -> methodName.startsWith(k.keyword))
.findFirst().orElse(UNKNOWN);
Expand Down

0 comments on commit ac4689a

Please sign in to comment.