From 45bf1f7479831bc38cb024b3ed90548bb96453b4 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Thu, 25 Jun 2015 23:29:13 +0200 Subject: [PATCH] make method parameters final --- .../easyrules/core/ActionMethodOrderBean.java | 6 ++--- .../java/org/easyrules/core/BasicRule.java | 8 +++---- .../easyrules/core/DefaultRulesEngine.java | 22 +++++++++-------- .../core/RuleDefinitionValidator.java | 24 +++++++++---------- .../org/easyrules/core/RulePriorityBean.java | 8 +++---- .../java/org/easyrules/core/RuleProxy.java | 13 +++++----- .../easyrules/core/RulesEngineBuilder.java | 12 +++++----- .../main/java/org/easyrules/util/Utils.java | 10 ++++---- 8 files changed, 53 insertions(+), 50 deletions(-) diff --git a/easyrules-core/src/main/java/org/easyrules/core/ActionMethodOrderBean.java b/easyrules-core/src/main/java/org/easyrules/core/ActionMethodOrderBean.java index 25d59c5..f4971d4 100644 --- a/easyrules-core/src/main/java/org/easyrules/core/ActionMethodOrderBean.java +++ b/easyrules-core/src/main/java/org/easyrules/core/ActionMethodOrderBean.java @@ -13,7 +13,7 @@ class ActionMethodOrderBean implements Comparable { 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 { } @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 { } @Override - public boolean equals(Object o) { + public boolean equals(final Object o) { if (this == o) return true; if (!(o instanceof ActionMethodOrderBean)) return false; diff --git a/easyrules-core/src/main/java/org/easyrules/core/BasicRule.java b/easyrules-core/src/main/java/org/easyrules/core/BasicRule.java index fd73ec9..8dd40fc 100644 --- a/easyrules-core/src/main/java/org/easyrules/core/BasicRule.java +++ b/easyrules-core/src/main/java/org/easyrules/core/BasicRule.java @@ -31,7 +31,7 @@ import javax.management.MXBean; /** * Basic rule implementation class that provides common methods. - * + *

* 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 { 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 { 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 { */ @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; diff --git a/easyrules-core/src/main/java/org/easyrules/core/DefaultRulesEngine.java b/easyrules-core/src/main/java/org/easyrules/core/DefaultRulesEngine.java index 254203b..4bbfd49 100644 --- a/easyrules-core/src/main/java/org/easyrules/core/DefaultRulesEngine.java +++ b/easyrules-core/src/main/java/org/easyrules/core/DefaultRulesEngine.java @@ -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 ruleListeners; - DefaultRulesEngine(String name, boolean skipOnFirstAppliedRule, boolean skipOnFirstFailedRule, - int rulePriorityThreshold, List ruleListeners, boolean silentMode) { + DefaultRulesEngine(final String name, final boolean skipOnFirstAppliedRule, final boolean skipOnFirstFailedRule, + final int rulePriorityThreshold, final List ruleListeners, final boolean silentMode) { this.name = name; rules = new TreeSet(); 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; diff --git a/easyrules-core/src/main/java/org/easyrules/core/RuleDefinitionValidator.java b/easyrules-core/src/main/java/org/easyrules/core/RuleDefinitionValidator.java index f0f999f..788279c 100644 --- a/easyrules-core/src/main/java/org/easyrules/core/RuleDefinitionValidator.java +++ b/easyrules-core/src/main/java/org/easyrules/core/RuleDefinitionValidator.java @@ -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 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 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 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 getMethodsAnnotatedWith(Class annotation, Object rule) { + private List getMethodsAnnotatedWith(final Class annotation, final Object rule) { Method[] methods = getMethods(rule); List annotatedMethods = new ArrayList(); 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(); } diff --git a/easyrules-core/src/main/java/org/easyrules/core/RulePriorityBean.java b/easyrules-core/src/main/java/org/easyrules/core/RulePriorityBean.java index 7f86889..81a6cdb 100644 --- a/easyrules-core/src/main/java/org/easyrules/core/RulePriorityBean.java +++ b/easyrules-core/src/main/java/org/easyrules/core/RulePriorityBean.java @@ -5,13 +5,13 @@ package org.easyrules.core; * * @author Mahmoud Ben Hassine (mahmoud@benhassine.fr) */ -class RulePriorityBean implements Comparable { +final class RulePriorityBean implements Comparable { 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 { } @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 { return result; } -} \ No newline at end of file +} diff --git a/easyrules-core/src/main/java/org/easyrules/core/RuleProxy.java b/easyrules-core/src/main/java/org/easyrules/core/RuleProxy.java index c10d440..b68a339 100644 --- a/easyrules-core/src/main/java/org/easyrules/core/RuleProxy.java +++ b/easyrules-core/src/main/java/org/easyrules/core/RuleProxy.java @@ -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); } -} \ No newline at end of file +} diff --git a/easyrules-core/src/main/java/org/easyrules/core/RulesEngineBuilder.java b/easyrules-core/src/main/java/org/easyrules/core/RulesEngineBuilder.java index 7bba599..111bc8b 100644 --- a/easyrules-core/src/main/java/org/easyrules/core/RulesEngineBuilder.java +++ b/easyrules-core/src/main/java/org/easyrules/core/RulesEngineBuilder.java @@ -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; } diff --git a/easyrules-core/src/main/java/org/easyrules/util/Utils.java b/easyrules-core/src/main/java/org/easyrules/util/Utils.java index 7224200..12a4d49 100644 --- a/easyrules-core/src/main/java/org/easyrules/util/Utils.java +++ b/easyrules-core/src/main/java/org/easyrules/util/Utils.java @@ -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 getInterfaces(Object rule) { + public static List getInterfaces(final Object rule) { List interfaces = new ArrayList(); 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)); }