/*
 * 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@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.servlet;

import java.util.*;
import com.silverwrist.venice.ui.*;

class HttpVeniceUISessionList implements VeniceUISessionList
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private int m_max = 0;                  // maximum session count
  private HashSet m_set = new HashSet();  // hash set

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

  private HttpVeniceUISessionList(RequestInput ri)
  {
    ri.setAppAttribute(APP_ATTR_NAME,this);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface VeniceUISessionList
   *--------------------------------------------------------------------------------
   */

  public synchronized int getSessionCount()
  {
    return m_set.size();

  } // end getSessionCount

  public synchronized int getMaxSessionCount()
  {
    return m_max;

  } // end getMaxSessionCount

  public synchronized Iterator listSessions()
  {
    if (m_set.isEmpty())
      return Collections.EMPTY_LIST.iterator();
    ArrayList tmp = new ArrayList(m_set);
    return Collections.unmodifiableList(tmp).iterator();

  } // end listSessions

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

  synchronized void joinSession(VeniceUISession session)
  {
    m_set.add(session);
    if (m_set.size()>m_max)
      m_max = m_set.size();

  } // end joinSession

  synchronized void partSession(VeniceUISession session)
  {
    m_set.remove(session);

  } // end partSession

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

  static synchronized HttpVeniceUISessionList get(RequestInput ri)
  {
    HttpVeniceUISessionList rc = (HttpVeniceUISessionList)(ri.getAppAttribute(APP_ATTR_NAME));
    if (rc==null)
      rc = new HttpVeniceUISessionList(ri);
    return rc;

  } // end get

} // end class HttpVeniceUISessionList
