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