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.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   * Keep first n last m algorithm.
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  }