6029 |
28 Oct 20 |
nicklas |
1 |
package net.sf.basedb.reggie.converter; |
6029 |
28 Oct 20 |
nicklas |
2 |
|
6029 |
28 Oct 20 |
nicklas |
3 |
import java.util.HashMap; |
6029 |
28 Oct 20 |
nicklas |
4 |
import java.util.Map; |
6029 |
28 Oct 20 |
nicklas |
5 |
|
6029 |
28 Oct 20 |
nicklas |
6 |
/** |
6029 |
28 Oct 20 |
nicklas |
Map and converter implementation that can translate a given set |
6029 |
28 Oct 20 |
nicklas |
of values. If no translation is found the original value is returned. |
6029 |
28 Oct 20 |
nicklas |
@since 4.28 |
6029 |
28 Oct 20 |
nicklas |
10 |
*/ |
6029 |
28 Oct 20 |
nicklas |
11 |
public class Translater |
6029 |
28 Oct 20 |
nicklas |
12 |
extends HashMap<String, String> |
6029 |
28 Oct 20 |
nicklas |
13 |
implements ValueConverter<String, String> |
6029 |
28 Oct 20 |
nicklas |
14 |
{ |
6029 |
28 Oct 20 |
nicklas |
15 |
|
6029 |
28 Oct 20 |
nicklas |
16 |
private static final long serialVersionUID = 1223355400967371440L; |
6029 |
28 Oct 20 |
nicklas |
17 |
|
6029 |
28 Oct 20 |
nicklas |
18 |
/** |
6029 |
28 Oct 20 |
nicklas |
Creates a new empty translater. |
6029 |
28 Oct 20 |
nicklas |
20 |
*/ |
6029 |
28 Oct 20 |
nicklas |
21 |
public Translater() |
6029 |
28 Oct 20 |
nicklas |
22 |
{} |
6029 |
28 Oct 20 |
nicklas |
23 |
|
6029 |
28 Oct 20 |
nicklas |
24 |
/** |
6029 |
28 Oct 20 |
nicklas |
Create a new translater and fill it with translations. |
6029 |
28 Oct 20 |
nicklas |
26 |
*/ |
6029 |
28 Oct 20 |
nicklas |
27 |
public Translater(Map<? extends String, ? extends String> translations) |
6029 |
28 Oct 20 |
nicklas |
28 |
{ |
6029 |
28 Oct 20 |
nicklas |
29 |
super(translations); |
6029 |
28 Oct 20 |
nicklas |
30 |
} |
6029 |
28 Oct 20 |
nicklas |
31 |
|
6029 |
28 Oct 20 |
nicklas |
32 |
/** |
6966 |
10 Jan 23 |
nicklas |
Create a new translater and fill it with translations. The keys and translations |
6966 |
10 Jan 23 |
nicklas |
are given as a single array which must have an even number of elements. |
6966 |
10 Jan 23 |
nicklas |
@since 4.42 |
6966 |
10 Jan 23 |
nicklas |
36 |
*/ |
6966 |
10 Jan 23 |
nicklas |
37 |
public Translater(String... keysAndValues) |
6966 |
10 Jan 23 |
nicklas |
38 |
{ |
6966 |
10 Jan 23 |
nicklas |
39 |
if (keysAndValues != null) |
6966 |
10 Jan 23 |
nicklas |
40 |
{ |
6966 |
10 Jan 23 |
nicklas |
41 |
if (keysAndValues.length % 2 != 0) |
6966 |
10 Jan 23 |
nicklas |
42 |
{ |
6966 |
10 Jan 23 |
nicklas |
43 |
throw new IllegalArgumentException("'keysAndValues' array must have an even number of elements."); |
6966 |
10 Jan 23 |
nicklas |
44 |
} |
6966 |
10 Jan 23 |
nicklas |
45 |
for (int i = 0; i < keysAndValues.length; i+=2) |
6966 |
10 Jan 23 |
nicklas |
46 |
{ |
6966 |
10 Jan 23 |
nicklas |
47 |
put(keysAndValues[i], keysAndValues[i+1]); |
6966 |
10 Jan 23 |
nicklas |
48 |
} |
6966 |
10 Jan 23 |
nicklas |
49 |
} |
6966 |
10 Jan 23 |
nicklas |
50 |
} |
6966 |
10 Jan 23 |
nicklas |
51 |
|
6966 |
10 Jan 23 |
nicklas |
52 |
/** |
6029 |
28 Oct 20 |
nicklas |
Get the translated value or the key (as a String) if no |
6029 |
28 Oct 20 |
nicklas |
translation is found. |
6029 |
28 Oct 20 |
nicklas |
55 |
*/ |
6029 |
28 Oct 20 |
nicklas |
56 |
@Override |
6029 |
28 Oct 20 |
nicklas |
57 |
public String get(Object key) |
6029 |
28 Oct 20 |
nicklas |
58 |
{ |
6029 |
28 Oct 20 |
nicklas |
59 |
return getOrDefault(key, key == null ? null : key.toString()); |
6029 |
28 Oct 20 |
nicklas |
60 |
} |
6029 |
28 Oct 20 |
nicklas |
61 |
|
6029 |
28 Oct 20 |
nicklas |
62 |
/** |
6029 |
28 Oct 20 |
nicklas |
Same as get(value). |
6029 |
28 Oct 20 |
nicklas |
64 |
*/ |
6029 |
28 Oct 20 |
nicklas |
65 |
@Override |
6029 |
28 Oct 20 |
nicklas |
66 |
public String convert(String value) |
6029 |
28 Oct 20 |
nicklas |
67 |
{ |
6029 |
28 Oct 20 |
nicklas |
68 |
return get(value); |
6029 |
28 Oct 20 |
nicklas |
69 |
} |
6029 |
28 Oct 20 |
nicklas |
70 |
|
6029 |
28 Oct 20 |
nicklas |
71 |
} |