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

ru.curs.celesta.event.TriggerDispatcher Maven / Gradle / Ivy

The newest version!
package ru.curs.celesta.event;

import ru.curs.celesta.dbutils.Cursor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Created by ioann on 31.05.2017.
 */
public final class TriggerDispatcher {

    private final EnumMap, List>>> triggerMap =
            new EnumMap<>(TriggerType.class);

    public TriggerDispatcher() {
        Arrays.stream(TriggerType.values()).forEach(t -> triggerMap.put(t, new HashMap<>()));
    }

    public  void registerTrigger(TriggerType type, Class cursorClass,
                                                   Consumer consumer) {
        Map, List>> cursorClassMap = triggerMap.get(type);
        cursorClassMap.computeIfAbsent(cursorClass, s -> new ArrayList<>()).add(consumer);
    }

    @SuppressWarnings("unchecked")
    public void fireTrigger(TriggerType type, Cursor cursor) {
        Map, List>> cursorClassMap = triggerMap.get(type);
        final Class cursorClass = cursor.getClass();
        List> triggers = cursorClassMap.getOrDefault(cursorClass, Collections.emptyList());
        triggers.forEach(consumer ->
                ((Consumer) consumer).accept(cursor));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy