Class Warehouse

Object
  extended by Warehouse
All Implemented Interfaces:
Runnable

public class Warehouse
extends Object
implements Runnable

A centralized supplier of grain.
CS 537, Spring 2007, Project 3.
This class replaces the Traders of Project 2. A Warehouse accepts request from Brewers and serves them as fast as possible. If it has multiple requests, it satisfies them in a variety of orders, depending on the algorithm specified in the constructor.


Field Summary
private  int algorithm
          The algorithm used by this Warehouse.
private  Order delivered
          The total amount delivered to Customers.
private  int fulfilled
          Number of requests fulfilled.
private static int NUM_ALGORITHMS
          The number of algorithms implemented.
private  Order onHand
          The current stock on hand.
private  Order supplied
          The total amount received from the Supplier.
private static int VERSION
          Source version number.
private  List<Request> waiters
          Queue of waiting requests.
 
Constructor Summary
Warehouse(int algorithm)
          Creates a new Warehouse.
 
Method Summary
private  void algorithm1()
          Tries to satisfy and release one or more customers.
private  void algorithm2()
          Tries to satisfy and release one or more customers.
private  void algorithm3()
          Tries to satisfy and release one or more customers.
private  void algorithm4()
          Tries to satisfy and release one or more customers.
 void deliver(Order amount)
          Accepts more grain from the supplier.
private  Request enqueue(int id, Order amt)
          Creates a Request object and places it onto the waiters list.
 void get(int id, Order amt)
          Accepts a request from a Brewer and blocks the Brewer until the request can be satisfied.
 Order getAmountOnHand()
          Reports on the total amount of grain held by this Warehouse.
private  void recordFulfillment(Order amt)
          Record the fact that an order was fulfilled.
 void run()
          Main loop.
 void undo(Order amount)
          Accepts back grain from an interrupted request.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

private static final int VERSION
Source version number.

See Also:
Constant Field Values

algorithm

private int algorithm
The algorithm used by this Warehouse.


NUM_ALGORITHMS

private static final int NUM_ALGORITHMS
The number of algorithms implemented.

See Also:
Constant Field Values

waiters

private List<Request> waiters
Queue of waiting requests.


onHand

private Order onHand
The current stock on hand.


supplied

private Order supplied
The total amount received from the Supplier.


fulfilled

private int fulfilled
Number of requests fulfilled.


delivered

private Order delivered
The total amount delivered to Customers.

Constructor Detail

Warehouse

public Warehouse(int algorithm)
Creates a new Warehouse.

Parameters:
algorithm - the algorithm used to choose among requests.
Method Detail

deliver

public void deliver(Order amount)
Accepts more grain from the supplier.

Parameters:
amount - the amount being supplied.

undo

public void undo(Order amount)
Accepts back grain from an interrupted request.

Parameters:
amount - the amount being returned.

get

public void get(int id,
                Order amt)
         throws InterruptedException
Accepts a request from a Brewer and blocks the Brewer until the request can be satisfied.

Parameters:
id - the requesting Brewer's id (for debugging output).
amt - the request.
Throws:
InterruptedException - if the Brewer thread is interrupted while waiting for the request to be filled.

enqueue

private Request enqueue(int id,
                        Order amt)
Creates a Request object and places it onto the waiters list.

Parameters:
id - the requesting customer's id (for debugging output).
amt - the request.
Returns:
the created Order

run

public void run()
Main loop.

Specified by:
run in interface Runnable

getAmountOnHand

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

Returns:
an indication of the amount of each grain type held by this Warehouse.

algorithm1

private void algorithm1()
Tries to satisfy and release one or more customers. Uses algorithm 1. Should be called whenever conditions change. Only called from synchronized methods.


algorithm2

private void algorithm2()
Tries to satisfy and release one or more customers. Uses algorithm 2. Should be called whenever conditions change. Only called from synchronized methods.


algorithm3

private void algorithm3()
Tries to satisfy and release one or more customers. Uses algorithm 3. Should be called whenever conditions change. Only called from synchronized methods.


algorithm4

private void algorithm4()
Tries to satisfy and release one or more customers. Uses algorithm 4. Should be called whenever conditions change. Only called from synchronized methods.


recordFulfillment

private void recordFulfillment(Order amt)
Record the fact that an order was fulfilled.

Parameters:
amt - the order.