jfreerails.world.top
Class WorldImpl

java.lang.Object
  extended by jfreerails.world.top.WorldImpl
All Implemented Interfaces:
java.io.Serializable, FreerailsMutableSerializable, ReadOnlyWorld, World
Direct Known Subclasses:
WorldDiffs

public class WorldImpl
extends java.lang.Object
implements World

An implementation of World that uses standard java.util collections internally.

Author:
Luke
See Also:
Serialized Form

Nested Class Summary
static class WorldImpl.ActivityAndTime
           
 
Field Summary
(package private)  List3D<WorldImpl.ActivityAndTime> activityLists
          A 3D list: D1 is player, D2 is train id, D3 is train position.
(package private)  List2D<TransactionAndTimeStamp> bankAccounts
          A 2D list: D1 is player, D2 is transaction.
(package private)  List1D<Money> currentBalance
           
(package private)  List1D<FreerailsSerializable> items
           
(package private)  List3D<FreerailsSerializable> lists
          A 3D list: D1 is player, D2 is type, D3 is element.
(package private)  FreerailsSerializable[][] map
           
(package private)  List1D<Player> players
           
private static long serialVersionUID
           
(package private)  List2D<FreerailsSerializable> sharedLists
          A 2D list: D1 is type, D2 is element.
(package private)  GameTime time
           
 
Constructor Summary
WorldImpl()
           
WorldImpl(int mapWidth, int mapHeight)
           
 
Method Summary
 void add(FreerailsPrincipal p, int index, Activity element)
           
 int add(FreerailsPrincipal p, KEY key, FreerailsSerializable element)
          Appends the specified element to the end of the specifed list and returns the index that can be used to retrieve it.
 int add(SKEY key, FreerailsSerializable element)
          Appends the specified element to the end of the specifed list and returns the index that can be used to retrieve it.
 int addActiveEntity(FreerailsPrincipal p, Activity element)
           
 int addPlayer(Player player)
           
 void addTransaction(FreerailsPrincipal p, Transaction t)
          Adds the specified transaction to the specified principal's bank account.
 boolean boundsContain(FreerailsPrincipal p, KEY k, int index)
           
 boolean boundsContain(int x, int y)
           
 boolean boundsContain(SKEY k, int index)
           
 GameTime currentTime()
           
 World defensiveCopy()
          Returns a copy of this world object - making changes to this copy will not change this object.
 boolean equals(java.lang.Object o)
           
 FreerailsSerializable get(FreerailsPrincipal p, KEY key, int index)
          Returns the element at the specified position in the specified list.
 FreerailsSerializable get(ITEM item)
          Returns the element mapped to the specified item.
 FreerailsSerializable get(SKEY key, int index)
          Returns the element at the specified position in the specified list.
 ActivityIterator getActivities(FreerailsPrincipal p, int index)
           
 Money getCurrentBalance(FreerailsPrincipal p)
           
 int getID(FreerailsPrincipal p)
           
 int getMapHeight()
          Returns the height of the map in tiles.
 int getMapWidth()
          Returns the width of the map in tiles.
 int getNumberOfActiveEntities(FreerailsPrincipal p)
           
 int getNumberOfPlayers()
           
 int getNumberOfTransactions(FreerailsPrincipal p)
           
 Player getPlayer(int i)
           
private  int getPlayerIndex(FreerailsPrincipal p)
           
 FreerailsSerializable getTile(int x, int y)
          Returns the tile at the specified position on the map.
 Transaction getTransaction(FreerailsPrincipal p, int i)
           
 GameTime getTransactionTimeStamp(FreerailsPrincipal p, int i)
           
 int hashCode()
           
 boolean isPlayer(FreerailsPrincipal p)
           
 FreerailsSerializable removeLast(FreerailsPrincipal p, KEY key)
          Removes the last element from the specified list.
 FreerailsSerializable removeLast(SKEY key)
          Removes the last element from the specified list.
 Activity removeLastActiveEntity(FreerailsPrincipal p)
           
 Activity removeLastActivity(FreerailsPrincipal p, int index)
           
 Player removeLastPlayer()
          Removes the last player to be added.
 Transaction removeLastTransaction(FreerailsPrincipal p)
          Removes and returns the last transaction added the the specified principal's bank account.
 void set(FreerailsPrincipal p, KEY key, int index, FreerailsSerializable element)
          Replaces the element at the specified position in the specified list with the specified element.
 void set(ITEM item, FreerailsSerializable element)
          Replaces the element mapped to the specified item with the specified element.
 void set(SKEY key, int index, FreerailsSerializable element)
          Replaces the element at the specified position in the specified list with the specified element.
 void setTile(int x, int y, FreerailsSerializable element)
          Replaces the tile at the specified position on the map with the specified tile.
 void setTime(GameTime t)
           
