/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.util.cache;

import org.apache.commons.collections.*;

/**
 * A chache which stores objects by key value, and is capable of creating them given an instance of
 * <CODE>ObjectFactory</CODE> to do the creating.  The cache uses <CODE>SoftReferences</CODE> which can
 * automatically be removed by the garbage collector if necessary.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 * @see ObjectFactory
 */
public class ObjectCache
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private ReferenceMap the_data = new ReferenceMap(ReferenceMap.HARD,ReferenceMap.SOFT); // actual main data store
  private LRUMap mru_cache;                                // most-recently-used map
  private ObjectFactory factory;                           // used to create new objects

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  /**
   * Creates a new <CODE>ObjectCache</CODE>.
   *
   * @param factory The factory object used to create new objects when <CODE>getOrCreate</CODE> is called.
   * @exception java.lang.NullPointerException The object factory passed in is <CODE>null</CODE>.
   */
  public ObjectCache(ObjectFactory factory)
  {
    this(factory,5);

  } // end constructor

  /**
   * Creates a new <CODE>ObjectCache</CODE>.
   *
   * @param factory The factory object used to create new objects when <CODE>getOrCreate</CODE> is called.
   * @param lru_size How many objects to maintain hard references to.
   * @exception java.lang.NullPointerException The object factory passed in is <CODE>null</CODE>.
   */
  public ObjectCache(ObjectFactory factory, int lru_size)
  {
    if (factory==null)
      throw new NullPointerException("object factory cannot be null!");
    this.factory = factory;
    this.mru_cache = new LRUMap(lru_size);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  /**
   * Retrieves an object from the cache.
   *
   * @param key The key value of the object to look up.
   * @return The corresponding object value, or <CODE>null</CODE> if that object isn't in the cache.
   */
  public synchronized Object get(Object key)
  {
    Object rc = mru_cache.get(key);
    if (rc==null)
    { // retrieve from main map, poke back into cache
      rc = the_data.get(key);
      if (rc!=null)
	mru_cache.put(key,rc);

    } // end if

    return rc;

  } // end get

  /**
   * Retrieves an object from the cache, creating it if it doesn't already exist.
   *
   * @param key The key value of the object to look up or create.
   * @return The corresponding object value.
   * @exception com.silverwrist.util.cache.ObjectFactoryException If an exception occurred while creating
   *            a new object.
   * @see ObjectFactory#newObject(java.lang.Object)
   */
  public synchronized Object getOrCreate(Object key) throws ObjectFactoryException
  {
    Object rc = this.get(key);
    if (rc==null)
    { // create a new object
      rc = factory.newObject(key);
      if (rc!=null)
      { // stash in both main map and cache
	the_data.put(key,rc);
	mru_cache.put(key,rc);

      } // end if

    } // end if

    return rc;

  } // end getOrCreate

  /**
   * Registers a newly-created object with the cache.
   *
   * @param key The key value to register this object with.
   * @param data The object to be registered.
   * @exception com.silverwrist.util.cache.ObjectCacheException If an object with that key value already
   *            exists in the cache.
   */
  public synchronized void register(Object key, Object data)
  {
    if (the_data.get(key)!=null)
      throw new ObjectCacheException("object already in cache",key);
    the_data.put(key,data);
    mru_cache.put(key,data);

  } // end register

  /**
   * Detaches an object from the cache.
   *
   * @param key Key value of the object to be detached.
   */
  public synchronized void detach(Object key)
  {
    mru_cache.remove(key);
    the_data.remove(key);

  } // end detach

  public synchronized void shutdown()
  {
    mru_cache.clear();
    the_data.clear();

  } // end shutdown

} // end class ObjectCache
