1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.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
26
27
28
29 public class LookupTranslator extends CharSequenceTranslator {
30
31 private final HashMap<String, String> lookupMap;
32 private final HashSet<Character> prefixSet;
33 private final int shortest;
34 private final int longest;
35
36
37
38
39
40
41
42
43
44
45 public LookupTranslator(final CharSequence[]... lookup) {
46 lookupMap = new HashMap<>();
47 prefixSet = new HashSet<>();
48 int _shortest = Integer.MAX_VALUE;
49 int _longest = 0;
50 if (lookup != null) {
51 for (final CharSequence[] seq : lookup) {
52 this.lookupMap.put(seq[0].toString(), seq[1].toString());
53 this.prefixSet.add(seq[0].charAt(0));
54 final int sz = seq[0].length();
55 if (sz < _shortest) {
56 _shortest = sz;
57 }
58 if (sz > _longest) {
59 _longest = sz;
60 }
61 }
62 }
63 shortest = _shortest;
64 longest = _longest;
65 }
66
67
68
69
70 @Override
71 public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
72
73 if (prefixSet.contains(input.charAt(index))) {
74 int max = longest;
75 if (index + longest > input.length()) {
76 max = input.length() - index;
77 }
78
79 for (int i = max; i >= shortest; i--) {
80 final CharSequence subSeq = input.subSequence(index, index + i);
81 final String result = lookupMap.get(subSeq.toString());
82
83 if (result != null) {
84 out.write(result);
85 return i;
86 }
87 }
88 }
89 return 0;
90 }
91 }