View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Keep first n last m algorithm.
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  }