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

nyla.solutions.global.patterns.cache.CacheFarm Maven / Gradle / Ivy

Go to download

Nyla Solutions Global Java API provides support for basic application utilities (application configuration, data encryption, debugger and text processing).

The newest version!
package nyla.solutions.global.patterns.cache;

import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;





import nyla.solutions.global.exception.NoDataFoundException;
import nyla.solutions.global.exception.NotImplementedException;
import nyla.solutions.global.exception.RequiredException;


public class CacheFarm implements Cache, Cloneable
{	
	/**
	 * 
	 * @see java.util.Hashtable#clear()
	 */
	public void clear()
	{
		map.clear();
	}

	/**
	 * @return
	 * @see java.util.Hashtable#clone()
	 */
	public Object clone()
	{
		/*CacheFarm copy = new CacheFarm();
		copy.map = new WeakHashMap();
		copy.map.putAll(this.map);
		
		return copy;*/
		
		try
		{
			return super.clone();
		}
		catch (CloneNotSupportedException e)
		{
			throw new RuntimeException(e.getMessage(),e);
		}
	}

	/**
	 * @param value
	 * @return
	 * @see java.util.Hashtable#contains(java.lang.Object)
	 */
	public boolean contains(Object value)
	{
		return map.containsValue(value);
	}

	/**
	 * @param key
	 * @return
	 * @see java.util.Hashtable#containsKey(java.lang.Object)
	 */
	public boolean containsKey(Object key)
	{
		return map.containsKey(key);
	}

	/**
	 * @param value
	 * @return
	 * @see java.util.Hashtable#containsValue(java.lang.Object)
	 */
	public boolean containsValue(Object value)
	{
		return map.containsValue(value);
	}

	/**
	 * @return
	 * @see java.util.Hashtable#elements()
	 */
	public Enumeration elements()
	{
		throw new NotImplementedException();
	}

	/**
	 * @return
	 * @see java.util.Hashtable#entrySet()
	 */
	public Set> entrySet()
	{
		return map.entrySet();
	}// --------------------------------------------------------

	/**
	 * @param o
	 * @return
	 * @see java.util.Hashtable#equals(java.lang.Object)
	 */
	public boolean equals(Object o)
	{
		return map.equals(o);
	}

	/**
	 * @param key
	 * @return
	 * @see java.util.Hashtable#get(java.lang.Object)
	 */
	public V get(Object key)
	{
		return map.get(key);
	}

	/**
	 * @return
	 * @see java.util.Hashtable#hashCode()
	 */
	public int hashCode()
	{
		return map.hashCode();
	}

	/**
	 * @return
	 * @see java.util.Hashtable#isEmpty()
	 */
	public boolean isEmpty()
	{
		return map.isEmpty();
	}

	/**
	 * @return
	 * @see java.util.Hashtable#keys()
	 */
	public Enumeration keys()
	{
		throw new NotImplementedException();
	}

	/**
	 * @return
	 * @see java.util.Hashtable#keySet()
	 */
	public Set keySet()
	{
		return map.keySet();
	}

	/**
	 * @param arg0
	 * @param arg1
	 * @return
	 * @see java.util.Hashtable#put(java.lang.Object, java.lang.Object)
	 */
	public V put(K arg0, V arg1)
	{
		return map.put(arg0, arg1);
	}

	/**
	 * @param arg0
	 * @see java.util.Hashtable#putAll(java.util.Map)
	 */
	public void putAll(Map arg0)
	{
		map.putAll(arg0);
	}

	/**
	 * @param key
	 * @return
	 * @see java.util.Hashtable#remove(java.lang.Object)
	 */
	public V remove(Object key)
	{
		return map.remove(key);
	}

	/**
	 * @return
	 * @see java.util.Hashtable#size()
	 */
	public int size()
	{
		return map.size();
	}

	/**
	 * @return
	 * @see java.util.Hashtable#toString()
	 */
	public String toString()
	{
		return map.toString();
	}

	/**
	 * @return
	 * @see java.util.Hashtable#values()
	 */
	public Collection values()
	{
		return map.values();
	}//---------------------------------------------
	/**
	 * 
	 * @return singleton instance cache
	 */
	public static Cache getCache()
	{
		if(cache == null)
			cache = new CacheFarm();
		
		return cache;
		
	}//---------------------------------------------
	
	/**
	 * @see nyla.solutions.global.patterns.cache.Cache#getObject(java.lang.Class)
	 */
	public Object getObject(Class objClass)
	throws NoDataFoundException
	{
		String name = objClass.getName();
		Object obj = this.get(objClass.getName());
		
		if(obj == null)
			throw new NoDataFoundException("name="+name+" no found in keys="+this.map.keySet());
		
		
		return obj;
	}//---------------------------------------------

	/**
	 * @see nyla.solutions.global.patterns.cache.Cache#setObject(java.lang.Class, java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	public void setObject(Class objClass, V obj)
	{
		if(objClass == null)
			throw new RequiredException("objClass in CacheFarm");
		
		if(obj == null)
			throw new RequiredException("obj in CacheFarm");
		
		this.map.put((K)objClass.getName(), obj);
		
	}//---------------------------------------------
	/**
	 * Remove object from cache
	 */
	public void removeObject(Class objClass)
	{
		this.map.remove(objClass.getName());
	}//---------------------------------------------
	private static Cache cache = null;
	private WeakHashMap map = new WeakHashMap();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy