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

@ -31,7 +31,7 @@ import javax.management.MXBean;
/**
* Basic rule implementation class that provides common methods.
*
* <p/>
* You can extend this class and override {@link BasicRule#evaluate()} and {@link BasicRule#execute()}
* to provide rule conditions and actions logic.
*
@ -95,7 +95,7 @@ public class BasicRule implements Rule, Comparable<Rule> {
return description;
}
public void setDescription(String description) {
public void setDescription(final String description) {
this.description = description;
}
@ -103,7 +103,7 @@ public class BasicRule implements Rule, Comparable<Rule> {
return priority;
}
public void setPriority(int priority) {
public void setPriority(final int priority) {
this.priority = priority;
}
@ -112,7 +112,7 @@ public class BasicRule implements Rule, Comparable<Rule> {
*/
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
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.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.Logger;
@ -47,7 +49,7 @@ class DefaultRulesEngine implements RulesEngine {
private static final Logger LOGGER = Logger.getLogger(RulesEngine.class.getName());
/**
* The engine name
* The engine name.
*/
protected String name;
@ -76,8 +78,8 @@ class DefaultRulesEngine implements RulesEngine {
*/
private List<RuleListener> ruleListeners;
DefaultRulesEngine(String name, boolean skipOnFirstAppliedRule, boolean skipOnFirstFailedRule,
int rulePriorityThreshold, List<RuleListener> ruleListeners, boolean silentMode) {
DefaultRulesEngine(final String name, final boolean skipOnFirstAppliedRule, final boolean skipOnFirstFailedRule,
final int rulePriorityThreshold, final List<RuleListener> ruleListeners, final boolean silentMode) {
this.name = name;
rules = new TreeSet<Rule>();
this.skipOnFirstAppliedRule = skipOnFirstAppliedRule;
@ -95,12 +97,12 @@ class DefaultRulesEngine implements RulesEngine {
}
@Override
public void registerRule(Object rule) {
public void registerRule(final Object rule) {
rules.add(asRule(rule));
}
@Override
public void unregisterRule(Object rule) {
public void unregisterRule(final Object 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) {
ruleListener.onFailure(rule, exception);
}
}
private void triggerListenersOnSuccess(Rule rule) {
private void triggerListenersOnSuccess(final Rule rule) {
for (RuleListener ruleListener : ruleListeners) {
ruleListener.onSuccess(rule);
}
}
private void triggerListenersBeforeExecute(Rule rule) {
private void triggerListenersBeforeExecute(final Rule rule) {
for (RuleListener ruleListener : ruleListeners) {
ruleListener.beforeExecute(rule);
}
@ -194,7 +196,7 @@ class DefaultRulesEngine implements RulesEngine {
LOGGER.log(Level.INFO, "Skip on first failed rule: {0}", skipOnFirstFailedRule);
}
private Rule asRule(Object rule) {
private Rule asRule(final Object rule) {
Rule result;
if (Utils.getInterfaces(rule).contains(Rule.class)) {
result = (Rule) rule;

@ -22,26 +22,26 @@ import static java.lang.String.format;
*/
class RuleDefinitionValidator {
public void validateRuleDefinition(Object rule) {
public void validateRuleDefinition(final Object rule) {
checkRuleClass(rule);
checkConditionMethod(rule);
checkActionMethods(rule);
checkPriorityMethod(rule);
}
private void checkRuleClass(Object rule) {
private void checkRuleClass(final Object rule) {
if (!isRuleClassWellDefined(rule)) {
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);
if (conditionMethods.isEmpty()) {
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()));
}
@ -52,7 +52,7 @@ class RuleDefinitionValidator {
}
}
private void checkActionMethods(Object rule) {
private void checkActionMethods(final Object rule) {
List<Method> actionMethods = getMethodsAnnotatedWith(Action.class, rule);
if (actionMethods.isEmpty()) {
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);
@ -92,28 +92,28 @@ class RuleDefinitionValidator {
}
}
private boolean isRuleClassWellDefined(Object rule) {
private boolean isRuleClassWellDefined(final Object rule) {
return rule.getClass().isAnnotationPresent(Rule.class);
}
private boolean isConditionMethodWellDefined(Method method) {
private boolean isConditionMethodWellDefined(final Method method) {
return Modifier.isPublic(method.getModifiers())
&& method.getReturnType().equals(Boolean.TYPE)
&& method.getParameterTypes().length == 0;
}
private boolean isActionMethodWellDefined(Method method) {
private boolean isActionMethodWellDefined(final Method method) {
return Modifier.isPublic(method.getModifiers())
&& method.getParameterTypes().length == 0;
}
private boolean isPriorityMethodWellDefined(Method method) {
private boolean isPriorityMethodWellDefined(final Method method) {
return Modifier.isPublic(method.getModifiers())
&& method.getReturnType().equals(Integer.TYPE)
&& 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);
List<Method> annotatedMethods = new ArrayList<Method>();
for (Method method : methods) {
@ -124,7 +124,7 @@ class RuleDefinitionValidator {
return annotatedMethods;
}
private Method[] getMethods(Object rule) {
private Method[] getMethods(final Object rule) {
return rule.getClass().getMethods();
}

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

@ -9,7 +9,8 @@ import org.easyrules.util.Utils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;
import java.util.Set;
import java.util.TreeSet;
class RuleProxy implements InvocationHandler {
@ -17,7 +18,7 @@ class RuleProxy implements InvocationHandler {
private static RuleDefinitionValidator ruleDefinitionValidator = new RuleDefinitionValidator();
public RuleProxy(Object target) {
public RuleProxy(final Object target) {
this.target = target;
}
@ -27,7 +28,7 @@ class RuleProxy implements InvocationHandler {
* @param rule the annotated rule object.
* @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);
@ -38,7 +39,7 @@ class RuleProxy implements InvocationHandler {
}
@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")) {
return getRuleAnnotation().name();
}
@ -68,7 +69,7 @@ class RuleProxy implements InvocationHandler {
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();
int otherPriority = otherRule.getPriority();
String name = getRuleAnnotation().name();
@ -137,4 +138,4 @@ class RuleProxy implements InvocationHandler {
return target.getClass().getAnnotation(Rule.class);
}
}
}

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

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

Loading…
Cancel
Save