/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package API; import common.Instruction; import common.Log; import java.util.HashMap; import java.util.LinkedHashMap; /** * * @author Eugenio */ public class CodeProcessor { // Todos os processors serão armazenados aqui, e cada evendo possui uma lista de processors separada por virgula protected static LinkedHashMap processors = new LinkedHashMap<>(); protected static HashMap> events = new HashMap<>(); public static void _init() { } public static void On(String selectors, String event, String processors) { for (String selector : selectors.split(",")) { if (!events.containsKey(selector)) { events.put(selector, new HashMap()); } events.get(selector).put(event, processors); } } public static void Trigger(common.Code c, String selector, String event) throws Exception { // System.out.println("Try trigger:" + selector); if (events.containsKey(selector)) { HashMap t = events.get(selector); if (t.containsKey(event)) { ProcessorExecute(c, t.get(event)); } } } public static void Add(String id, API.CodeProcessorInterface p) throws Exception { if (processors.containsKey(id)) { Log.PrintWarning( "Code Processor", new Instruction().S("msg", String.format("Tentativa de sobrescrita do processor %s.", id)) ); } processors.put(id, p); } protected static String[] ProcToList(String events) { // System.out.println("events:" + events); return events.replaceAll("\\s+", "").split(","); } public static void ProcessorExecute(common.Code c, String eventList) throws Exception { for (String x : ProcToList(eventList)) { if (x.equals("")) { continue; } if (!processors.containsKey(x)) { throw new Exception(String.format("Processador de código '%s' não definido!", x)); } processors.get(x.trim()).Exec(c, processors); } } }