package net.catpad.infobus.test.dinner;


public class Table {

  // Fork is "true" when it is on the table;
  // Fork is "false" when it is taken by a philosopher
  
  boolean[] forks = new boolean[InfoBusTestDinner.PHILOSOPHERS_NUMBER];

  public Table() {
    
    // Put all forks on the table
    for (int i = 0; i < InfoBusTestDinner.PHILOSOPHERS_NUMBER; i++) {
      forks[itrue;
    }
  }
    
  /**
   * This function will mark the fork as available
   @param number
   */
  public synchronized void returnFork(int number) {
    forks[numbertrue;
  }

  /**
   * This function will check whether both forks are available
   * and, if yes, will mark them as not available and return true;
   * It will return false if at least one of the forks is occupied
   @param leftFork
   @param rightFork
   @return
   */
  public synchronized boolean takeForks(int leftFork, int rightFork) {
        
    if (forks[leftFork&& forks[rightFork]) {    
      forks[leftForkfalse;
      forks[rightForkfalse;
      return true;
    }    
    return false;    
  }
  
}