You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by tu...@apache.org on 2022/12/12 01:51:33 UTC

[shardingsphere] branch master updated: Revise pr 22776 for code style (#22814)

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

tuichenchuxin 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 d55dd6dcc5e Revise pr 22776 for code style (#22814)
d55dd6dcc5e is described below

commit d55dd6dcc5e6bc6cfc92a89b50de62f1142a4997
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Mon Dec 12 09:51:26 2022 +0800

    Revise pr 22776 for code style (#22814)
---
 .../cover/KeepFirstNLastMMaskAlgorithm.java        | 61 ++++++++++----------
 .../algorithm/cover/KeepFromXToYMaskAlgorithm.java | 67 +++++++++++-----------
 .../KeepFirstNLastMMaskAlgorithmTest.java          | 45 ++++++---------
 .../algorithm/KeepFromXToYMaskAlgorithmTest.java   | 49 +++++++---------
 4 files changed, 107 insertions(+), 115 deletions(-)

diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java
index 11820297a2e..ca50840ebe8 100644
--- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java
@@ -18,26 +18,26 @@
 package org.apache.shardingsphere.mask.algorithm.cover;
 
 import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
 import lombok.Getter;
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
-import java.util.Optional;
 import java.util.Properties;
 
 /**
- * KEEP_FIRST_N_LAST_M Algorithm.
+ * Keep first n last m algorithm.
  */
 public final class KeepFirstNLastMMaskAlgorithm implements MaskAlgorithm<Object, String> {
     
-    private static final String N = "n";
+    private static final String FIRST_N = "first-n";
     
-    private static final String M = "m";
+    private static final String LAST_M = "last-m";
     
     private static final String REPLACE_CHAR = "replace-char";
     
-    private Integer n;
+    private Integer firstN;
     
-    private Integer m;
+    private Integer lastM;
     
     private Character replaceChar;
     
@@ -45,40 +45,43 @@ public final class KeepFirstNLastMMaskAlgorithm implements MaskAlgorithm<Object,
     private Properties props;
     
     @Override
-    public String mask(final Object plainValue) {
-        String value = Optional.ofNullable(plainValue).orElse("").toString();
-        if ("".equals(value) || value.length() <= n + m) {
-            return value;
-        }
-        char[] chars = value.toCharArray();
-        for (int i = n; i < value.length() - m; i++) {
-            chars[i] = replaceChar;
-        }
-        return new String(chars);
+    public void init(final Properties props) {
+        this.props = props;
+        this.firstN = createFirstN(props);
+        this.lastM = createLastM(props);
+        this.replaceChar = createReplaceChar(props);
     }
     
-    private Integer initNIndex(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(N), "%s can not be null.", N);
-        return Integer.parseInt(props.getProperty(N));
+    private Integer createFirstN(final Properties props) {
+        Preconditions.checkArgument(props.containsKey(FIRST_N), "%s can not be null.", FIRST_N);
+        return Integer.parseInt(props.getProperty(FIRST_N));
     }
     
-    private Integer initMIndex(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(M), "%s can not be null.", M);
-        return Integer.parseInt(props.getProperty(M));
+    private Integer createLastM(final Properties props) {
+        Preconditions.checkArgument(props.containsKey(LAST_M), "%s can not be null.", LAST_M);
+        return Integer.parseInt(props.getProperty(LAST_M));
     }
     
-    private Character initReplaceChar(final Properties props) {
+    private Character createReplaceChar(final Properties props) {
         Preconditions.checkArgument(props.containsKey(REPLACE_CHAR), "%s can not be null.", REPLACE_CHAR);
-        Preconditions.checkArgument(props.getProperty(REPLACE_CHAR).length() == 1, "%s length must be 1.", REPLACE_CHAR);
+        Preconditions.checkArgument(1 == props.getProperty(REPLACE_CHAR).length(), "%s's length must be one.", REPLACE_CHAR);
         return props.getProperty(REPLACE_CHAR).charAt(0);
     }
     
     @Override
-    public void init(final Properties props) {
-        this.props = props;
-        this.n = initNIndex(props);
-        this.m = initMIndex(props);
-        this.replaceChar = initReplaceChar(props);
+    public String mask(final Object plainValue) {
+        String result = String.valueOf(plainValue);
+        if (Strings.isNullOrEmpty(result)) {
+            return result;
+        }
+        if (result.length() < firstN + lastM) {
+            return result;
+        }
+        char[] chars = result.toCharArray();
+        for (int i = firstN; i < result.length() - lastM; i++) {
+            chars[i] = replaceChar;
+        }
+        return new String(chars);
     }
     
     @Override
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
index 97ecccfa6e8..128f24b9c06 100644
--- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
@@ -18,26 +18,26 @@
 package org.apache.shardingsphere.mask.algorithm.cover;
 
 import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
 import lombok.Getter;
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
-import java.util.Optional;
 import java.util.Properties;
 
 /**
- * KEEP_FROM_X_TO_Y Algorithm.
+ * Keep from x to y algorithm.
  */
 public final class KeepFromXToYMaskAlgorithm implements MaskAlgorithm<Object, String> {
     
-    private static final String X = "x";
+    private static final String FROM_X = "from-x";
     
-    private static final String Y = "y";
+    private static final String TO_Y = "to-y";
     
     private static final String REPLACE_CHAR = "replace-char";
     
-    private Integer x;
+    private Integer fromX;
     
-    private Integer y;
+    private Integer toY;
     
     private Character replaceChar;
     
@@ -45,43 +45,46 @@ public final class KeepFromXToYMaskAlgorithm implements MaskAlgorithm<Object, St
     private Properties props;
     
     @Override
-    public String mask(final Object plainValue) {
-        String value = Optional.ofNullable(plainValue).orElse("").toString();
-        if ("".equals(value) || value.length() <= x || y <= x) {
-            return value;
-        }
-        char[] chars = value.toCharArray();
-        for (int i = 0; i < x; i++) {
-            chars[i] = replaceChar;
-        }
-        for (int i = y + 1; i < chars.length; i++) {
-            chars[i] = replaceChar;
-        }
-        return new String(chars);
+    public void init(final Properties props) {
+        this.props = props;
+        this.fromX = createFromX(props);
+        this.toY = createToY(props);
+        this.replaceChar = createReplaceChar(props);
     }
     
-    private Integer initXIndex(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(X), "%s can not be null.", X);
-        return Integer.parseInt(props.getProperty(X));
+    private Integer createFromX(final Properties props) {
+        Preconditions.checkArgument(props.containsKey(FROM_X), "%s can not be null.", FROM_X);
+        return Integer.parseInt(props.getProperty(FROM_X));
     }
     
-    private Integer initYIndex(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(Y), "%s can not be null.", Y);
-        return Integer.parseInt(props.getProperty(Y));
+    private Integer createToY(final Properties props) {
+        Preconditions.checkArgument(props.containsKey(TO_Y), "%s can not be null.", TO_Y);
+        return Integer.parseInt(props.getProperty(TO_Y));
     }
     
-    private Character initReplaceChar(final Properties props) {
+    private Character createReplaceChar(final Properties props) {
         Preconditions.checkArgument(props.containsKey(REPLACE_CHAR), "%s can not be null.", REPLACE_CHAR);
-        Preconditions.checkArgument(props.getProperty(REPLACE_CHAR).length() == 1, "%s length must be 1.", REPLACE_CHAR);
+        Preconditions.checkArgument(props.getProperty(REPLACE_CHAR).length() == 1, "%s's length must be one.", REPLACE_CHAR);
         return props.getProperty(REPLACE_CHAR).charAt(0);
     }
     
     @Override
-    public void init(final Properties props) {
-        this.props = props;
-        this.x = initXIndex(props);
-        this.y = initYIndex(props);
-        this.replaceChar = initReplaceChar(props);
+    public String mask(final Object plainValue) {
+        String result = String.valueOf(plainValue);
+        if (Strings.isNullOrEmpty(result)) {
+            return result;
+        }
+        if (result.length() <= fromX || toY <= fromX) {
+            return result;
+        }
+        char[] chars = result.toCharArray();
+        for (int i = 0; i < fromX; i++) {
+            chars[i] = replaceChar;
+        }
+        for (int i = toY + 1; i < chars.length; i++) {
+            chars[i] = replaceChar;
+        }
+        return new String(chars);
     }
     
     @Override
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/KeepFirstNLastMMaskAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/KeepFirstNLastMMaskAlgorithmTest.java
index ef09a1664fa..5d5e75073f2 100644
--- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/KeepFirstNLastMMaskAlgorithmTest.java
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/KeepFirstNLastMMaskAlgorithmTest.java
@@ -25,49 +25,42 @@ import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertThrows;
 
-/**
- * KEEP_FIRST_N_LAST_M test.
- */
 public final class KeepFirstNLastMMaskAlgorithmTest {
     
-    private KeepFirstNLastMMaskAlgorithm keepFirstNLastMMaskAlgorithm;
+    private KeepFirstNLastMMaskAlgorithm maskAlgorithm;
     
     @Before
     public void setUp() {
-        keepFirstNLastMMaskAlgorithm = new KeepFirstNLastMMaskAlgorithm();
-        keepFirstNLastMMaskAlgorithm.init(initProperties());
+        maskAlgorithm = new KeepFirstNLastMMaskAlgorithm();
+        maskAlgorithm.init(createProperties("2", "5", "*"));
     }
     
-    private Properties initProperties() {
-        Properties properties = new Properties();
-        properties.setProperty("n", "2");
-        properties.setProperty("m", "5");
-        properties.setProperty("replace-char", "*");
-        return properties;
+    private Properties createProperties(final String firstN, final String lastM, final String replaceChar) {
+        Properties result = new Properties();
+        result.setProperty("first-n", firstN);
+        result.setProperty("last-m", lastM);
+        result.setProperty("replace-char", replaceChar);
+        return result;
     }
     
     @Test
-    public void testMask() {
-        String actual = keepFirstNLastMMaskAlgorithm.mask("abc123456");
+    public void assertMask() {
+        String actual = maskAlgorithm.mask("abc123456");
         assertThat(actual, is("ab**23456"));
     }
     
     @Test
-    public void testMaskIfPlainValueIsLess() {
-        String actual = keepFirstNLastMMaskAlgorithm.mask("abc");
+    public void assertMaskWhenPlainValueLengthLessThenFirstNLastMSum() {
+        String actual = maskAlgorithm.mask("abc");
         assertThat(actual, is("abc"));
     }
     
-    @Test
-    public void testNotSetStartIndex() {
-        KeepFirstNLastMMaskAlgorithm keepFirstNLastMMaskAlgorithm1 = new KeepFirstNLastMMaskAlgorithm();
-        Properties wrongProperties = new Properties();
-        wrongProperties.setProperty("m", "5");
-        wrongProperties.setProperty("replace-char", "*");
-        assertThrows(IllegalArgumentException.class, () -> {
-            keepFirstNLastMMaskAlgorithm1.init(wrongProperties);
-        });
+    @Test(expected = IllegalArgumentException.class)
+    public void assertInitWhenConfigWrongProps() {
+        KeepFirstNLastMMaskAlgorithm maskAlgorithm = new KeepFirstNLastMMaskAlgorithm();
+        maskAlgorithm.init(createProperties("", "3", "+"));
+        maskAlgorithm.init(createProperties("2", "", "+"));
+        maskAlgorithm.init(createProperties("2", "5", ""));
     }
 }
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/KeepFromXToYMaskAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/KeepFromXToYMaskAlgorithmTest.java
index 2cbbb50281d..8434f96abf1 100644
--- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/KeepFromXToYMaskAlgorithmTest.java
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/KeepFromXToYMaskAlgorithmTest.java
@@ -26,55 +26,48 @@ import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertThrows;
 
-/**
- * KEEP_FROM_X_TO_Y test.
- */
 public final class KeepFromXToYMaskAlgorithmTest {
     
-    private KeepFromXToYMaskAlgorithm keepFromXToYMaskAlgorithm;
+    private KeepFromXToYMaskAlgorithm maskAlgorithm;
     
     @Before
     public void setUp() {
-        keepFromXToYMaskAlgorithm = new KeepFromXToYMaskAlgorithm();
-        keepFromXToYMaskAlgorithm.init(initProperties());
+        maskAlgorithm = new KeepFromXToYMaskAlgorithm();
+        maskAlgorithm.init(createProperties("2", "5", "*"));
     }
     
-    private Properties initProperties() {
-        Properties properties = new Properties();
-        properties.setProperty("x", "2");
-        properties.setProperty("y", "5");
-        properties.setProperty("replace-char", "*");
-        return properties;
+    private Properties createProperties(final String fromX, final String toY, final String replaceChar) {
+        Properties result = new Properties();
+        result.setProperty("from-x", fromX);
+        result.setProperty("to-y", toY);
+        result.setProperty("replace-char", replaceChar);
+        return result;
     }
     
     @Test
-    public void testMask() {
-        String actual = keepFromXToYMaskAlgorithm.mask("abc123456");
+    public void assertMask() {
+        String actual = maskAlgorithm.mask("abc123456");
         assertThat(actual, is("**c123***"));
     }
     
     @Test
-    public void testMaskIfPlainValueIsLess() {
-        String actual = keepFromXToYMaskAlgorithm.mask("abc");
+    public void assertMaskWhenPlainValueLengthLessThanToY() {
+        String actual = maskAlgorithm.mask("abc");
         assertThat(actual, is("**c"));
     }
     
     @Test
-    public void testMaskIfPlainValueIsOne() {
-        String actual = keepFromXToYMaskAlgorithm.mask("a");
+    public void assertMaskWhenPlainValueLengthLessThanFromX() {
+        String actual = maskAlgorithm.mask("a");
         assertThat(actual, is("a"));
     }
     
-    @Test
-    public void testNotSetStartIndex() {
-        KeepFirstNLastMMaskAlgorithm keepFirstNLastMMaskAlgorithm1 = new KeepFirstNLastMMaskAlgorithm();
-        Properties wrongProperties = new Properties();
-        wrongProperties.setProperty("m", "5");
-        wrongProperties.setProperty("replace-char", "*");
-        assertThrows(IllegalArgumentException.class, () -> {
-            keepFirstNLastMMaskAlgorithm1.init(wrongProperties);
-        });
+    @Test(expected = IllegalArgumentException.class)
+    public void assertInitWhenConfigWrongProps() {
+        KeepFirstNLastMMaskAlgorithm maskAlgorithm = new KeepFirstNLastMMaskAlgorithm();
+        maskAlgorithm.init(createProperties("", "3", "+"));
+        maskAlgorithm.init(createProperties("2", "", "+"));
+        maskAlgorithm.init(createProperties("2", "5", ""));
     }
 }