From ba95fbffedef3d64ab0ea106ddf50e0e35670a77 Mon Sep 17 00:00:00 2001 From: Jeffrey Phillips Freeman <jeffrey.freeman@syncleus.com> Date: Sun, 9 Nov 2014 08:53:27 -0500 Subject: [PATCH] Added the ability for ActionTriggers to act on nodes taht have multiple actions of the same name. Change-Id: I58a267fec192d56eae56d10b4f4e2e78dc0e96b4 --- .../graph/action/AbstractActionTrigger.java | 19 ++++++++++----- .../action/AbstractPrioritySerialTrigger.java | 24 ++++++++++--------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/syncleus/grail/graph/action/AbstractActionTrigger.java b/src/main/java/com/syncleus/grail/graph/action/AbstractActionTrigger.java index a2f8085..0e705af 100644 --- a/src/main/java/com/syncleus/grail/graph/action/AbstractActionTrigger.java +++ b/src/main/java/com/syncleus/grail/graph/action/AbstractActionTrigger.java @@ -4,13 +4,13 @@ import java.lang.reflect.Method; import java.util.*; public abstract class AbstractActionTrigger implements ActionTrigger { - private final static Map<Class<?>, Map<String, Method>> ACTION_METHOD_CACHE = new HashMap<>(); + private final static Map<Class<?>, Map<String, Set<Method>>> ACTION_METHOD_CACHE = new HashMap<>(); - protected static final Map<String, Method> populateCache(final Class<?> parentClass) { - Map<String, Method> actionMethods = AbstractActionTrigger.ACTION_METHOD_CACHE.get(parentClass); + protected static final Map<String, Set<Method>> populateCache(final Class<?> parentClass) { + Map<String, Set<Method>> actionMethods = AbstractActionTrigger.ACTION_METHOD_CACHE.get(parentClass); if( actionMethods != null ) return actionMethods; - actionMethods = new HashMap<String, Method>(); + actionMethods = new HashMap<String, Set<Method>>(); AbstractActionTrigger.ACTION_METHOD_CACHE.put(parentClass, actionMethods); for(Class<?> triggerClass : parentClass.getInterfaces() ) { @@ -20,11 +20,18 @@ public abstract class AbstractActionTrigger implements ActionTrigger { if (actionAnnotation != null ) { if (triggerMethod.getParameterCount() > 0) throw new IllegalStateException("A method annotated with @Action required parameters"); - actionMethods.put(actionAnnotation.value(), triggerMethod); + + Set<Method> methods = actionMethods.get(actionAnnotation.value()); + if( methods == null ) { + methods = new HashSet<Method>(); + actionMethods.put(actionAnnotation.value(), methods); + } + + methods.add(triggerMethod); } } } - return actionMethods; + return Collections.unmodifiableMap(actionMethods); } } diff --git a/src/main/java/com/syncleus/grail/graph/action/AbstractPrioritySerialTrigger.java b/src/main/java/com/syncleus/grail/graph/action/AbstractPrioritySerialTrigger.java index 915ff40..e909eec 100644 --- a/src/main/java/com/syncleus/grail/graph/action/AbstractPrioritySerialTrigger.java +++ b/src/main/java/com/syncleus/grail/graph/action/AbstractPrioritySerialTrigger.java @@ -20,20 +20,22 @@ public abstract class AbstractPrioritySerialTrigger extends AbstractActionTrigge final Class<?> parentClass = triggerObject.getClass(); - Map<String, Method> actionMethods = AbstractPrioritySerialTrigger.populateCache(parentClass); + Map<String, Set<Method>> actionMethods = AbstractPrioritySerialTrigger.populateCache(parentClass); - final Method triggerMethod = actionMethods.get(actionName); - if( triggerMethod == null ) + final Set<Method> triggerMethods = actionMethods.get(actionName); + if( triggerMethods == null || triggerMethods.size() <= 0 ) throw new IllegalStateException("A ActionTrigger was configured to trigger an action which does not exist on the current object"); - try { - triggerMethod.invoke(triggerObject, null); - } - catch (final IllegalAccessException caught) { - throw new IllegalStateException("Tried to trigger an action method but can not access", caught); - } - catch (final InvocationTargetException caught) { - throw new IllegalStateException("Tried to trigger an action method but can not access", caught); + for( final Method triggerMethod : triggerMethods ) { + try { + triggerMethod.invoke(triggerObject, null); + } + catch (final IllegalAccessException caught) { + throw new IllegalStateException("Tried to trigger an action method but can not access", caught); + } + catch (final InvocationTargetException caught) { + throw new IllegalStateException("Tried to trigger an action method but can not access", caught); + } } } } -- GitLab