/*
 * 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) 2002-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.velocity;

import java.util.*;
import org.apache.velocity.context.Context;

/**
 * An Adapter which allows a {@link com.silverwrist.dynamo.velocity.VelocityParamSupplier VelocityParamSupplier}
 * to supply rendering parameters to Velocity as a standard <CODE>Context</CODE> object.
 */
class RenderableContext implements Context
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private VelocityParamSupplier m_vr;    // the parameter supplier we're adapting
  private Object[] m_arr = null;         // cache of the parameter names array

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

  /**
   * Creates a new <CODE>RenderableContext</CODE>.
   *
   * @param vr The {@link com.silverwrist.dynamo.velocity.VelocityParamSupplier VelocityParamSupplier} to be
   *           wrapped by this object.
   */
  RenderableContext(VelocityParamSupplier vr)
  {
    m_vr = vr;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Context
   *--------------------------------------------------------------------------------
   */

  /**
   * Adds a name/value pair to the context.  For this class, this is a no-op.
   *
   * @param key The name to key the provided value with.
   * @param value The corresponding value.
   * @return The previous value set for that key, or <CODE>null/CODE>.
   */
  public Object put(String key, Object value)
  {
    return null; // dummy method

  } // end put

  /**
   * Gets the value corresponding to the provided key from the context.
   *
   * @param key The name of the desired value.
   * @return The value corresponding to the provided key.
   */
  public Object get(String key)
  {
    return m_vr.getParameter(key);

  } // end get

  /**
   * Indicates whether the specified key is in the context.
   *
   * @param key The key to look for.
   * @return <CODE>true</CODE> if the key is in the context, <CODE>false</CODE> if not.
   */
  public boolean containsKey(Object key)
  {
    return (m_vr.getParameter(key.toString())!=null);

  } // end containsKey

  /**
   * Get all the keys for the values in the context.
   *
   * @return An array containing all the keys from this context.
   */
  public Object[] getKeys()
  {
    if (m_arr==null)
      m_arr = m_vr.getParameterNames().toArray();
    return m_arr;

  } // end getKeys
  
  /**
   * Removes the value associated with the specified key from the context.  For this class, this is a no-op.
   *
   * @param key The name of the value to remove.
   * @return The value that the key was mapped to, or <CODE>null</CODE> if unmapped.
   */
  public Object remove(Object key)
  {
    return null;  // dummy method

  } // end remove

} // end class RenderableContext
