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