Class Request

Object
  extended by Request

public class Request
extends Object

A request from a Brewer to the warehouse.
CS 537, Spring 2007, Project 3.
A Request keeps track of the amounts requested and allocated thus far, and serves as a waiting place for the ordering thread to wait for completion.


Field Summary
private  Order alloc
          The amount allocated thus far.
private  boolean completed
          Flag to indicate this Request is satisfied.
private  int id
          The requesting customer's id (for debugging output).
private static int nextSeq
          Source of sequence numbers.
private  Order requested
          The original amount requested.
 int seq
          The sequence number of this request in the order received.
private static int VERSION
          Source version number.
 
Constructor Summary
Request(int id, Order request)
          Creates a new Request.
 
Method Summary
 void await()
          Await fulfillment of this order.
 void complete()
          Signal completion.
 Order getAlloc()
          Gets the current allocation to this request.
 int give(Order supply, int limit)
          Allocate some grain to this Request from supply.
 boolean lessOrEqual(Order supply)
          Check whether the unfilled portion of this request is less than or equal to supply (in all grains).
 int remaining()
          Return the remaining amount requested.
 boolean satisfied()
          Check whether this order is satified.
 int size()
          Return the total amount requested.
 String toString()
          Turn a Request into a printable string.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VERSION

private static final int VERSION
Source version number.

See Also:
Constant Field Values

nextSeq

private static int nextSeq
Source of sequence numbers.


requested

private Order requested
The original amount requested.


id

private int id
The requesting customer's id (for debugging output).


seq

public final int seq
The sequence number of this request in the order received.


alloc

private Order alloc
The amount allocated thus far.


completed

private boolean completed
Flag to indicate this Request is satisfied.

Constructor Detail

Request

public Request(int id,
               Order request)
Creates a new Request. Note: This method is not thread-safe, so it should be only called from a synchronized method that prevents two concurrent invocations of "new Request(...)".

Parameters:
id - the requesting customer's id (for debugging output).
request - the amount of each commodity needed.
Method Detail

lessOrEqual

public boolean lessOrEqual(Order supply)
Check whether the unfilled portion of this request is less than or equal to supply (in all grains).

Parameters:
supply - the amount to compare to.
Returns:
true if the request can be completely satisfied by supply.

give

public int give(Order supply,
                int limit)
Allocate some grain to this Request from supply. The amount of each grain given is the minimum of the amount in supply and the amount needed to complete the order. However, if limit is postive, no more than 'limit' bushels of any grain will be given.

Parameters:
supply - the source of grain.
limit - if greater than zero, do not give more than this many bushels of any one grain.
Returns:
the total number of bushels of grain given.

satisfied

public boolean satisfied()
Check whether this order is satified.

Returns:
true if the amount allocated matches the amount requested, for all resources.

size

public int size()
Return the total amount requested.

Returns:
the sum of the numbers of bushels of all grains.

remaining

public int remaining()
Return the remaining amount requested.

Returns:
the sum of request - alloc over all grains.

complete

public void complete()
Signal completion. The customer who placed this order is allowed to return.


await

public void await()
           throws InterruptedException
Await fulfillment of this order.

Throws:
InterruptedException - if the current thread is interrupted while waiting for the request to be filled.

getAlloc

public Order getAlloc()
Gets the current allocation to this request.

Returns:
the current amount allocated

toString

public String toString()
Turn a Request into a printable string.

Overrides:
toString in class Object
Returns:
a printable version of this request.