Coverage Report - org.apache.commons.lang3.text.translate.LookupTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
LookupTranslator
100%
25/25
92%
13/14
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  
 
 23  
 /**
 24  
  * Translates a value using a lookup table.
 25  
  * 
 26  
  * @since 3.0
 27  
  * @version $Id: LookupTranslator.java 1436770 2013-01-22 07:09:45Z ggregory $
 28  
  */
 29  
 public class LookupTranslator extends CharSequenceTranslator {
 30  
 
 31  
     private final HashMap<CharSequence, CharSequence> lookupMap;
 32  
     private final int shortest;
 33  
     private final int longest;
 34  
 
 35  
     /**
 36  
      * Define the lookup table to be used in translation
 37  
      *
 38  
      * @param lookup CharSequence[][] table of size [*][2]
 39  
      */
 40  21
     public LookupTranslator(final CharSequence[]... lookup) {
 41  21
         lookupMap = new HashMap<CharSequence, CharSequence>();
 42  21
         int _shortest = Integer.MAX_VALUE;
 43  21
         int _longest = 0;
 44  21
         if (lookup != null) {
 45  759
             for (final CharSequence[] seq : lookup) {
 46  738
                 this.lookupMap.put(seq[0], seq[1]);
 47  738
                 final int sz = seq[0].length();
 48  738
                 if (sz < _shortest) {
 49  32
                     _shortest = sz;
 50  
                 }
 51  738
                 if (sz > _longest) {
 52  28
                     _longest = sz;
 53  
                 }
 54  
             }
 55  
         }
 56  21
         shortest = _shortest;
 57  21
         longest = _longest;
 58  21
     }
 59  
 
 60  
     /**
 61  
      * {@inheritDoc}
 62  
      */
 63  
     @Override
 64  
     public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
 65  424085
         int max = longest;
 66  424085
         if (index + longest > input.length()) {
 67  70160
             max = input.length() - index;
 68  
         }
 69  
         // descend so as to get a greedy algorithm
 70  2150350
         for (int i = max; i >= shortest; i--) {
 71  1727990
             final CharSequence subSeq = input.subSequence(index, index + i);
 72  1727990
             final CharSequence result = lookupMap.get(subSeq);
 73  1727990
             if (result != null) {
 74  1725
                 out.write(result.toString());
 75  1725
                 return i;
 76  
             }
 77  
         }
 78  422360
         return 0;
 79  
     }
 80  
 }