com.alon.spring.crud.domain.service.BiHookManager 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.BiFunction;
public final class BiHookManager {
private BiHookManager() {}
protected static Map>> GLOBAL_HOOKS = new HashMap<>();
protected static PA executeHook(
S service, LifeCycleHook hookType, PA paramA, PB paramB) {
List hooks = getHooks(service, hookType);
for (BiFunction hook : hooks) {
paramA = (PA) hook.apply(paramA, paramB);
}
return paramA;
}
protected static void addHook(S hookable, LifeCycleHook hookType, BiFunction 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();
}
}
}