1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.xnap.commons.i18n;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Map;
29
30 /**
31 * Stores a list of {@link I18n} objects by a String key.
32 *
33 * @author Steffen Pingel
34 */
35 class I18nCache {
36
37 private final Map i18nByPackage = new HashMap();
38
39 I18nCache()
40 {
41 }
42
43 public void clear()
44 {
45 i18nByPackage.clear();
46 }
47
48 public I18n get(final String packageName, final Locale locale)
49 {
50 if (locale == null) {
51 throw new IllegalArgumentException();
52 }
53
54 List list = (List)i18nByPackage.get(packageName);
55 if (list != null) {
56 for (Iterator it = list.iterator(); it.hasNext();) {
57 I18n i18n = (I18n)it.next();
58 if (locale.equals(i18n.getLocale())) {
59 return i18n;
60 }
61 }
62 }
63 return null;
64 }
65
66 public void put(String packageName, I18n i18n)
67 {
68 List list = (List)i18nByPackage.get(packageName);
69 if (list == null) {
70 list = new ArrayList();
71 i18nByPackage.put(packageName, list);
72 }
73 list.add(i18n);
74 }
75
76 public void visit(final Visitor visitor)
77 {
78 for (Iterator it = i18nByPackage.values().iterator(); it.hasNext();) {
79 List list = (List)it.next();
80 for (Iterator it2 = list.iterator(); it2.hasNext();) {
81 I18n i18n = (I18n)it2.next();
82 visitor.visit(i18n);
83 }
84 }
85 }
86
87 public static interface Visitor {
88
89 void visit(I18n i18n);
90
91 }
92
93 }