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       * <p>
46       * Note that, as of Lang 3.1 (the origin of this code), the key to the lookup table is converted to a {@link 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       * </p>
49       *
50       * @param lookupMap Map&lt;CharSequence, CharSequence&gt; table of translator mappings, may not be null.
51       */
52      public LookupTranslator(final Map<CharSequence, CharSequence> lookupMap) {
53          Objects.requireNonNull(lookupMap, "lookupMap");
54          this.lookupMap = new HashMap<>();
55          this.prefixSet = new BitSet();
56          int currentShortest = Integer.MAX_VALUE;
57          int currentLongest = 0;
58          for (final Map.Entry<CharSequence, CharSequence> pair : lookupMap.entrySet()) {
59              this.lookupMap.put(pair.getKey().toString(), pair.getValue().toString());
60              this.prefixSet.set(pair.getKey().charAt(0));
61              final int sz = pair.getKey().length();
62              if (sz < currentShortest) {
63                  currentShortest = sz;
64              }
65              if (sz > currentLongest) {
66                  currentLongest = sz;
67              }
68          }
69          this.shortest = currentShortest;
70          this.longest = currentLongest;
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public int translate(final CharSequence input, final int index, final Writer writer) throws IOException {
78          // check if translation exists for the input at position index
79          if (prefixSet.get(input.charAt(index))) {
80              int max = longest;
81              if (index + longest > input.length()) {
82                  max = input.length() - index;
83              }
84              // implement greedy algorithm by trying maximum match first
85              for (int i = max; i >= shortest; i--) {
86                  final CharSequence subSeq = input.subSequence(index, index + i);
87                  final String result = lookupMap.get(subSeq.toString());
88                  if (result != null) {
89                      writer.write(result);
90                      return Character.codePointCount(subSeq, 0, subSeq.length());
91                  }
92              }
93          }
94          return 0;
95      }
96  }