View Javadoc

1   /*
2    *  Gettext Commons
3    *
4    *  Copyright (C) 2005  Felix Berger
5    *  Copyright (C) 2005  Steffen Pingel
6    *
7    *  This library is free software; you can redistribute it and/or
8    *  modify it under the terms of the GNU Lesser General Public
9    *  License as published by the Free Software Foundation; either
10   *  version 2.1 of the License, or (at your option) any later version.
11   *
12   *  This library is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   *  Lesser General Public License for more details.
16   *
17   *  You should have received a copy of the GNU Lesser General Public
18   *  License along with this library; if not, write to the Free Software
19   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  }