Coverage Report - org.apache.commons.lang3.text.translate.LookupTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
LookupTranslator
100%
28/28
93%
15/16
5,5
 
 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  
 package org.apache.commons.lang3.text.translate;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.Writer;
 21  
 import java.util.HashMap;
 22  
 import java.util.HashSet;
 23  
 
 24  
 /**
 25  
  * Translates a value using a lookup table.
 26  
  *
 27  
  * @since 3.0
 28  
  * @deprecated as of 3.6, use commons-text
 29  
  * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/LookupTranslator.html">
 30  
  * LookupTranslator</a> instead
 31  
  */
 32  
 @Deprecated
 33  
 public class LookupTranslator extends CharSequenceTranslator {
 34  
 
 35  
     private final HashMap<String, String> lookupMap;
 36  
     private final HashSet<Character> prefixSet;
 37  
     private final int shortest;
 38  
     private final int longest;
 39  
 
 40  
     /**
 41  
      * Define the lookup table to be used in translation
 42  
      *
 43  
      * Note that, as of Lang 3.1, the key to the lookup table is converted to a
 44  
      * java.lang.String. This is because we need the key to support hashCode and
 45  
      * equals(Object), allowing it to be the key for a HashMap. See LANG-882.
 46  
      *
 47  
      * @param lookup CharSequence[][] table of size [*][2]
 48  
      */
 49  30
     public LookupTranslator(final CharSequence[]... lookup) {
 50  30
         lookupMap = new HashMap<>();
 51  30
         prefixSet = new HashSet<>();
 52  30
         int _shortest = Integer.MAX_VALUE;
 53  30
         int _longest = 0;
 54  30
         if (lookup != null) {
 55  825
             for (final CharSequence[] seq : lookup) {
 56  795
                 this.lookupMap.put(seq[0].toString(), seq[1].toString());
 57  795
                 this.prefixSet.add(seq[0].charAt(0));
 58  795
                 final int sz = seq[0].length();
 59  795
                 if (sz < _shortest) {
 60  41
                     _shortest = sz;
 61  
                 }
 62  795
                 if (sz > _longest) {
 63  37
                     _longest = sz;
 64  
                 }
 65  
             }
 66  
         }
 67  30
         shortest = _shortest;
 68  30
         longest = _longest;
 69  30
     }
 70  
 
 71  
     /**
 72  
      * {@inheritDoc}
 73  
      */
 74  
     @Override
 75  
     public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
 76  
         // check if translation exists for the input at position index
 77  405935
         if (prefixSet.contains(input.charAt(index))) {
 78  394739
             int max = longest;
 79  394739
             if (index + longest > input.length()) {
 80  69677
                 max = input.length() - index;
 81  
             }
 82  
             // implement greedy algorithm by trying maximum match first
 83  2090114
             for (int i = max; i >= shortest; i--) {
 84  1696825
                 final CharSequence subSeq = input.subSequence(index, index + i);
 85  1696825
                 final String result = lookupMap.get(subSeq.toString());
 86  
 
 87  1696825
                 if (result != null) {
 88  1450
                     out.write(result);
 89  1450
                     return i;
 90  
                 }
 91  
             }
 92  
         }
 93  404485
         return 0;
 94  
     }
 95  
 }