01: package net.sourceforge.squirrel_sql.fw.util;
02:
03: /*
04: * Copyright (C) 2003 Colin Bell
05: * colbell@users.sourceforge.net
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * 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: import java.util.HashMap;
22: import java.util.Map;
23:
24: /**
25: * This class manages instances of <TT>StringManager</TT> objects. It keeps a
26: * cache of them, one for each package.
27: *
28: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
29: */
30: public class StringManagerFactory {
31: /**
32: * Collection of <TT>StringManager</TT> objects keyed by the Java package
33: * name.
34: */
35: private static final Map<String, StringManager> s_mgrs = new HashMap<String, StringManager>();
36:
37: /**
38: * Retrieve an instance of <TT>StringManager</TT> for the passed class.
39: * Currently an instance of <TT>Stringmanager</TT> is stored for each
40: * package.
41: *
42: * @param clazz <TT>Class</TT> to retrieve <TT>StringManager</TT> for.
43: *
44: * @return instance of <TT>StringManager</TT>.
45: *
46: * @throws IllegalArgumentException
47: * Thrown if <TT>null</TT> <TT>clazz</TT> passed.
48: */
49: public static synchronized StringManager getStringManager(
50: Class<?> clazz) {
51: if (clazz == null) {
52: throw new IllegalArgumentException("clazz == null");
53: }
54:
55: final String key = getKey(clazz);
56: StringManager mgr = s_mgrs.get(key);
57: if (mgr == null) {
58: mgr = new StringManager(key, clazz.getClassLoader());
59: s_mgrs.put(key, mgr);
60: }
61: return mgr;
62: }
63:
64: /**
65: * Retrieve the key to use to identify the <TT>StringManager</TT> instance
66: * for the passed class. Currently one instance is stored for each package.
67: *
68: * @param clazz <TT>Class</TT> to get key for.
69: *
70: * @return the key to use.
71: *
72: * @throws IllegalArgumentException
73: * Thrown if <TT>null</TT> <TT>clazz</TT> passed.
74: */
75: private static String getKey(Class<?> clazz) {
76: if (clazz == null) {
77: throw new IllegalArgumentException("clazz == null");
78: }
79:
80: final String clazzName = clazz.getName();
81: return clazzName.substring(0, clazzName.lastIndexOf('.'));
82: }
83: }
|