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  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   * @version $Id: LookupTranslator.java 1668348 2015-03-22 09:52:25Z britter $
29   */
30  public class LookupTranslator extends CharSequenceTranslator {
31  
32      private final HashMap<String, CharSequence> lookupMap;
33      private final HashSet<Character> prefixSet;
34      private final int shortest;
35      private final int longest;
36  
37      /**
38       * Define the lookup table to be used in translation
39       *
40       * Note that, as of Lang 3.1, the key to the lookup table is converted to a 
41       * java.lang.String, while the value remains as a java.lang.CharSequence. 
42       * This is because we need the key to support hashCode and equals(Object), 
43       * allowing it to be the key for a HashMap. See LANG-882.
44       *
45       * @param lookup CharSequence[][] table of size [*][2]
46       */
47      public LookupTranslator(final CharSequence[]... lookup) {
48          lookupMap = new HashMap<String, CharSequence>();
49          prefixSet = new HashSet<Character>();
50          int _shortest = Integer.MAX_VALUE;
51          int _longest = 0;
52          if (lookup != null) {
53              for (final CharSequence[] seq : lookup) {
54                  this.lookupMap.put(seq[0].toString(), seq[1]);
55                  this.prefixSet.add(seq[0].charAt(0));
56                  final int sz = seq[0].length();
57                  if (sz < _shortest) {
58                      _shortest = sz;
59                  }
60                  if (sz > _longest) {
61                      _longest = sz;
62                  }
63              }
64          }
65          shortest = _shortest;
66          longest = _longest;
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
74          int max = longest;
75          if (index + longest > input.length()) {
76              max = input.length() - index;
77          }
78  
79          if (!prefixSet.contains(input.charAt(index))) {
80              // no translation exists for the input at position index
81              return 0;
82          }
83  
84          // descend so as to get a greedy algorithm
85          for (int i = max; i >= shortest; i--) {
86              final CharSequence subSeq = input.subSequence(index, index + i);
87              final CharSequence result = lookupMap.get(subSeq.toString());
88  
89              if (result != null) {
90                  out.write(result.toString());
91                  return i;
92              }
93          }
94          return 0;
95      }
96  }