LookupTranslator.java

  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. import java.io.IOException;
  19. import java.io.Writer;
  20. import java.util.HashMap;
  21. import java.util.HashSet;

  22. /**
  23.  * Translates a value using a lookup table.
  24.  *
  25.  * @since 3.0
  26.  * @deprecated As of 3.6, use Apache Commons Text
  27.  * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/LookupTranslator.html">
  28.  * LookupTranslator</a> instead
  29.  */
  30. @Deprecated
  31. public class LookupTranslator extends CharSequenceTranslator {

  32.     private final HashMap<String, String> lookupMap;
  33.     private final HashSet<Character> prefixSet;
  34.     private final int shortest;
  35.     private final int longest;

  36.     /**
  37.      * Define the lookup table to be used in translation
  38.      *
  39.      * Note that, as of Lang 3.1, the key to the lookup table is converted to a
  40.      * java.lang.String. This is because we need the key to support hashCode and
  41.      * equals(Object), allowing it to be the key for a HashMap. See LANG-882.
  42.      *
  43.      * @param lookup CharSequence[][] table of size [*][2]
  44.      */
  45.     public LookupTranslator(final CharSequence[]... lookup) {
  46.         lookupMap = new HashMap<>();
  47.         prefixSet = new HashSet<>();
  48.         int tmpShortest = Integer.MAX_VALUE;
  49.         int tmpLongest = 0;
  50.         if (lookup != null) {
  51.             for (final CharSequence[] seq : lookup) {
  52.                 this.lookupMap.put(seq[0].toString(), seq[1].toString());
  53.                 this.prefixSet.add(seq[0].charAt(0));
  54.                 final int sz = seq[0].length();
  55.                 if (sz < tmpShortest) {
  56.                     tmpShortest = sz;
  57.                 }
  58.                 if (sz > tmpLongest) {
  59.                     tmpLongest = sz;
  60.                 }
  61.             }
  62.         }
  63.         this.shortest = tmpShortest;
  64.         this.longest = tmpLongest;
  65.     }

  66.     /**
  67.      * {@inheritDoc}
  68.      */
  69.     @Override
  70.     public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
  71.         // check if translation exists for the input at position index
  72.         if (prefixSet.contains(input.charAt(index))) {
  73.             int max = longest;
  74.             if (index + longest > input.length()) {
  75.                 max = input.length() - index;
  76.             }
  77.             // implement greedy algorithm by trying maximum match first
  78.             for (int i = max; i >= shortest; i--) {
  79.                 final CharSequence subSeq = input.subSequence(index, index + i);
  80.                 final String result = lookupMap.get(subSeq.toString());

  81.                 if (result != null) {
  82.                     out.write(result);
  83.                     return i;
  84.                 }
  85.             }
  86.         }
  87.         return 0;
  88.     }
  89. }