(package private)  void setupItems()
           
 void setupMap(int mapWidth, int mapHeight)
           
 int size(FreerailsPrincipal p)
          Returns number of active entities belonging to the specified principal.
 int size(FreerailsPrincipal p, KEY key)
          Returns the number of elements in the specified list.
 int size(SKEY key)
          Returns the number of elements in the specified list.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

activityLists

List3D<WorldImpl.ActivityAndTime> activityLists
A 3D list: D1 is player, D2 is train id, D3 is train position.


bankAccounts

List2D<TransactionAndTimeStamp> bankAccounts
A 2D list: D1 is player, D2 is transaction.


currentBalance

List1D<Money> currentBalance

items

List1D<FreerailsSerializable> items

lists

List3D<FreerailsSerializable> lists
A 3D list: D1 is player, D2 is type, D3 is element.


map

FreerailsSerializable[][] map

players

List1D<Player> players

sharedLists

List2D<FreerailsSerializable> sharedLists
A 2D list: D1 is type, D2 is element.


time

GameTime time
Constructor Detail

WorldImpl

public WorldImpl()

WorldImpl

public WorldImpl(int mapWidth,
                 int mapHeight)
Method Detail

add

public void add(FreerailsPrincipal p,
                int index,
                Activity element)
Specified by:
add in interface World

add

public int add(FreerailsPrincipal p,
               KEY key,
               FreerailsSerializable element)
Description copied from interface: World
Appends the specified element to the end of the specifed list and returns the index that can be used to retrieve it.

Specified by:
add in interface World

add

public int add(SKEY key,
               FreerailsSerializable element)
Description copied from interface: World
Appends the specified element to the end of the specifed list and returns the index that can be used to retrieve it.

Specified by:
add in interface World

addActiveEntity

public int addActiveEntity(FreerailsPrincipal p,
                           Activity element)
Specified by:
addActiveEntity in interface World

addPlayer

public int addPlayer(Player player)
Specified by:
addPlayer in interface World
Parameters:
player - Player to add
Returns:
index of the player

addTransaction

public void addTransaction(FreerailsPrincipal p,
                           Transaction t)
Description copied from interface: World
Adds the specified transaction to the specified principal's bank account.

Specified by:
addTransaction in interface World

boundsContain

public boolean boundsContain(FreerailsPrincipal p,
                             KEY k,
                             int index)
Specified by:
boundsContain in interface ReadOnlyWorld

boundsContain

public boolean boundsContain(int x,
                             int y)
Specified by:
boundsContain in interface ReadOnlyWorld

boundsContain

public boolean boundsContain(SKEY k,
                             int index)
Specified by:
boundsContain in interface ReadOnlyWorld

currentTime

public GameTime currentTime()
Specified by:
currentTime in interface ReadOnlyWorld

defensiveCopy

public World defensiveCopy()
Description copied from interface: World
Returns a copy of this world object - making changes to this copy will not change this object.

Specified by:
defensiveCopy in interface World

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

get

public FreerailsSerializable get(FreerailsPrincipal p,
                                 KEY key,
                                 int index)
Description copied from interface: ReadOnlyWorld
Returns the element at the specified position in the specified list.

Specified by:
get in interface ReadOnlyWorld

get

public FreerailsSerializable get(ITEM item)
Description copied from interface: ReadOnlyWorld
Returns the element mapped to the specified item.

Specified by:
get in interface ReadOnlyWorld

get

public FreerailsSerializable get(SKEY key,
                                 int index)
Description copied from interface: ReadOnlyWorld
Returns the element at the specified position in the specified list.

Specified by:
get in interface ReadOnlyWorld

getActivities

public ActivityIterator getActivities(FreerailsPrincipal p,
                                      int index)
Specified by:
getActivities in interface ReadOnlyWorld

getCurrentBalance

public Money getCurrentBalance(FreerailsPrincipal p)
Specified by:
getCurrentBalance in interface ReadOnlyWorld

getID

public int getID(FreerailsPrincipal p)
Specified by:
getID in interface ReadOnlyWorld

getMapHeight

