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