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
37 /** The first character of each key in the lookupMap. */
38 private final BitSet prefixSet;
39
40 /** The length of the shortest key in the lookupMap. */
41 private final int shortest;
42
43 /** The length of the longest key in the lookupMap. */
44 private final int longest;
45
46 /**
47 * Constructs the lookup table to be used in translation.
48 * <p>
49 * 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
50 * support hashCode and equals(Object), allowing it to be the key for a HashMap. See LANG-882.
51 * </p>
52 *
53 * @param lookupMap Map<CharSequence, CharSequence> table of translator mappings, may not be null.
54 */
55 public LookupTranslator(final Map<CharSequence, CharSequence> lookupMap) {
56 Objects.requireNonNull(lookupMap, "lookupMap");
57 this.lookupMap = new HashMap<>();
58 this.prefixSet = new BitSet();
59 int currentShortest = Integer.MAX_VALUE;
60 int currentLongest = 0;
61 for (final Map.Entry<CharSequence, CharSequence> pair : lookupMap.entrySet()) {
62 this.lookupMap.put(pair.getKey().toString(), pair.getValue().toString());
63 this.prefixSet.set(pair.getKey().charAt(0));
64 final int sz = pair.getKey().length();
65 if (sz < currentShortest) {
66 currentShortest = sz;
67 }
68 if (sz > currentLongest) {
69 currentLongest = sz;
70 }
71 }
72 this.shortest = currentShortest;
73 this.longest = currentLongest;
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 public int translate(final CharSequence input, final int index, final Writer writer) throws IOException {
81 // check if translation exists for the input at position index
82 if (prefixSet.get(input.charAt(index))) {
83 int max = longest;
84 if (index + longest > input.length()) {
85 max = input.length() - index;
86 }
87 // implement greedy algorithm by trying maximum match first
88 for (int i = max; i >= shortest; i--) {
89 final CharSequence subSeq = input.subSequence(index, index + i);
90 final String result = lookupMap.get(subSeq.toString());
91 if (result != null) {
92 writer.write(result);
93 return Character.codePointCount(subSeq, 0, subSeq.length());
94 }
95 }
96 }
97 return 0;
98 }
99 }