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