public int getMapHeight()
Description copied from interface: ReadOnlyWorld
Returns the height of the map in tiles.

Specified by:
getMapHeight in interface ReadOnlyWorld

getMapWidth

public int getMapWidth()
Description copied from interface: ReadOnlyWorld
Returns the width of the map in tiles.

Specified by:
getMapWidth in interface ReadOnlyWorld

getNumberOfPlayers

public int getNumberOfPlayers()
Specified by:
getNumberOfPlayers in interface ReadOnlyWorld

getNumberOfTransactions

public int getNumberOfTransactions(FreerailsPrincipal p)
Specified by:
getNumberOfTransactions in interface ReadOnlyWorld

getPlayer

public Player getPlayer(int i)
Specified by:
getPlayer in interface ReadOnlyWorld

getPlayerIndex

private int getPlayerIndex(FreerailsPrincipal p)

getTile

public FreerailsSerializable getTile(int x,
                                     int y)
Description copied from interface: ReadOnlyWorld
Returns the tile at the specified position on the map.

Specified by:
getTile in interface ReadOnlyWorld

getTransaction

public Transaction getTransaction(FreerailsPrincipal p,
                                  int i)
Specified by:
getTransaction in interface ReadOnlyWorld

getTransactionTimeStamp

public GameTime getTransactionTimeStamp(FreerailsPrincipal p,
                                        int i)
Specified by:
getTransactionTimeStamp in interface ReadOnlyWorld

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

isPlayer

public boolean isPlayer(FreerailsPrincipal p)
Specified by:
isPlayer in interface ReadOnlyWorld

removeLast

public FreerailsSerializable removeLast(FreerailsPrincipal p,
                                        KEY key)
Description copied from interface: World
Removes the last element from the specified list.

Specified by:
removeLast in interface World

removeLast

public FreerailsSerializable removeLast(SKEY key)
Description copied from interface: World
Removes the last element from the specified list.

Specified by:
removeLast in interface World

removeLastActiveEntity

public Activity removeLastActiveEntity(FreerailsPrincipal p)
Specified by:
removeLastActiveEntity in interface World

removeLastActivity

public Activity removeLastActivity(FreerailsPrincipal p,
                                   int index)
Specified by:
removeLastActivity in interface World

removeLastPlayer

public Player removeLastPlayer()
Removes the last player to be added.

Specified by:
removeLastPlayer in interface World
Returns:
the player that was removed.
Throws:
java.lang.IllegalStateException - if any elements belonging to the player have not been removed.

removeLastTransaction

public Transaction removeLastTransaction(FreerailsPrincipal p)
Description copied from interface: World
Removes and returns the last transaction added the the specified principal's bank account. This method is only here so that moves that add transactions can be undone.

Specified by:
removeLastTransaction in interface World

set

public void set(FreerailsPrincipal p,
                KEY key,
                int index,
                FreerailsSerializable element)
Description copied from interface: World
Replaces the element at the specified position in the specified list with the specified element.

Specified by:
set in interface World

set

public void set(ITEM item,
                FreerailsSerializable element)
Description copied from interface: World
Replaces the element mapped to the specified item with the specified element.

Specified by:
set in interface World

set

public void set(SKEY key,
                int index,
                FreerailsSerializable element)
Description copied from interface: World
Replaces the element at the specified position in the specified list with the specified element.

Specified by:
set in interface World

setTile

public void setTile(int x,
                    int y,
                    FreerailsSerializable element)
Description copied from interface: World
Replaces the tile at the specified position on the map with the specified tile.

Specified by:
setTile in interface World

setTime

public void setTime(GameTime t)
Specified by:
setTime in interface World

setupItems

void setupItems()

setupMap

public void setupMap(int mapWidth,
                     int mapHeight)

size

public int size(FreerailsPrincipal p)
Description copied from interface: ReadOnlyWorld
Returns number of active entities belonging to the specified principal.

Specified by:
size in interface ReadOnlyWorld

size

public int size(FreerailsPrincipal p,
                KEY key)
Description copied from interface: ReadOnlyWorld
Returns the number of elements in the specified list.

Specified by:
size in interface ReadOnlyWorld

size

public int size(SKEY key)
Description copied from interface: ReadOnlyWorld
Returns the number of elements in the specified list.

Specified by:
size in interface ReadOnlyWorld

getNumberOfActiveEntities

public int getNumberOfActiveEntities(FreerailsPrincipal p)
Specified by:
getNumberOfActiveEntities in interface ReadOnlyWorld