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.algorithm.core.exception.AlgorithmInitializationException;
22  import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
23  import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
24  import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmPropertiesChecker;
25  import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
26  
27  import java.util.Properties;
28  
29  /**
30   * Keep from x to y algorithm.
31   */
32  public final class KeepFromXToYMaskAlgorithm implements MaskAlgorithm<Object, String> {
33      
34      private static final String FROM_X = "from-x";
35      
36      private static final String TO_Y = "to-y";
37      
38      private static final String REPLACE_CHAR = "replace-char";
39      
40      private Integer fromX;
41      
42      private Integer toY;
43      
44      private Character replaceChar;
45      
46      @Override
47      public void init(final Properties props) {
48          fromX = createFromX(props);
49          toY = createToY(props);
50          replaceChar = createReplaceChar(props);
51          ShardingSpherePreconditions.checkState(fromX <= toY, () -> new AlgorithmInitializationException(this, "fromX must be less than or equal to toY"));
52      }
53      
54      private Integer createFromX(final Properties props) {
55          MaskAlgorithmPropertiesChecker.checkPositiveInteger(props, FROM_X, this);
56          return Integer.parseInt(props.getProperty(FROM_X));
57      }
58      
59      private Integer createToY(final Properties props) {
60          MaskAlgorithmPropertiesChecker.checkPositiveInteger(props, TO_Y, this);
61          return Integer.parseInt(props.getProperty(TO_Y));
62      }
63      
64      private Character createReplaceChar(final Properties props) {
65          MaskAlgorithmPropertiesChecker.checkSingleChar(props, REPLACE_CHAR, this);
66          return props.getProperty(REPLACE_CHAR).charAt(0);
67      }
68      
69      @HighFrequencyInvocation
70      @Override
71      public String mask(final Object plainValue) {
72          String result = null == plainValue ? null : String.valueOf(plainValue);
73          if (Strings.isNullOrEmpty(result)) {
74              return result;
75          }
76          char[] chars = result.toCharArray();
77          for (int i = 0; i < Math.min(fromX, result.length()); i++) {
78              chars[i] = replaceChar;
79          }
80          for (int i = toY + 1; i < chars.length; i++) {
81              chars[i] = replaceChar;
82          }
83          return new String(chars);
84      }
85      
86      @Override
87      public String getType() {
88          return "KEEP_FROM_X_TO_Y";
89      }
90  }