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   * Mask after special-chars algorithm.
29   */
30  public final class MaskAfterSpecialCharsAlgorithm implements MaskAlgorithm<Object, String> {
31      
32      private static final String SPECIAL_CHARS = "special-chars";
33      
34      private static final String REPLACE_CHAR = "replace-char";
35      
36      private String specialChars;
37      
38      private Character replaceChar;
39      
40      @Override
41      public void init(final Properties props) {
42          specialChars = createSpecialChars(props);
43          replaceChar = createReplaceChar(props);
44      }
45      
46      private String createSpecialChars(final Properties props) {
47          MaskAlgorithmPropertiesChecker.checkAtLeastOneChar(props, SPECIAL_CHARS, this);
48          return props.getProperty(SPECIAL_CHARS);
49      }
50      
51      private Character createReplaceChar(final Properties props) {
52          MaskAlgorithmPropertiesChecker.checkSingleChar(props, REPLACE_CHAR, this);
53          return props.getProperty(REPLACE_CHAR).charAt(0);
54      }
55      
56      @HighFrequencyInvocation
57      @Override
58      public String mask(final Object plainValue) {
59          String result = null == plainValue ? null : String.valueOf(plainValue);
60          if (Strings.isNullOrEmpty(result)) {
61              return result;
62          }
63          int index = result.contains(specialChars) ? result.indexOf(specialChars) + specialChars.length() : -1;
64          char[] chars = result.toCharArray();
65          for (int i = index; i != -1 && i < chars.length; i++) {
66              chars[i] = replaceChar;
67          }
68          return new String(chars);
69      }
70      
71      @Override
72      public String getType() {
73          return "MASK_AFTER_SPECIAL_CHARS";
74      }
75  }