Class TraderImpl

Object
  extended by TraderImpl
All Implemented Interfaces:
Trader

public class TraderImpl
extends Object
implements Trader

Implementation of a trader. CS 537, Spring 2007, Project 2.


Constructor Summary
TraderImpl(Grain specialty)
          Creates a new Trader.
 
Method Summary
 void deliver(int bushels)
          Accepts more grain from the supplier.
 void get(Order order)
          A purchase request from a Brewer.
 Order getAmountOnHand()
          Reports on the total amount of resources held by this Trader.
 void swap(Grain what, int bushels)
          Responds to a swap request from another trader.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TraderImpl

public TraderImpl(Grain specialty)
Creates a new Trader.

Parameters:
specialty - the grain type for which this Trader is the supplier.
Method Detail

getAmountOnHand

public Order getAmountOnHand()
Reports on the total amount of resources held by this Trader. Also audit debits and credits.

Specified by:
getAmountOnHand in interface Trader
Returns:
an indication of the amount of each grain type held by this Trader.

get

public void get(Order order)
         throws InterruptedException
A purchase request from a Brewer. The caller is blocked until the request can be completely filled. Note that this method is not synchronized. We do not wish to hold the monitor lock while calling other synchronized methods (namely Trader.swap). However, it uses synchronized methods to access all shared fields, so it is thread-safe.

Specified by:
get in interface Trader
Parameters:
order - the amount needed of each type of grain.
Throws:
InterruptedException - if the current thread is interrupted while waiting for the order to be filled.

swap

public void swap(Grain what,
                 int bushels)
          throws InterruptedException
Responds to a swap request from another trader. Delays the caller until this Trader can complete the swap.

Specified by:
swap in interface Trader
Parameters:
what - the type of grain the other trader wants to swap.
bushels - the number of bushels to swap.
Throws:
InterruptedException - if the current thread is interrupted while waiting for the swap to succeed.

deliver

public void deliver(int bushels)
Accepts more grain from the supplier.

Specified by:
deliver in interface Trader
Parameters:
bushels - the number of bushels of this Trader's specialty grain type being delivered.