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 KeepFirstNLastMMaskAlgorithm implements MaskAlgorithm<Object, String> {
31
32 private static final String FIRST_N = "first-n";
33
34 private static final String LAST_M = "last-m";
35
36 private static final String REPLACE_CHAR = "replace-char";
37
38 private Integer firstN;
39
40 private Integer lastM;
41
42 private Character replaceChar;
43
44 @Override
45 public void init(final Properties props) {
46 firstN = createFirstN(props);
47 lastM = createLastM(props);
48 replaceChar = createReplaceChar(props);
49 }
50
51 private Integer createFirstN(final Properties props) {
52 MaskAlgorithmPropertiesChecker.checkPositiveInteger(props, FIRST_N, this);
53 return Integer.parseInt(props.getProperty(FIRST_N));
54 }
55
56 private Integer createLastM(final Properties props) {
57 MaskAlgorithmPropertiesChecker.checkPositiveInteger(props, LAST_M, this);
58 return Integer.parseInt(props.getProperty(LAST_M));
59 }
60
61 private Character createReplaceChar(final Properties props) {
62 MaskAlgorithmPropertiesChecker.checkSingleChar(props, REPLACE_CHAR, this);
63 return props.getProperty(REPLACE_CHAR).charAt(0);
64 }
65
66 @HighFrequencyInvocation
67 @Override
68 public String mask(final Object plainValue) {
69 String result = null == plainValue ? null : String.valueOf(plainValue);
70 if (Strings.isNullOrEmpty(result)) {
71 return result;
72 }
73 if (result.length() < firstN + lastM) {
74 return result;
75 }
76 char[] chars = result.toCharArray();
77 for (int i = firstN; i < result.length() - lastM; i++) {
78 chars[i] = replaceChar;
79 }
80 return new String(chars);
81 }
82
83 @Override
84 public String getType() {
85 return "KEEP_FIRST_N_LAST_M";
86 }
87 }