Class Order

Object
  extended byOrder

public class Order
extends Object

CS 537, Fall 2004, Project 3. A partially-filled order for metals. It keeps track of the amounts requested and granted and serves as a waiting place for the ordering thread to wait for the order to be completely filled.


Field Summary
 int seq
          The sequence number of this request in the order received.
 
Constructor Summary
Order(int id, int[] request)
          Creates a new Order.
 
Method Summary
 void cancel(int[] revoked)
          Rejects this Order.
 void complete()
          Signals completion.
 int give(int[] supply, int limit)
          Gives some resources to this Order from supply.
 boolean lessOrEqual(int[] amt)
          Check whether the remainder of this request is less than or equal to amt (in all metals).
 int remaining()
          Returns the remaining amount requested.
 boolean satisfied()
          Checks whether this order is satified.
 int size()
          Returns the total amount requested.
 String toString()
          Turns this Order into a printable string.
 boolean waitFor()
          Waits for this order to be fulfilled.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

seq

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

Constructor Detail

Order

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

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

lessOrEqual

public boolean lessOrEqual(int[] amt)
Check whether the remainder of this request is less than or equal to amt (in all metals).

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

give

public int give(int[] supply,
                int limit)
Gives some resources to this Order from supply. The amount of each resource 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' units of any resource will be given.

Parameters:
supply - the source of metals.
limit - if greater than zero, do not give more than this amount of any resource.
Returns:
the total number of units of resources given.

satisfied

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

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

size

public int size()
Returns the total amount requested.

Returns:
the sum of requests for all resources.

remaining

public int remaining()
Returns the remaining amount requested.

Returns:
the sum of request - alloc for all resources.

complete

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


cancel

public void cancel(int[] revoked)
Rejects this Order. All resources previously granted to this Order are returned to revoked, and the order is completed with a failure indication.

Parameters:
revoked - the place to put the revoked resources.

waitFor

public boolean waitFor()
Waits for this order to be fulfilled.

Returns:
false if the order was rejected because of shutdown.

toString

public String toString()
Turns this Order into a printable string. Syntax is seq:consumer[alloc1/request1,...,allocn/requestn] where seq is the sequence number of this Order, consumer is the id of the requesting Consumer, alloci is the amount of the ith metal already allocated to this order, and requesti is the initial request amount of metal i.