1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.mask.algorithm.cover;
19
20 import com.google.common.base.Strings;
21 import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
22 import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmPropertiesChecker;
23 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
24
25 import java.util.Properties;
26
27
28
29
30 public final class MaskAfterSpecialCharsAlgorithm implements MaskAlgorithm<Object, String> {
31
32 private static final String SPECIAL_CHARS = "special-chars";
33
34 private static final String REPLACE_CHAR = "replace-char";
35
36 private String specialChars;
37
38 private Character replaceChar;
39
40 @Override
41 public void init(final Properties props) {
42 specialChars = createSpecialChars(props);
43 replaceChar = createReplaceChar(props);
44 }
45
46 private String createSpecialChars(final Properties props) {
47 MaskAlgorithmPropertiesChecker.checkAtLeastOneChar(props, SPECIAL_CHARS, this);
48 return props.getProperty(SPECIAL_CHARS);
49 }
50
51 private Character createReplaceChar(final Properties props) {
52 MaskAlgorithmPropertiesChecker.checkSingleChar(props, REPLACE_CHAR, this);
53 return props.getProperty(REPLACE_CHAR).charAt(0);
54 }
55
56 @HighFrequencyInvocation
57 @Override
58 public String mask(final Object plainValue) {
59 String result = null == plainValue ? null : String.valueOf(plainValue);
60 if (Strings.isNullOrEmpty(result)) {
61 return result;
62 }
63 int index = result.contains(specialChars) ? result.indexOf(specialChars) + specialChars.length() : -1;
64 char[] chars = result.toCharArray();
65 for (int i = index; i != -1 && i < chars.length; i++) {
66 chars[i] = replaceChar;
67 }
68 return new String(chars);
69 }
70
71 @Override
72 public String getType() {
73 return "MASK_AFTER_SPECIAL_CHARS";
74 }
75 }