Class Order

java.lang.Object
  |
  +--Order

public class Order
extends Object

CS 537, Fall 2002, Project 3. An order for commodities. It keeps track of the amounts requested and serves as a waiting place for the ordering thread to wait for them.

Copyright © 2002 by Marvin Solomon. All rights reserved.


Field Summary
private  int[] alloc
          The amount allocated thus far.
private  boolean done
          Indication that this request has been released by calling release().
private  int id
          The requesting customer's id (for debugging output).
private static int nextSeq
          Source of sequence numbers.
private  int[] request
          The original amount requested.
private  boolean result
          Return code to be returned by await.
 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
 boolean await()
          Await fulfillment of this order.
 void cancel(int[] dest)
          Reject this Order.
 void complete()
          Signal completion.
 int give(int[] supply, int limit)
          Give 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 commodities).
 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 an Order into a printable string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

nextSeq

private static int nextSeq
Source of sequence numbers.


request

private int[] request
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 int[] alloc
The amount allocated thus far.


done

private boolean done
Indication that this request has been released by calling release().


result

private boolean result
Return code to be returned by await. The value false indicates the request was refused (because the system is shutting down).

Constructor Detail

Order

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

Parameters:
id - the requesting customer's id (for debugging output).
request - the amount of each commodity 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 commodities).

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)
Give 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 commodities.
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()
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 requests for all resources.

remaining

public int remaining()
Return the remaining amount requested.

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

complete

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


cancel

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

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

await

public boolean await()
Await fulfillment of this order.

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

toString

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

Overrides:
toString in class Object