Start Search Contents Index Links About
Location.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Implementes a physical Location in the Simulation.
 * This includes the name of the Location, and the Hosts
 * which are present.
 
 * Furthermore, this class includes the algorithms to spread
 * Viruses amongst the population at this Location.
 
 @author Havard Rast Blok
 *
 */
public class Location {

  /**
   * The name of the Location.
   */
  protected String name;
  
  /**
   * The Hosts currently at this Location.
   */
  protected Collection<Host> hosts = new ArrayList<Host>();
  
  /**
   * Constructs a Location with the specified name.
   @param name the name of the new Location.
   */
  public Location(String name) {
    this.name = name;
  }
  
  /**
   * Adds the specified Host to this Location.
   @param host the Host to add.
   */
  public void addHost(Host host) {
    this.hosts.add(host);
  }

  /**
   * Removes the specified Host from this Location.
   @param host the Host to remove.
   */
  public void remove(Host host) {
    this.hosts.remove(host);
  }
  
  /**
   * Adds all the Hosts in the specified Collection to this Location.
   @param hosts the Hosts to add.
   */
  public void addAll(Collection<Host> hosts) {
    this.hosts.addAll(hosts);
  }
  
  /**
   * Makes the viruses spread at this Location.
   *
   */
  public void spread() {
    //for each of the hosts in this Location
    for(Host h : hosts) {
      //find the ones that are infected
      if(h.isInfected()) {
        //get one virus from that host
        Collection<Virus> viruses = h.getViruses();
        Virus v = getRandomVirus(viruses);
        
        //get another random host at this Location
        Host h2 = getRandomHost(hosts);
        
        //try to infect the other host
        v.infect(h2);
      }
    }
  }

  /**
   * Returns a random Virus from the given Collection of Viruses.
   * If the argument is null, or contains no elements,
   * null is returned.
   
   @param viruses Collection to pick from
   @return random virus from the Collection, or null if
   * the argument is null or contains no elements.
   */
  protected Virus getRandomVirus(Collection<Virus> viruses) {
    return (Virus)getRandomObject(viruses);
  }

  /**
   * Returns a random Host from the given Collection of Hosts.
   * If the argument is null, or contains no elements,
   * null is returned.
   
   @param hosts Collection to pick from
   @return random host from the Collection, or null if
   * the argument is null or contains no elements.
   */
  protected Host getRandomHost(Collection<Host> hosts) {
    return (Host)getRandomObject(hosts);
  }

  /**
   * Returns a random element from the given Collection.
   * If the argument is null, or contains no elements,
   * null is returned.
   
   @param c Collection to pick from
   @return random element from the Collection, or null if
   * the argument is null or contains no elements.
   */
  public static Object getRandomObject(Collection c) {
    //if the argument is null, return null
    if(c == null) {
      return null;
    }
    
    //get random index
    double size = c.size();
    int rnd = (int)(Math.random()*size);
    
    //iterate to that index
    Iterator itr = c.iterator();
    for (int i = 0; i < size; i++) {
      Object obj = itr.next();
      //and return the Virus
      if(i == rnd) {
        return obj;
      }
    }
    
    //only reached if the Collection contains no elements
    return null;
  }

  /**
   * Lists the status of the Hosts at this Location.
   * Currently hardcoded for Humans and Birds.
   @return
   */
  public String listStats() {
    int totHumans = 0;
    int totBirds = 0;
    int infHumans = 0;
    int infBirds = 0;
    
    for(Host h:hosts) {
      if(instanceof Human) {
        totHumans++;
        infHumans+=(h.isInfected())?1:0;
      }
      else if(instanceof Bird) {
        totBirds++;
        infBirds+=(h.isInfected())?1:0;
      }      
    }
    
    return "\t"+totHumans+" ("+infHumans+")\t"+
              totBirds+" ("+infBirds+")";
  }
  
  /**
   * Gives the status at this Location.
   */
  public String toString() {
    return name+": "+listStats();
  }

}


site: Håvard Rast Blok
mail:
updated: 27 July 2007