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.algorithm.core.exception.AlgorithmInitializationException;
22 import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
23 import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
24 import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmPropertiesChecker;
25 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
26
27 import java.util.Properties;
28
29
30
31
32 public final class KeepFromXToYMaskAlgorithm implements MaskAlgorithm<Object, String> {
33
34 private static final String FROM_X = "from-x";
35
36 private static final String TO_Y = "to-y";
37
38 private static final String REPLACE_CHAR = "replace-char";
39
40 private Integer fromX;
41
42 private Integer toY;
43
44 private Character replaceChar;
45
46 @Override
47 public void init(final Properties props) {
48 fromX = createFromX(props);
49 toY = createToY(props);
50 replaceChar = createReplaceChar(props);
51 ShardingSpherePreconditions.checkState(fromX <= toY, () -> new AlgorithmInitializationException(this, "fromX must be less than or equal to toY"));
52 }
53
54 private Integer createFromX(final Properties props) {
55 MaskAlgorithmPropertiesChecker.checkPositiveInteger(props, FROM_X, this);
56 return Integer.parseInt(props.getProperty(FROM_X));
57 }
58
59 private Integer createToY(final Properties props) {
60 MaskAlgorithmPropertiesChecker.checkPositiveInteger(props, TO_Y, this);
61 return Integer.parseInt(props.getProperty(TO_Y));
62 }
63
64 private Character createReplaceChar(final Properties props) {
65 MaskAlgorithmPropertiesChecker.checkSingleChar(props, REPLACE_CHAR, this);
66 return props.getProperty(REPLACE_CHAR).charAt(0);
67 }
68
69 @HighFrequencyInvocation
70 @Override
71 public String mask(final Object plainValue) {
72 String result = null == plainValue ? null : String.valueOf(plainValue);
73 if (Strings.isNullOrEmpty(result)) {
74 return result;
75 }
76 char[] chars = result.toCharArray();
77 for (int i = 0; i < Math.min(fromX, result.length()); i++) {
78 chars[i] = replaceChar;
79 }
80 for (int i = toY + 1; i < chars.length; i++) {
81 chars[i] = replaceChar;
82 }
83 return new String(chars);
84 }
85
86 @Override
87 public String getType() {
88 return "KEEP_FROM_X_TO_Y";
89 }
90 }