alpiv.trucks
Class Road

java.lang.Object
  |
  +--alpiv.trucks.BlockSupport
        |
        +--alpiv.trucks.Road

public class Road
extends BlockSupport

Objects of this class represent sections of a road map. A road can be (b)locked for exclusive access by a (Truck)-Thread. It is also possible register a truck as traffic on the road, but only one truck at a time.


Field Summary
static int AHEAD
           
static int DIRECTIONS
          Constants for the directions of exits.
static java.lang.String[] DIRNAMES
          Names for the directions.
static int EAST
           
static int GOAL
           
static int LEFT
          Constants for direction changes.
static int NORTH
           
static int NORTHEAST
           
static int NORTHWEST
           
static int NOWHERE
           
static int REGULAR
          Constants for the road types.
static int RIGHT
           
static int SOUTH
           
static int SOUTHEAST
           
static int SOUTHWEST
           
static int START
           
static int WEST
           
 
Constructor Summary
Road(java.lang.String name, alpiv.trucks.RoadObserver observer)
          Convenience constructor with name and observer.
Road(java.lang.String name, alpiv.trucks.RoadObserver observer, int type)
          Convenience constructor with name, observer, and type.
Road(java.lang.String name, alpiv.trucks.RoadObserver observer, int x, int y, int type)
          Full constructor.
 
Method Summary
 Road getExit(int direction)
          Get the road reachable in the given direction.
 java.lang.String getName()
          Get the name of the road object.
 alpiv.trucks.Truck getTraffic()
          Get the current traffic on this road.
 int getType()
          Get the type of the road object.
 int getX()
          Get the X coordinate for map layout.
 int getY()
          Get the Y coordinate for map layout.
static int nextDirection(int current, int turn)
          A helper method to calculate direction changes.
static int oppositeDirection(int current)
          A helper method to calculate the opposite of a direction.
 void setExit(int direction, Road to)
          Link this road to another one in the given direction.
 void setLocation(int x, int y)
          Set this roads location for layout purposes.
 void setTraffic(alpiv.trucks.Truck traffic)
          Place or remove some traffic on the road.
 
Methods inherited from class alpiv.trucks.BlockSupport
block, blocking, unblock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DIRECTIONS

public static final int DIRECTIONS
Constants for the directions of exits.

See Also:
Constant Field Values

NORTH

public static final int NORTH
See Also:
Constant Field Values

NORTHEAST

public static final int NORTHEAST
See Also:
Constant Field Values

EAST

public static final int EAST
See Also:
Constant Field Values

SOUTHEAST

public static final int SOUTHEAST
See Also:
Constant Field Values

SOUTH

public static final int SOUTH
See Also:
Constant Field Values

SOUTHWEST

public static final int SOUTHWEST
See Also:
Constant Field Values

WEST

public static final int WEST
See Also:
Constant Field Values

NORTHWEST

public static final int NORTHWEST
See Also:
Constant Field Values

DIRNAMES

public static final java.lang.String[] DIRNAMES
Names for the directions.


LEFT

public static final int LEFT
Constants for direction changes.

See Also:
Constant Field Values

AHEAD

public static final int AHEAD
See Also:
Constant Field Values

RIGHT

public static final int RIGHT
See Also:
Constant Field Values

NOWHERE

public static final int NOWHERE
See Also:
Constant Field Values

REGULAR

public static final int REGULAR
Constants for the road types.

See Also:
Constant Field Values

START

public static final int START
See Also:
Constant Field Values

GOAL

public static final int GOAL
See Also:
Constant Field Values
Constructor Detail

Road

public Road(java.lang.String name,
            alpiv.trucks.RoadObserver observer)
Convenience constructor with name and observer. This creates a regular road with coordinates 0/0.

Parameters:
name - the name of the Road object
observer - whom to notify of traffic changes

Road

public Road(java.lang.String name,
            alpiv.trucks.RoadObserver observer,
            int type)
Convenience constructor with name, observer, and type. This creates a road of the given type with coordinates 0/0.

Parameters:
name - the name of the Road object
observer - whom to notify of traffic changes
type - what kind of road is this

Road

public Road(java.lang.String name,
            alpiv.trucks.RoadObserver observer,
            int x,
            int y,
            int type)
Full constructor. This creates a road of the given type at the given coordinates. Name and observer must be valid non-null Objects. The type must be one of the constants defined above.

Parameters:
name - the name of the Road object
observer - whom to notify of traffic changes
x - X coordinate for displaying this road
y - Y coordinate for displaying this road
type - what kind of road is this
Method Detail

nextDirection

public static int nextDirection(int current,
                                int turn)
A helper method to calculate direction changes. This takes the passed current direction, and calculates the new direction based on the given direction change constant.

Parameters:
current - the current direction
turn - the direction to move to
Returns:
the new direction

oppositeDirection

public static int oppositeDirection(int current)
A helper method to calculate the opposite of a direction.

Parameters:
current - the current direction
Returns:
the opposite direction of the current one

getType

public int getType()
Get the type of the road object. Returns one of the constants defined above.

Returns:
type of the road

getName

public java.lang.String getName()
Get the name of the road object.

Returns:
name of the road object.

getX

public int getX()
Get the X coordinate for map layout.

Returns:
the X coordinate of this road object

getY

public int getY()
Get the Y coordinate for map layout.

Returns:
the Y coordinate of this road object

getTraffic

public alpiv.trucks.Truck getTraffic()
Get the current traffic on this road. This is either a truck object or null.

Returns:
current ruck on the road, or null

setLocation

public void setLocation(int x,
                        int y)
Set this roads location for layout purposes.

Parameters:
x - the X coordinate of this road
y - the Y coordinate of this road

setTraffic

public void setTraffic(alpiv.trucks.Truck traffic)
Place or remove some traffic on the road. A null argument removes the traffic, an non-null arguments sets it. The latter is only allowed if the road is currently empty. Otherwise, a crash occurs, indicated by a RuntimeException. Whenever this method is called, the roads observer is notified of the change.

Parameters:
traffic - the truck to place on this road, or null to clear it

setExit

public void setExit(int direction,
                    Road to)
Link this road to another one in the given direction. The direction must be one of the constants defined above.

Parameters:
direction - direction where to reach the other road
to - the road the exits leads to

getExit

public Road getExit(int direction)
Get the road reachable in the given direction. The direction must be one of the constants defined above.

Parameters:
direction - direction to check
Returns:
The road reachable in the given direction, or null if none