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    *      https://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.commons.text.translate;
19  
20  import java.io.IOException;
21  import java.io.Writer;
22  import java.util.BitSet;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Objects;
26  
27  /**
28   * Translates a value using a lookup table.
29   *
30   * @since 1.0
31   */
32  public class LookupTranslator extends CharSequenceTranslator {
33  
34      /** The mapping to be used in translation. */
35      private final Map<String, String> lookupMap;
36      /** The first character of each key in the lookupMap. */
37      private final BitSet prefixSet;
38      /** The length of the shortest key in the lookupMap. */
39      private final int shortest;
40      /** The length of the longest key in the lookupMap. */
41      private final int longest;
42  
43      /**
44       * Constructs the lookup table to be used in translation.
45       *
46       * Note that, as of Lang 3.1 (the origin of this code), the key to the lookup table is converted to a java.lang.String. This is because we need the key to
47       * support hashCode and equals(Object), allowing it to be the key for a HashMap. See LANG-882.
48       *
49       * @param lookupMap Map&lt;CharSequence, CharSequence&gt; table of translator mappings, may not be null.
50       */
51      public LookupTranslator(final Map<CharSequence, CharSequence> lookupMap) {
52          Objects.requireNonNull(lookupMap, "lookupMap");
53          this.lookupMap = new HashMap<>();
54          this.prefixSet = new BitSet();
55          int currentShortest = Integer.MAX_VALUE;
56          int currentLongest = 0;
57          for (final Map.Entry<CharSequence, CharSequence> pair : lookupMap.entrySet()) {
58              this.lookupMap.put(pair.getKey().toString(), pair.getValue().toString());
59              this.prefixSet.set(pair.getKey().charAt(0));
60              final int sz = pair.getKey().length();
61              if (sz < currentShortest) {
62                  currentShortest = sz;
63              }
64              if (sz > currentLongest) {
65                  currentLongest = sz;
66              }
67          }
68          this.shortest = currentShortest;
69          this.longest = currentLongest;
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public int translate(final CharSequence input, final int index, final Writer writer) throws IOException {
77          // check if translation exists for the input at position index
78          if (prefixSet.get(input.charAt(index))) {
79              int max = longest;
80              if (index + longest > input.length()) {
81                  max = input.length() - index;
82              }
83              // implement greedy algorithm by trying maximum match first
84              for (int i = max; i >= shortest; i--) {
85                  final CharSequence subSeq = input.subSequence(index, index + i);
86                  final String result = lookupMap.get(subSeq.toString());
87                  if (result != null) {
88                      writer.write(result);
89                      return Character.codePointCount(subSeq, 0, subSeq.length());
90                  }
91              }
92          }
93          return 0;
94      }
95  }