com.alon.spring.crud.domain.service.HookManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of crud-api-base Show documentation
Show all versions of crud-api-base Show documentation
Fornece implementação básica e expansível para criação API's CRUD com Spring Boot e Spring Data JPA.
package com.alon.spring.crud.domain.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public final class HookManager {
private HookManager() {}
protected static Map>> GLOBAL_HOOKS = new HashMap<>();
protected static P executeHook(S service, P param, LifeCycleHook hookType) {
List hooks = getHooks(service, hookType);
param = (P) hooks.stream()
.reduce(Function.identity(), Function::andThen)
.apply(param);
return param;
}
protected static void addHook(S hookable, LifeCycleHook hookType, Function hook) {
getServiceHooks(hookable).get(hookType).add(hook);
}
protected static List getHooks(S service, LifeCycleHook hookType) {
return getServiceHooks(service).get(hookType);
}
protected static Map> getServiceHooks(S service) {
Map> hooks = GLOBAL_HOOKS.get(service);
if (hooks == null)
hooks = initHooks(service);
return hooks;
}
protected static Map> initHooks(T service) {
Map> hooks = new HashMap<>();
for (LifeCycleHook hook : LifeCycleHook.values())
hooks.put(hook, new ArrayList<>());
GLOBAL_HOOKS.put(service, hooks);
return hooks;
}
protected static void clearHooks(T service, LifeCycleHook... hookTypes) {
for (LifeCycleHook hook : hookTypes) {
Map> hooks = GLOBAL_HOOKS.get(service);
if (hooks != null)
hooks.get(hook).clear();
}
}
}