You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/08/26 14:35:10 UTC

[shardingsphere] branch master updated: ConditionValueGenerator in ConditionValueGeneratorFactory should be singleton-object (#20515)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new fd3e246a4eb ConditionValueGenerator in ConditionValueGeneratorFactory should be singleton-object (#20515)
fd3e246a4eb is described below

commit fd3e246a4ebb445a5ac695b6b59fccdeb89eddac
Author: Li Hongyu <lh...@gmail.com>
AuthorDate: Fri Aug 26 22:35:02 2022 +0800

    ConditionValueGenerator in ConditionValueGeneratorFactory should be singleton-object (#20515)
    
    * each ConditionValueGenerator in ConditionValueGeneratorFactory is stateless, and they are better to be static final and not to be allocated a new object in each invoke of generate method
    
    * checkstyle
    
    Co-authored-by: bjyflihongyu <li...@jd.com>
---
 .../generator/ConditionValueGeneratorFactory.java      | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValueGeneratorFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValueGeneratorFactory.java
index 2d0a7ec861b..9c32f9ed3ce 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValueGeneratorFactory.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValueGeneratorFactory.java
@@ -37,24 +37,30 @@ import java.util.Optional;
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class ConditionValueGeneratorFactory {
-    
+
+    private static final ConditionValueCompareOperatorGenerator COMPARE_OPERATOR_GENERATOR = new ConditionValueCompareOperatorGenerator();
+
+    private static final ConditionValueInOperatorGenerator IN_OPERATOR_GENERATOR = new ConditionValueInOperatorGenerator();
+
+    private static final ConditionValueBetweenOperatorGenerator BETWEEN_OPERATOR_GENERATOR = new ConditionValueBetweenOperatorGenerator();
+
     /**
      * Generate condition value.
      *
-     * @param predicate predicate right value
-     * @param column column
+     * @param predicate  predicate right value
+     * @param column     column
      * @param parameters SQL parameters
      * @return route value
      */
     public static Optional<ShardingConditionValue> generate(final ExpressionSegment predicate, final Column column, final List<Object> parameters) {
         if (predicate instanceof BinaryOperationExpression) {
-            return new ConditionValueCompareOperatorGenerator().generate((BinaryOperationExpression) predicate, column, parameters);
+            return COMPARE_OPERATOR_GENERATOR.generate((BinaryOperationExpression) predicate, column, parameters);
         }
         if (predicate instanceof InExpression) {
-            return new ConditionValueInOperatorGenerator().generate((InExpression) predicate, column, parameters);
+            return IN_OPERATOR_GENERATOR.generate((InExpression) predicate, column, parameters);
         }
         if (predicate instanceof BetweenExpression) {
-            return new ConditionValueBetweenOperatorGenerator().generate((BetweenExpression) predicate, column, parameters);
+            return BETWEEN_OPERATOR_GENERATOR.generate((BetweenExpression) predicate, column, parameters);
         }
         return Optional.empty();
     }