All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.squirrel_sql.fw.util.StringManagerFactory Maven / Gradle / Ivy

package net.sourceforge.squirrel_sql.fw.util;
/*
 * Copyright (C) 2003 Colin Bell
 * [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.util.HashMap;
import java.util.Map;
/**
 * This class manages instances of StringManager objects. It keeps a
 * cache of them, one for each package.
 *
 * @author Colin Bell
 */
public class StringManagerFactory
{
	/**
	 * Collection of StringManager objects keyed by the Java package
	 * name.
	 */
	private static final Map s_mgrs = 
	    new HashMap();

	/**
	 * Retrieve an instance of StringManager for the passed class.
	 * Currently an instance of Stringmanager is stored for each
	 * package.
	 *
	 * @param	clazz	Class to retrieve StringManager for.
	 *
	 * @return	instance of StringManager.
	 *
	 * @throws	IllegalArgumentException
	 * 			Thrown if null clazz passed.
	 */
	public static synchronized StringManager getStringManager(Class clazz)
	{
		if (clazz == null)
		{
			throw new IllegalArgumentException("clazz == null");
		}

		final String key = getKey(clazz);
		StringManager mgr = s_mgrs.get(key);
		if (mgr == null)
		{
			mgr = new StringManager(key, clazz.getClassLoader());
			s_mgrs.put(key, mgr);
		}
		return mgr;
	}

	/**
	 * Retrieve the key to use to identify the StringManager instance
	 * for the passed class. Currently one instance is stored for each package.
	 *
	 * @param	clazz	Class to get key for.
	 *
	 * @return	the key to use.
	 *
	 * @throws	IllegalArgumentException
	 * 			Thrown if null clazz passed.
	 */
	private static String getKey(Class clazz)
	{
		if (clazz == null)
		{
			throw new IllegalArgumentException("clazz == null");
		}

		final String clazzName = clazz.getName();
		return clazzName.substring(0, clazzName.lastIndexOf('.'));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy