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

panjuan 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 2badc4c0440 Refactor throw IllegalStateException to 
Preconditions.checkState (#21124)
2badc4c0440 is described below

commit 2badc4c0440ea579204f6ca77a772770c3d6f6d8
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Sep 22 11:25:39 2022 +0800

    Refactor throw IllegalStateException to Preconditions.checkState (#21124)
---
 .../loadbalance/WeightReadQueryLoadBalanceAlgorithm.java  |  5 ++---
 .../proxy/backend/config/ProxyConfigurationLoader.java    | 15 ++++++---------
 .../postgresql/command/PostgreSQLConnectionContext.java   |  7 +++----
 .../postgresql/command/query/extended/JDBCPortal.java     |  2 +-
 4 files changed, 12 insertions(+), 17 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
 
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
index 55b21bb1679..4f733e5e743 100644
--- 
a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
+++ 
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
@@ -68,9 +68,8 @@ public final class WeightReadQueryLoadBalanceAlgorithm 
implements ReadQueryLoadB
     
     private double[] initWeight(final List<String> readDataSourceNames) {
         double[] result = getWeights(readDataSourceNames);
-        if (result.length != 0 && Math.abs(result[result.length - 1] - 1.0D) 
>= ACCURACY_THRESHOLD) {
-            throw new IllegalStateException("The cumulative weight is 
calculated incorrectly, and the sum of the probabilities is not equal to 1.");
-        }
+        Preconditions.checkState(result.length == 0 || 
!(Math.abs(result[result.length - 1] - 1.0D) >= ACCURACY_THRESHOLD),
+                "The cumulative weight is calculated incorrectly, and the sum 
of the probabilities is not equal to 1");
         return result;
     }
     
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
index db1d02cd9a1..e81e3ea3c06 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
@@ -120,19 +120,16 @@ public final class ProxyConfigurationLoader {
         }
         Map<Class<? extends RuleConfiguration>, Long> ruleConfigTypeCountMap = 
ruleConfigurations.stream()
                 
.collect(Collectors.groupingBy(YamlRuleConfiguration::getRuleConfigurationType, 
Collectors.counting()));
-        Optional<Entry<Class<? extends RuleConfiguration>, Long>> 
duplicateRuleConfiguration = 
ruleConfigTypeCountMap.entrySet().stream().filter(each -> each.getValue() > 
1).findFirst();
-        if (duplicateRuleConfiguration.isPresent()) {
-            throw new IllegalStateException(String.format("Duplicate rule tag 
'!%s' in file %s.", 
getDuplicateRuleTagName(duplicateRuleConfiguration.get().getKey()), 
yamlFile.getName()));
+        Optional<Entry<Class<? extends RuleConfiguration>, Long>> 
duplicateRuleConfig = ruleConfigTypeCountMap.entrySet().stream().filter(each -> 
each.getValue() > 1).findFirst();
+        if (duplicateRuleConfig.isPresent()) {
+            throw new IllegalStateException(String.format("Duplicate rule tag 
`!%s` in file `%s`", 
getDuplicateRuleTagName(duplicateRuleConfig.get().getKey()), 
yamlFile.getName()));
         }
     }
     
     @SuppressWarnings("rawtypes")
-    private static Object getDuplicateRuleTagName(final Class<? extends 
RuleConfiguration> ruleConfigurationClass) {
-        Optional<YamlRuleConfigurationSwapper> optional = 
YamlRuleConfigurationSwapperFactory.getAllInstances().stream().filter(each -> 
ruleConfigurationClass.equals(each.getTypeClass())).findFirst();
-        if (optional.isPresent()) {
-            return optional.get().getRuleTagName();
-        }
-        throw new IllegalStateException("Not find rule tag name of class " + 
ruleConfigurationClass);
+    private static Object getDuplicateRuleTagName(final Class<? extends 
RuleConfiguration> ruleConfigClass) {
+        Optional<YamlRuleConfigurationSwapper> result = 
YamlRuleConfigurationSwapperFactory.getAllInstances().stream().filter(each -> 
ruleConfigClass.equals(each.getTypeClass())).findFirst();
+        return result.orElseThrow(() -> new IllegalStateException("Not find 
rule tag name of class " + ruleConfigClass));
     }
     
     private static File[] findRuleConfigurationFiles(final File path) {
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLConnectionContext.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLConnectionContext.java
index ae3495c2a7a..e70951acc3b 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLConnectionContext.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLConnectionContext.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.proxy.frontend.postgresql.command;
 
+import com.google.common.base.Preconditions;
 import 
org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.Portal;
 
 import java.util.LinkedHashMap;
@@ -36,9 +37,7 @@ public final class PostgreSQLConnectionContext {
      */
     public void addPortal(final Portal<?> portal) {
         boolean isNamedPortal = !portal.getName().isEmpty();
-        if (isNamedPortal && portals.containsKey(portal.getName())) {
-            throw new IllegalStateException("Named portal [" + 
portal.getName() + "] must be explicitly closed");
-        }
+        Preconditions.checkState(!isNamedPortal || 
!portals.containsKey(portal.getName()), "Named portal `%s` must be explicitly 
closed", portal.getName());
         Portal<?> previousPortal = portals.put(portal.getName(), portal);
         if (null != previousPortal) {
             previousPortal.close();
@@ -48,7 +47,7 @@ public final class PostgreSQLConnectionContext {
     /**
      * Get portal.
      *
-     * @param <T> type of Portal
+     * @param <T> type of portal
      * @param portal portal name
      * @return portal
      */
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortal.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortal.java
index 5f48e4361ae..9a2a54c9ce0 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortal.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/JDBCPortal.java
@@ -122,7 +122,7 @@ public final class JDBCPortal implements Portal<Void> {
         if (responseHeader instanceof UpdateResponseHeader) {
             return PostgreSQLNoDataPacket.getInstance();
         }
-        throw new IllegalStateException("Cannot describe portal [" + name + "] 
before bind");
+        throw new IllegalStateException(String.format("Can not describe portal 
`%s` before bind", name));
     }
     
     private PostgreSQLRowDescriptionPacket createRowDescriptionPacket(final 
QueryResponseHeader queryResponseHeader) {

Reply via email to