提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.desensitize.core.slider.handler; |
H |
2 |
|
|
3 |
import com.iailab.framework.desensitize.core.base.handler.DesensitizationHandler; |
|
4 |
|
|
5 |
import java.lang.annotation.Annotation; |
|
6 |
|
|
7 |
/** |
|
8 |
* 滑动脱敏处理器抽象类,已实现通用的方法 |
|
9 |
* |
|
10 |
* @author gaibu |
|
11 |
*/ |
|
12 |
public abstract class AbstractSliderDesensitizationHandler<T extends Annotation> |
|
13 |
implements DesensitizationHandler<T> { |
|
14 |
|
|
15 |
@Override |
|
16 |
public String desensitize(String origin, T annotation) { |
|
17 |
int prefixKeep = getPrefixKeep(annotation); |
|
18 |
int suffixKeep = getSuffixKeep(annotation); |
|
19 |
String replacer = getReplacer(annotation); |
|
20 |
int length = origin.length(); |
|
21 |
|
|
22 |
// 情况一:原始字符串长度小于等于保留长度,则原始字符串全部替换 |
|
23 |
if (prefixKeep >= length || suffixKeep >= length) { |
|
24 |
return buildReplacerByLength(replacer, length); |
|
25 |
} |
|
26 |
|
|
27 |
// 情况二:原始字符串长度小于等于前后缀保留字符串长度,则原始字符串全部替换 |
|
28 |
if ((prefixKeep + suffixKeep) >= length) { |
|
29 |
return buildReplacerByLength(replacer, length); |
|
30 |
} |
|
31 |
|
|
32 |
// 情况三:原始字符串长度大于前后缀保留字符串长度,则替换中间字符串 |
|
33 |
int interval = length - prefixKeep - suffixKeep; |
|
34 |
return origin.substring(0, prefixKeep) + |
|
35 |
buildReplacerByLength(replacer, interval) + |
|
36 |
origin.substring(prefixKeep + interval); |
|
37 |
} |
|
38 |
|
|
39 |
/** |
|
40 |
* 根据长度循环构建替换符 |
|
41 |
* |
|
42 |
* @param replacer 替换符 |
|
43 |
* @param length 长度 |
|
44 |
* @return 构建后的替换符 |
|
45 |
*/ |
|
46 |
private String buildReplacerByLength(String replacer, int length) { |
|
47 |
StringBuilder builder = new StringBuilder(); |
|
48 |
for (int i = 0; i < length; i++) { |
|
49 |
builder.append(replacer); |
|
50 |
} |
|
51 |
return builder.toString(); |
|
52 |
} |
|
53 |
|
|
54 |
/** |
|
55 |
* 前缀保留长度 |
|
56 |
* |
|
57 |
* @param annotation 注解信息 |
|
58 |
* @return 前缀保留长度 |
|
59 |
*/ |
|
60 |
abstract Integer getPrefixKeep(T annotation); |
|
61 |
|
|
62 |
/** |
|
63 |
* 后缀保留长度 |
|
64 |
* |
|
65 |
* @param annotation 注解信息 |
|
66 |
* @return 后缀保留长度 |
|
67 |
*/ |
|
68 |
abstract Integer getSuffixKeep(T annotation); |
|
69 |
|
|
70 |
/** |
|
71 |
* 替换符 |
|
72 |
* |
|
73 |
* @param annotation 注解信息 |
|
74 |
* @return 替换符 |
|
75 |
*/ |
|
76 |
abstract String getReplacer(T annotation); |
|
77 |
|
|
78 |
} |