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

// $Id: WidgetSet.java,v 1.5 2003/03/06 00:40:39 moltmans Exp $
// Time-stamp: <2000-03-11 18:59:37 moltmans>
package rec.sys;



////////////////////////////////////////////////////////////////////////////////
/* widget sets are family members... but they ONLY CONTAIN WIDGETS, not
 * other widget sets!!!
 */

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

public class WidgetSet extends HashSet implements FamilyMember
{
  public WidgetSet( Collection c )
  {
    super( c );
  }

  public WidgetSet()
  {
    super();
  }
  
  public void die()
  {
    Iterator it = iterator();
    while ( it.hasNext() ) {
      FamilyMember f = (FamilyMember)it.next();
      f.die();
    }
  }
  
  public Collection getAllChildren()
  {
    Collection c = new HashSet();
    Iterator it = iterator();
    while ( it.hasNext() ) {
      FamilyMember f = (FamilyMember)it.next();
      c.add( f );
      c.addAll( f.getAllChildren() );
    }
    
    return c;
  }

  /**
   * Does not return the me--since we are a non-widget family member.
   * be warned...
   */
  public Collection getAllChildrenAndMe()
  {
    return getAllChildren();
  }

  public Iterator getChildrenIterator()
  {  
    return iterator();
  }

  public Collection getChildren() 
  {
    return this;
  }

  public boolean parentOf( Widget w )
  {
    Iterator it = iterator();
    while( it.hasNext() )
      {
	FamilyMember f = (FamilyMember)it.next();
	
	if ( f.parentOf( w ) )
	  return true;
      }

    return false;
  }

  public int getStrength()
  {
    if ( size() == 0 ) {
      return 0;
    }
    
    Console.writeln( "getting strength of widget set with " + size() + " widgets" );

    Iterator it = iterator();
    int total_strength = 0;

    while ( it.hasNext() ) {
      FamilyMember f = (FamilyMember)it.next();
      total_strength += f.getStrength();
    }
    
    return ( total_strength / size() );
      
  }

  public void removeParent( Widget p )
  {
    Iterator it = getChildren().iterator();
    while ( it.hasNext() ) {
      ((Widget)it.next()).removeParent( p );
    }
  }

  public void addParent( Widget p )
  {
    Iterator it = getChildren().iterator();
    while ( it.hasNext() ) {
      ((Widget)it.next()).addParent( p );
    }
  }

  public String toString()
  {
    return "Collection";
  }

  public void printAll()
  {
    Iterator it = iterator();
    while ( it.hasNext() ) {
      Console.writeln( (FamilyMember)it.next() + "" );
    }
			 
  }

}
