// (c) MIT 2003.  All rights reserved.

// $Id: ModuleManager.java,v 1.12 2003/03/06 00:40:39 moltmans Exp $
// Time-stamp: <2000-03-13 21:33:41 moltmans>
package rec.sys;


////////////////////////////////////////////////////////////////////////////////
//
// FILE:	ModuleManager.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	September, 1998
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:	Drawing recognition
//
////////////////////////////////////////////////////////////////////////////////


import java.util.*;
import rec.util.*;

public class ModuleManager
{
  int m_NumNonSystem;
  Vector m_Modules;

  public ModuleManager()
  {
    m_NumNonSystem = 0;
    m_Modules = new Vector();
  }

  /////////////////////////////////////////////////////////////////////////////

  public boolean isEmpty()
  {
    return( m_NumNonSystem == 0 );
  }

  public void add( Module m )
  {
    Assert.check( ! m_Modules.contains( m ),
		  "Module already added." );
    
    if( m instanceof SystemModule )
      Assert.check( m_NumNonSystem == 0,
		    "cannot add system modules over modules" );
    else
      ++m_NumNonSystem;

    // insert element at the top
    m_Modules.insertElementAt( m, 0 );
  }

  public Module remove()
  {
    Assert.error( "This shouldn't be used anymore... ModuleManager is no longer a stack" );
    Assert.check( !isEmpty(), "module manager is empty" );

    Module ret = (Module)m_Modules.elementAt( 0 );

    m_Modules.removeElementAt( 0 );
    
    m_NumNonSystem--;
	 
    return( ret );
  }

  // This will be a major method someday.
  //   currently used by some special recognizers.
  public Module remove( Module m )
  {
    Assert.check( !isEmpty(), "module manager is empty" );

    boolean ret = m_Modules.removeElement( m );

    if ( ret ) {
      m_NumNonSystem--;
      return m;
    }
	 
    return null;
  }

  public Module remove( Id module_id )
  {
    Iterator it = m_Modules.iterator();
    while ( it.hasNext() ) {
      Module mod = (Module)it.next();
      if ( mod.getId().equals( module_id ) ) {
	return ( remove( mod ) );
      }
    }
    return null;
  }

  public Iterator getModules()
  {
    return( m_Modules.iterator() );
  }

  public Module top()
  {
    Assert.error( "This shouldn't be used anymore... ModuleManager is no longer a stack" );
    return (Module) m_Modules.elementAt( 0 );
  }

  public int size()
  {
    return m_NumNonSystem;
  }

  /////////////////////////////////////////////////////////////////////////////

}


