|
1 | | -package net.lecousin.framework.locale; |
2 | | - |
3 | | -import java.util.Iterator; |
4 | | -import java.util.LinkedList; |
5 | | -import java.util.List; |
6 | | - |
7 | | -import net.lecousin.framework.concurrent.synch.AsyncWork; |
8 | | -import net.lecousin.framework.concurrent.synch.JoinPoint; |
9 | | -import net.lecousin.framework.exception.NoException; |
10 | | - |
11 | | -/** |
12 | | - * List of objects to concatenate to form a string, in which any object implementing ILocalizableString will be localized. |
13 | | - */ |
14 | | -public class LocalizableStringBuffer implements ILocalizableString { |
15 | | - |
16 | | - /** Constructor. */ |
17 | | - public LocalizableStringBuffer(Object... list) { |
18 | | - for (int i = 0; i < list.length; ++i) |
19 | | - this.list.add(list[i]); |
20 | | - } |
21 | | - |
22 | | - private LinkedList<Object> list = new LinkedList<>(); |
23 | | - |
24 | | - /** Append the given object. */ |
25 | | - public void add(Object string) { |
26 | | - list.add(string); |
27 | | - } |
28 | | - |
29 | | - @Override |
30 | | - public AsyncWork<String, NoException> localize(String[] languageTag) { |
31 | | - JoinPoint<NoException> jp = new JoinPoint<>(); |
32 | | - List<AsyncWork<String, NoException>> list = new LinkedList<>(); |
33 | | - for (Object o : list) |
34 | | - if (o instanceof ILocalizableString) { |
35 | | - AsyncWork<String, NoException> l = ((ILocalizableString)o).localize(languageTag); |
36 | | - jp.addToJoin(l); |
37 | | - list.add(l); |
38 | | - } |
39 | | - AsyncWork<String, NoException> result = new AsyncWork<>(); |
40 | | - jp.start(); |
41 | | - jp.listenInline(new Runnable() { |
42 | | - @Override |
43 | | - public void run() { |
44 | | - Iterator<AsyncWork<String, NoException>> it = list.iterator(); |
45 | | - StringBuffer s = new StringBuffer(); |
46 | | - for (Object o : list) { |
47 | | - if (o instanceof String) |
48 | | - s.append((String)o); |
49 | | - else if (o instanceof ILocalizableString) |
50 | | - s.append(it.next().getResult()); |
51 | | - else |
52 | | - s.append(o); |
53 | | - } |
54 | | - result.unblockSuccess(s.toString()); |
55 | | - } |
56 | | - }); |
57 | | - return result; |
58 | | - } |
59 | | - |
60 | | - @Override |
61 | | - public String localizeSync(String[] languageTag) { |
62 | | - StringBuffer s = new StringBuffer(); |
63 | | - for (Object o : list) { |
64 | | - if (o instanceof String) |
65 | | - s.append((String)o); |
66 | | - else if (o instanceof ILocalizableString) |
67 | | - s.append(((ILocalizableString)o).localizeSync(languageTag)); |
68 | | - else |
69 | | - s.append(o); |
70 | | - } |
71 | | - return s.toString(); |
72 | | - } |
73 | | -} |
| 1 | +package net.lecousin.framework.locale; |
| 2 | + |
| 3 | +import java.util.Iterator; |
| 4 | +import java.util.LinkedList; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import net.lecousin.framework.concurrent.synch.AsyncWork; |
| 8 | +import net.lecousin.framework.concurrent.synch.JoinPoint; |
| 9 | +import net.lecousin.framework.exception.NoException; |
| 10 | + |
| 11 | +/** |
| 12 | + * List of objects to concatenate to form a string, in which any object implementing ILocalizableString will be localized. |
| 13 | + */ |
| 14 | +public class LocalizableStringBuffer implements ILocalizableString { |
| 15 | + |
| 16 | + /** Constructor. */ |
| 17 | + public LocalizableStringBuffer(Object... list) { |
| 18 | + for (int i = 0; i < list.length; ++i) |
| 19 | + this.list.add(list[i]); |
| 20 | + } |
| 21 | + |
| 22 | + private LinkedList<Object> list = new LinkedList<>(); |
| 23 | + |
| 24 | + /** Append the given object. */ |
| 25 | + public void add(Object string) { |
| 26 | + list.add(string); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public AsyncWork<String, NoException> localize(String[] languageTag) { |
| 31 | + JoinPoint<NoException> jp = new JoinPoint<>(); |
| 32 | + List<AsyncWork<String, NoException>> list = new LinkedList<>(); |
| 33 | + for (Object o : this.list) |
| 34 | + if (o instanceof ILocalizableString) { |
| 35 | + AsyncWork<String, NoException> l = ((ILocalizableString)o).localize(languageTag); |
| 36 | + jp.addToJoin(l); |
| 37 | + list.add(l); |
| 38 | + } |
| 39 | + AsyncWork<String, NoException> result = new AsyncWork<>(); |
| 40 | + jp.start(); |
| 41 | + jp.listenInline(new Runnable() { |
| 42 | + @Override |
| 43 | + public void run() { |
| 44 | + Iterator<AsyncWork<String, NoException>> it = list.iterator(); |
| 45 | + StringBuffer s = new StringBuffer(); |
| 46 | + for (Object o : LocalizableStringBuffer.this.list) { |
| 47 | + if (o instanceof String) |
| 48 | + s.append((String)o); |
| 49 | + else if (o instanceof ILocalizableString) |
| 50 | + s.append(it.next().getResult()); |
| 51 | + else |
| 52 | + s.append(o); |
| 53 | + } |
| 54 | + result.unblockSuccess(s.toString()); |
| 55 | + } |
| 56 | + }); |
| 57 | + return result; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String localizeSync(String[] languageTag) { |
| 62 | + StringBuffer s = new StringBuffer(); |
| 63 | + for (Object o : list) { |
| 64 | + if (o instanceof String) |
| 65 | + s.append((String)o); |
| 66 | + else if (o instanceof ILocalizableString) |
| 67 | + s.append(((ILocalizableString)o).localizeSync(languageTag)); |
| 68 | + else |
| 69 | + s.append(o); |
| 70 | + } |
| 71 | + return s.toString(); |
| 72 | + } |
| 73 | +} |
0 commit comments