make method parameters final

pull/27/head
Mahmoud Ben Hassine 10 years ago
parent bea50076d9
commit 45bf1f7479

@ -13,7 +13,7 @@ class ActionMethodOrderBean implements Comparable<ActionMethodOrderBean> {
private int order; private int order;
ActionMethodOrderBean(Method method, int order) { ActionMethodOrderBean(final Method method, final int order) {
this.method = method; this.method = method;
this.order = order; this.order = order;
} }
@ -27,7 +27,7 @@ class ActionMethodOrderBean implements Comparable<ActionMethodOrderBean> {
} }
@Override @Override
public int compareTo(ActionMethodOrderBean actionMethodOrderBean) { public int compareTo(final ActionMethodOrderBean actionMethodOrderBean) {
if (order < actionMethodOrderBean.getOrder()) { if (order < actionMethodOrderBean.getOrder()) {
return -1; return -1;
} else if (order > actionMethodOrderBean.getOrder()) { } else if (order > actionMethodOrderBean.getOrder()) {
@ -38,7 +38,7 @@ class ActionMethodOrderBean implements Comparable<ActionMethodOrderBean> {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof ActionMethodOrderBean)) return false; if (!(o instanceof ActionMethodOrderBean)) return false;

@ -31,7 +31,7 @@ import javax.management.MXBean;
/** /**
* Basic rule implementation class that provides common methods. * Basic rule implementation class that provides common methods.
* * <p/>
* You can extend this class and override {@link BasicRule#evaluate()} and {@link BasicRule#execute()} * You can extend this class and override {@link BasicRule#evaluate()} and {@link BasicRule#execute()}
* to provide rule conditions and actions logic. * to provide rule conditions and actions logic.
* *
@ -95,7 +95,7 @@ public class BasicRule implements Rule, Comparable<Rule> {
return description; return description;
} }
public void setDescription(String description) { public void setDescription(final String description) {
this.description = description; this.description = description;
} }
@ -103,7 +103,7 @@ public class BasicRule implements Rule, Comparable<Rule> {
return priority; return priority;
} }
public void setPriority(int priority) { public void setPriority(final int priority) {
this.priority = priority; this.priority = priority;
} }
@ -112,7 +112,7 @@ public class BasicRule implements Rule, Comparable<Rule> {
*/ */
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;

@ -29,7 +29,9 @@ import org.easyrules.api.RuleListener;
import org.easyrules.api.RulesEngine; import org.easyrules.api.RulesEngine;
import org.easyrules.util.Utils; import org.easyrules.util.Utils;
import java.util.*; import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -47,7 +49,7 @@ class DefaultRulesEngine implements RulesEngine {
private static final Logger LOGGER = Logger.getLogger(RulesEngine.class.getName()); private static final Logger LOGGER = Logger.getLogger(RulesEngine.class.getName());
/** /**
* The engine name * The engine name.
*/ */
protected String name; protected String name;
@ -76,8 +78,8 @@ class DefaultRulesEngine implements RulesEngine {
*/ */
private List<RuleListener> ruleListeners; private List<RuleListener> ruleListeners;
DefaultRulesEngine(String name, boolean skipOnFirstAppliedRule, boolean skipOnFirstFailedRule, DefaultRulesEngine(final String name, final boolean skipOnFirstAppliedRule, final boolean skipOnFirstFailedRule,
int rulePriorityThreshold, List<RuleListener> ruleListeners, boolean silentMode) { final int rulePriorityThreshold, final List<RuleListener> ruleListeners, final boolean silentMode) {
this.name = name; this.name = name;
rules = new TreeSet<Rule>(); rules = new TreeSet<Rule>();
this.skipOnFirstAppliedRule = skipOnFirstAppliedRule; this.skipOnFirstAppliedRule = skipOnFirstAppliedRule;
@ -95,12 +97,12 @@ class DefaultRulesEngine implements RulesEngine {
} }
@Override @Override
public void registerRule(Object rule) { public void registerRule(final Object rule) {
rules.add(asRule(rule)); rules.add(asRule(rule));
} }
@Override @Override
public void unregisterRule(Object rule) { public void unregisterRule(final Object rule) {
rules.remove(asRule(rule)); rules.remove(asRule(rule));
} }
@ -170,19 +172,19 @@ class DefaultRulesEngine implements RulesEngine {
} }
private void triggerListenersOnFailure(Rule rule, Exception exception) { private void triggerListenersOnFailure(final Rule rule, final Exception exception) {
for (RuleListener ruleListener : ruleListeners) { for (RuleListener ruleListener : ruleListeners) {
ruleListener.onFailure(rule, exception); ruleListener.onFailure(rule, exception);
} }
} }
private void triggerListenersOnSuccess(Rule rule) { private void triggerListenersOnSuccess(final Rule rule) {
for (RuleListener ruleListener : ruleListeners) { for (RuleListener ruleListener : ruleListeners) {
ruleListener.onSuccess(rule); ruleListener.onSuccess(rule);
} }
} }
private void triggerListenersBeforeExecute(Rule rule) { private void triggerListenersBeforeExecute(final Rule rule) {
for (RuleListener ruleListener : ruleListeners) { for (RuleListener ruleListener : ruleListeners) {
ruleListener.beforeExecute(rule); ruleListener.beforeExecute(rule);
} }
@ -194,7 +196,7 @@ class DefaultRulesEngine implements RulesEngine {
LOGGER.log(Level.INFO, "Skip on first failed rule: {0}", skipOnFirstFailedRule); LOGGER.log(Level.INFO, "Skip on first failed rule: {0}", skipOnFirstFailedRule);
} }
private Rule asRule(Object rule) { private Rule asRule(final Object rule) {
Rule result; Rule result;
if (Utils.getInterfaces(rule).contains(Rule.class)) { if (Utils.getInterfaces(rule).contains(Rule.class)) {
result = (Rule) rule; result = (Rule) rule;

@ -22,26 +22,26 @@ import static java.lang.String.format;
*/ */
class RuleDefinitionValidator { class RuleDefinitionValidator {
public void validateRuleDefinition(Object rule) { public void validateRuleDefinition(final Object rule) {
checkRuleClass(rule); checkRuleClass(rule);
checkConditionMethod(rule); checkConditionMethod(rule);
checkActionMethods(rule); checkActionMethods(rule);
checkPriorityMethod(rule); checkPriorityMethod(rule);
} }
private void checkRuleClass(Object rule) { private void checkRuleClass(final Object rule) {
if (!isRuleClassWellDefined(rule)) { if (!isRuleClassWellDefined(rule)) {
throw new IllegalArgumentException(format("Rule '%s' is not annotated with '%s'", rule.getClass().getName(), Rule.class.getName())); throw new IllegalArgumentException(format("Rule '%s' is not annotated with '%s'", rule.getClass().getName(), Rule.class.getName()));
} }
} }
private void checkConditionMethod(Object rule) { private void checkConditionMethod(final Object rule) {
List<Method> conditionMethods = getMethodsAnnotatedWith(Condition.class, rule); List<Method> conditionMethods = getMethodsAnnotatedWith(Condition.class, rule);
if (conditionMethods.isEmpty()) { if (conditionMethods.isEmpty()) {
throw new IllegalArgumentException(format("Rule '%s' must have a public method annotated with '%s'", rule.getClass().getName(), Condition.class.getName())); throw new IllegalArgumentException(format("Rule '%s' must have a public method annotated with '%s'", rule.getClass().getName(), Condition.class.getName()));
} }
if(conditionMethods.size() > 1) { if (conditionMethods.size() > 1) {
throw new IllegalArgumentException(format("Rule '%s' must have exactly one method annotated with annotated with '%s'", rule.getClass().getName(), Condition.class.getName())); throw new IllegalArgumentException(format("Rule '%s' must have exactly one method annotated with annotated with '%s'", rule.getClass().getName(), Condition.class.getName()));
} }
@ -52,7 +52,7 @@ class RuleDefinitionValidator {
} }
} }
private void checkActionMethods(Object rule) { private void checkActionMethods(final Object rule) {
List<Method> actionMethods = getMethodsAnnotatedWith(Action.class, rule); List<Method> actionMethods = getMethodsAnnotatedWith(Action.class, rule);
if (actionMethods.isEmpty()) { if (actionMethods.isEmpty()) {
throw new IllegalArgumentException(format("Rule '%s' must have a public method annotated with '%s'", rule.getClass().getName(), Action.class.getName())); throw new IllegalArgumentException(format("Rule '%s' must have a public method annotated with '%s'", rule.getClass().getName(), Action.class.getName()));
@ -65,7 +65,7 @@ class RuleDefinitionValidator {
} }
} }
private int checkPriorityMethod(Object rule) { private int checkPriorityMethod(final Object rule) {
List<Method> priorityMethods = getMethodsAnnotatedWith(Priority.class, rule); List<Method> priorityMethods = getMethodsAnnotatedWith(Priority.class, rule);
@ -92,28 +92,28 @@ class RuleDefinitionValidator {
} }
} }
private boolean isRuleClassWellDefined(Object rule) { private boolean isRuleClassWellDefined(final Object rule) {
return rule.getClass().isAnnotationPresent(Rule.class); return rule.getClass().isAnnotationPresent(Rule.class);
} }
private boolean isConditionMethodWellDefined(Method method) { private boolean isConditionMethodWellDefined(final Method method) {
return Modifier.isPublic(method.getModifiers()) return Modifier.isPublic(method.getModifiers())
&& method.getReturnType().equals(Boolean.TYPE) && method.getReturnType().equals(Boolean.TYPE)
&& method.getParameterTypes().length == 0; && method.getParameterTypes().length == 0;
} }
private boolean isActionMethodWellDefined(Method method) { private boolean isActionMethodWellDefined(final Method method) {
return Modifier.isPublic(method.getModifiers()) return Modifier.isPublic(method.getModifiers())
&& method.getParameterTypes().length == 0; && method.getParameterTypes().length == 0;
} }
private boolean isPriorityMethodWellDefined(Method method) { private boolean isPriorityMethodWellDefined(final Method method) {
return Modifier.isPublic(method.getModifiers()) return Modifier.isPublic(method.getModifiers())
&& method.getReturnType().equals(Integer.TYPE) && method.getReturnType().equals(Integer.TYPE)
&& method.getParameterTypes().length == 0; && method.getParameterTypes().length == 0;
} }
private List<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation, Object rule) { private List<Method> getMethodsAnnotatedWith(final Class<? extends Annotation> annotation, final Object rule) {
Method[] methods = getMethods(rule); Method[] methods = getMethods(rule);
List<Method> annotatedMethods = new ArrayList<Method>(); List<Method> annotatedMethods = new ArrayList<Method>();
for (Method method : methods) { for (Method method : methods) {
@ -124,7 +124,7 @@ class RuleDefinitionValidator {
return annotatedMethods; return annotatedMethods;
} }
private Method[] getMethods(Object rule) { private Method[] getMethods(final Object rule) {
return rule.getClass().getMethods(); return rule.getClass().getMethods();
} }

@ -5,13 +5,13 @@ package org.easyrules.core;
* *
* @author Mahmoud Ben Hassine (mahmoud@benhassine.fr) * @author Mahmoud Ben Hassine (mahmoud@benhassine.fr)
*/ */
class RulePriorityBean implements Comparable<RulePriorityBean> { final class RulePriorityBean implements Comparable<RulePriorityBean> {
private int priority; private int priority;
private Object rule; private Object rule;
private RulePriorityBean(int priority, Object rule) { private RulePriorityBean(final int priority, final Object rule) {
this.priority = priority; this.priority = priority;
this.rule = rule; this.rule = rule;
} }
@ -25,7 +25,7 @@ class RulePriorityBean implements Comparable<RulePriorityBean> {
} }
@Override @Override
public int compareTo(RulePriorityBean ruleBean) { public int compareTo(final RulePriorityBean ruleBean) {
if (priority < ruleBean.getPriority()) { if (priority < ruleBean.getPriority()) {
return -1; return -1;
} else if (priority > ruleBean.getPriority()) { } else if (priority > ruleBean.getPriority()) {

@ -9,7 +9,8 @@ import org.easyrules.util.Utils;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.*; import java.util.Set;
import java.util.TreeSet;
class RuleProxy implements InvocationHandler { class RuleProxy implements InvocationHandler {
@ -17,7 +18,7 @@ class RuleProxy implements InvocationHandler {
private static RuleDefinitionValidator ruleDefinitionValidator = new RuleDefinitionValidator(); private static RuleDefinitionValidator ruleDefinitionValidator = new RuleDefinitionValidator();
public RuleProxy(Object target) { public RuleProxy(final Object target) {
this.target = target; this.target = target;
} }
@ -27,7 +28,7 @@ class RuleProxy implements InvocationHandler {
* @param rule the annotated rule object. * @param rule the annotated rule object.
* @return a proxy that implements the {@link org.easyrules.api.Rule} interface. * @return a proxy that implements the {@link org.easyrules.api.Rule} interface.
*/ */
public static org.easyrules.api.Rule asRule(Object rule) { public static org.easyrules.api.Rule asRule(final Object rule) {
ruleDefinitionValidator.validateRuleDefinition(rule); ruleDefinitionValidator.validateRuleDefinition(rule);
@ -38,7 +39,7 @@ class RuleProxy implements InvocationHandler {
} }
@Override @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
if (method.getName().equals("getName")) { if (method.getName().equals("getName")) {
return getRuleAnnotation().name(); return getRuleAnnotation().name();
} }
@ -68,7 +69,7 @@ class RuleProxy implements InvocationHandler {
return null; return null;
} }
private int compareTo(org.easyrules.api.Rule otherRule) throws Exception { private int compareTo(final org.easyrules.api.Rule otherRule) throws Exception {
String otherName = otherRule.getName(); String otherName = otherRule.getName();
int otherPriority = otherRule.getPriority(); int otherPriority = otherRule.getPriority();
String name = getRuleAnnotation().name(); String name = getRuleAnnotation().name();

@ -37,32 +37,32 @@ public class RulesEngineBuilder {
name = Utils.DEFAULT_ENGINE_NAME; name = Utils.DEFAULT_ENGINE_NAME;
} }
public RulesEngineBuilder named(String name) { public RulesEngineBuilder named(final String name) {
this.name = name; this.name = name;
return this; return this;
} }
public RulesEngineBuilder withSkipOnFirstAppliedRule(boolean skipOnFirstAppliedRule) { public RulesEngineBuilder withSkipOnFirstAppliedRule(final boolean skipOnFirstAppliedRule) {
this.skipOnFirstAppliedRule = skipOnFirstAppliedRule; this.skipOnFirstAppliedRule = skipOnFirstAppliedRule;
return this; return this;
} }
public RulesEngineBuilder withSkipOnFirstFailedRule(boolean skipOnFirstFailedRule) { public RulesEngineBuilder withSkipOnFirstFailedRule(final boolean skipOnFirstFailedRule) {
this.skipOnFirstFailedRule = skipOnFirstFailedRule; this.skipOnFirstFailedRule = skipOnFirstFailedRule;
return this; return this;
} }
public RulesEngineBuilder withRulePriorityThreshold(int rulePriorityThreshold) { public RulesEngineBuilder withRulePriorityThreshold(final int rulePriorityThreshold) {
this.rulePriorityThreshold = rulePriorityThreshold; this.rulePriorityThreshold = rulePriorityThreshold;
return this; return this;
} }
public RulesEngineBuilder withRuleListener(RuleListener ruleListener) { public RulesEngineBuilder withRuleListener(final RuleListener ruleListener) {
this.ruleListeners.add(ruleListener); this.ruleListeners.add(ruleListener);
return this; return this;
} }
public RulesEngineBuilder withSilentMode(boolean silentMode) { public RulesEngineBuilder withSilentMode(final boolean silentMode) {
this.silentMode = silentMode; this.silentMode = silentMode;
return this; return this;
} }

@ -15,7 +15,7 @@ import static java.util.Arrays.asList;
* *
* @author Mahmoud Ben Hassine (mahmoud@benhassine.fr) * @author Mahmoud Ben Hassine (mahmoud@benhassine.fr)
*/ */
public class Utils { public final class Utils {
/** /**
* Default rule name. * Default rule name.
@ -56,7 +56,7 @@ public class Utils {
} }
} }
private static void muteLogger(String logger) { private static void muteLogger(final String logger) {
Logger.getLogger(logger).setUseParentHandlers(false); Logger.getLogger(logger).setUseParentHandlers(false);
Handler[] handlers = Logger.getLogger(logger).getHandlers(); Handler[] handlers = Logger.getLogger(logger).getHandlers();
for (Handler handler : handlers) { for (Handler handler : handlers) {
@ -64,17 +64,17 @@ public class Utils {
} }
} }
public static List<Class> getInterfaces(Object rule) { public static List<Class> getInterfaces(final Object rule) {
List<Class> interfaces = new ArrayList<Class>(); List<Class> interfaces = new ArrayList<Class>();
Class clazz = rule.getClass(); Class clazz = rule.getClass();
while(clazz.getSuperclass() != null) { while (clazz.getSuperclass() != null) {
interfaces.addAll(asList(clazz.getInterfaces())); interfaces.addAll(asList(clazz.getInterfaces()));
clazz = clazz.getSuperclass(); clazz = clazz.getSuperclass();
} }
return interfaces; return interfaces;
} }
public static void checkNotNull(Object argument, String argumentName) { public static void checkNotNull(final Object argument, final String argumentName) {
if (argument == null) { if (argument == null) {
throw new IllegalArgumentException(format("The %s must not be null", argumentName)); throw new IllegalArgumentException(format("The %s must not be null", argumentName));
} }

Loading…
Cancel
Save