@Test public void testMask() { PartMask mask = new PartMask(); assertThat(mask.mask(null, new int[]{1})).isNull(); assertThat(mask.mask("", new int[]{1})).isEmpty(); assertThat(mask.mask("1", new int[]{1})).isEqualTo("*"); assertThat(mask.mask("123", new int[]{1})).isEqualTo("1*3"); assertThat(mask.mask("12345", new int[]{2})).isEqualTo("12*45"); assertThat(mask.mask("1234", new int[]{1, 2})).isEqualTo("1*34"); assertThat(mask.mask("1234", new int[]{1, 0})).isEqualTo("1***"); assertThat(mask.mask("1234", new int[]{0, 1})).isEqualTo("***4"); assertThat(mask.mask("1234", new int[]{0, 0})).isEqualTo("****"); assertThat(mask.mask("12", new int[]{1, 1})).isEqualTo("1*"); assertThat(mask.mask("1234", new int[]{4, 1})).isEqualTo("1***"); assertThat(mask.mask("1234", new int[]{5, 1})).isEqualTo("1***"); assertThat(mask.mask("1234", new int[]{1, 4})).isEqualTo("1***"); assertThat(mask.mask("1234", new int[]{1, 5})).isEqualTo("1***"); assertThat(mask.mask("1234", new int[]{3, 2})).isEqualTo("1***"); assertThat(mask.mask("1234", new int[]{2, 2})).isEqualTo("1***"); }
@Override public String mask(String source, int[] params) { if (source == null || source.isEmpty()) { return source; } if (source.length() == 1) { return "*"; } int leftKeep = Math.max(0, params[0]); int rightKeep = leftKeep; if (params.length > 1) { rightKeep = Math.max(0, params[1]); } boolean useDefault = leftKeep >= source.length() || rightKeep >= source.length() || (leftKeep + rightKeep) >= source .length(); if (useDefault) { return source.substring(0, 1).concat(getMaskCodeByNum(source.length() - 1)); } return source.substring(0, leftKeep).concat(getMaskCodeByNum(source.length() - leftKeep - rightKeep)) .concat(source.substring(source.length() - rightKeep, source.length())); }
PartMask implements MaskStrategy { @Override public String mask(String source, int[] params) { if (source == null || source.isEmpty()) { return source; } if (source.length() == 1) { return "*"; } int leftKeep = Math.max(0, params[0]); int rightKeep = leftKeep; if (params.length > 1) { rightKeep = Math.max(0, params[1]); } boolean useDefault = leftKeep >= source.length() || rightKeep >= source.length() || (leftKeep + rightKeep) >= source .length(); if (useDefault) { return source.substring(0, 1).concat(getMaskCodeByNum(source.length() - 1)); } return source.substring(0, leftKeep).concat(getMaskCodeByNum(source.length() - leftKeep - rightKeep)) .concat(source.substring(source.length() - rightKeep, source.length())); } }
PartMask implements MaskStrategy { @Override public String mask(String source, int[] params) { if (source == null || source.isEmpty()) { return source; } if (source.length() == 1) { return "*"; } int leftKeep = Math.max(0, params[0]); int rightKeep = leftKeep; if (params.length > 1) { rightKeep = Math.max(0, params[1]); } boolean useDefault = leftKeep >= source.length() || rightKeep >= source.length() || (leftKeep + rightKeep) >= source .length(); if (useDefault) { return source.substring(0, 1).concat(getMaskCodeByNum(source.length() - 1)); } return source.substring(0, leftKeep).concat(getMaskCodeByNum(source.length() - leftKeep - rightKeep)) .concat(source.substring(source.length() - rightKeep, source.length())); }  }
PartMask implements MaskStrategy { @Override public String mask(String source, int[] params) { if (source == null || source.isEmpty()) { return source; } if (source.length() == 1) { return "*"; } int leftKeep = Math.max(0, params[0]); int rightKeep = leftKeep; if (params.length > 1) { rightKeep = Math.max(0, params[1]); } boolean useDefault = leftKeep >= source.length() || rightKeep >= source.length() || (leftKeep + rightKeep) >= source .length(); if (useDefault) { return source.substring(0, 1).concat(getMaskCodeByNum(source.length() - 1)); } return source.substring(0, leftKeep).concat(getMaskCodeByNum(source.length() - leftKeep - rightKeep)) .concat(source.substring(source.length() - rightKeep, source.length())); }  @Override String mask(String source, int[] params); }
PartMask implements MaskStrategy { @Override public String mask(String source, int[] params) { if (source == null || source.isEmpty()) { return source; } if (source.length() == 1) { return "*"; } int leftKeep = Math.max(0, params[0]); int rightKeep = leftKeep; if (params.length > 1) { rightKeep = Math.max(0, params[1]); } boolean useDefault = leftKeep >= source.length() || rightKeep >= source.length() || (leftKeep + rightKeep) >= source .length(); if (useDefault) { return source.substring(0, 1).concat(getMaskCodeByNum(source.length() - 1)); } return source.substring(0, leftKeep).concat(getMaskCodeByNum(source.length() - leftKeep - rightKeep)) .concat(source.substring(source.length() - rightKeep, source.length())); }  @Override String mask(String source, int[] params);  }