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