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

[shardingsphere] branch master updated: add cover mask algorithm (#22776)

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

duanzhengqiang 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 3330b72bde4 add cover mask algorithm (#22776)
3330b72bde4 is described below

commit 3330b72bde404515a76e6eab4f583057a272bb2e
Author: zzzwjZhang <46...@users.noreply.github.com>
AuthorDate: Sat Dec 10 18:00:32 2022 +0800

    add cover mask algorithm (#22776)
    
    * add KEEP_FIRST_N_LAST_M mask algorithm
    
    * rename parameter and modify unit test
    
    * optimize algorithm
    
    * add KEEP_FROM_X_TO_Y mask algorithm
    
    Co-authored-by: zhangweijie <zh...@xinshiyun.com>
---
 .../cover/KeepFirstNLastMMaskAlgorithm.java        | 88 +++++++++++++++++++++
 .../algorithm/cover/KeepFromXToYMaskAlgorithm.java | 91 ++++++++++++++++++++++
 .../KeepFirstNLastMMaskAlgorithmTest.java          | 73 +++++++++++++++++
 .../algorithm/KeepFromXToYMaskAlgorithmTest.java   | 80 +++++++++++++++++++
 4 files changed, 332 insertions(+)

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
new file mode 100644
index 00000000000..11820297a2e
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.algorithm.cover;
+
+import com.google.common.base.Preconditions;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Optional;
+import java.util.Properties;
+
+/**
+ * KEEP_FIRST_N_LAST_M Algorithm.
+ */
+public final class KeepFirstNLastMMaskAlgorithm implements MaskAlgorithm<Object, String> {
+    
+    private static final String N = "n";
+    
+    private static final String M = "m";
+    
+    private static final String REPLACE_CHAR = "replace-char";
+    
+    private Integer n;
+    
+    private Integer m;
+    
+    private Character replaceChar;
+    
+    @Getter
+    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);
+    }
+    
+    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 initMIndex(final Properties props) {
+        Preconditions.checkArgument(props.containsKey(M), "%s can not be null.", M);
+        return Integer.parseInt(props.getProperty(M));
+    }
+    
+    private Character initReplaceChar(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);
+        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);
+    }
+    
+    @Override
+    public String getType() {
+        return "KEEP_FIRST_N_LAST_M";
+    }
+}
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
new file mode 100644
index 00000000000..97ecccfa6e8
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.algorithm.cover;
+
+import com.google.common.base.Preconditions;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Optional;
+import java.util.Properties;
+
+/**
+ * KEEP_FROM_X_TO_Y Algorithm.
+ */
+public final class KeepFromXToYMaskAlgorithm implements MaskAlgorithm<Object, String> {
+    
+    private static final String X = "x";
+    
+    private static final String Y = "y";
+    
+    private static final String REPLACE_CHAR = "replace-char";
+    
+    private Integer x;
+    
+    private Integer y;
+    
+    private Character replaceChar;
+    
+    @Getter
+    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);
+    }
+    
+    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 initYIndex(final Properties props) {
+        Preconditions.checkArgument(props.containsKey(Y), "%s can not be null.", Y);
+        return Integer.parseInt(props.getProperty(Y));
+    }
+    
+    private Character initReplaceChar(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);
+        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);
+    }
+    
+    @Override
+    public String getType() {
+        return "KEEP_FROM_X_TO_Y";
+    }
+}
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
new file mode 100644
index 00000000000..ef09a1664fa
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/KeepFirstNLastMMaskAlgorithmTest.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.algorithm;
+
+import org.apache.shardingsphere.mask.algorithm.cover.KeepFirstNLastMMaskAlgorithm;
+import org.junit.Before;
+import org.junit.Test;
+
+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;
+    
+    @Before
+    public void setUp() {
+        keepFirstNLastMMaskAlgorithm = new KeepFirstNLastMMaskAlgorithm();
+        keepFirstNLastMMaskAlgorithm.init(initProperties());
+    }
+    
+    private Properties initProperties() {
+        Properties properties = new Properties();
+        properties.setProperty("n", "2");
+        properties.setProperty("m", "5");
+        properties.setProperty("replace-char", "*");
+        return properties;
+    }
+    
+    @Test
+    public void testMask() {
+        String actual = keepFirstNLastMMaskAlgorithm.mask("abc123456");
+        assertThat(actual, is("ab**23456"));
+    }
+    
+    @Test
+    public void testMaskIfPlainValueIsLess() {
+        String actual = keepFirstNLastMMaskAlgorithm.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);
+        });
+    }
+}
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
new file mode 100644
index 00000000000..2cbbb50281d
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/KeepFromXToYMaskAlgorithmTest.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.algorithm;
+
+import org.apache.shardingsphere.mask.algorithm.cover.KeepFirstNLastMMaskAlgorithm;
+import org.apache.shardingsphere.mask.algorithm.cover.KeepFromXToYMaskAlgorithm;
+import org.junit.Before;
+import org.junit.Test;
+
+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;
+    
+    @Before
+    public void setUp() {
+        keepFromXToYMaskAlgorithm = new KeepFromXToYMaskAlgorithm();
+        keepFromXToYMaskAlgorithm.init(initProperties());
+    }
+    
+    private Properties initProperties() {
+        Properties properties = new Properties();
+        properties.setProperty("x", "2");
+        properties.setProperty("y", "5");
+        properties.setProperty("replace-char", "*");
+        return properties;
+    }
+    
+    @Test
+    public void testMask() {
+        String actual = keepFromXToYMaskAlgorithm.mask("abc123456");
+        assertThat(actual, is("**c123***"));
+    }
+    
+    @Test
+    public void testMaskIfPlainValueIsLess() {
+        String actual = keepFromXToYMaskAlgorithm.mask("abc");
+        assertThat(actual, is("**c"));
+    }
+    
+    @Test
+    public void testMaskIfPlainValueIsOne() {
+        String actual = keepFromXToYMaskAlgorithm.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);
+        });
+    }
+}