jfreerails.world.top
Class WorldDiffs

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

public class WorldDiffs
extends WorldImpl

An implemenation of World that only stores differences relative to an underlying world object. Below is some stylised code showing what this class does. The key object could be a location on the map, a position in a list etc.

 HashMap underlyingWorldObject;
 
 HashMap differences;
 
 public void put(Object key, Object value) {
        if (underlyingWorldObject.get(key).equals(value)) {
                if (differences.containsKey(key)) {
                        differences.remove(key);
                }
        } else {
                differences.put(key, value);
        }
 }
 
 public Object get(Object key) {
        if (differences.containsKey(key)) {
                return differences.get(key);
        } else {
                return underlyingWorldObject.get(key);
        }
 }
 
The advantages of using an instance of this class instead of a copy of the world object are:
  1. Uses less memory.
  2. Lets you pinpoint where differences on the map are, so you don't need to check every tile.

Version:
2
Author:
Luke
See Also:
Serialized Form

Nested Class Summary
static class WorldDiffs.LISTID
           
 
Nested classes/interfaces inherited from class jfreerails.world.top.WorldImpl
WorldImpl.ActivityAndTime
 
Field Summary
private  java.util.SortedMap<ListKey,java.lang.Object> listDiff
           
private  java.util.HashMap<ImPoint,java.lang.Object> mapDiff
          Stores the differences on the map, ImPoint are used as keys.
private static long serialVersionUID
           
private  WorldImpl underlying
           
 
Fields inherited from class jfreerails.world.top.WorldImpl
activityLists, bankAccounts, currentBalance, items, lists, map, players, sharedLists, time
 
Constructor Summary
WorldDiffs(ReadOnlyWorld row)
           
 
Method Summary
 java.lang.Object getDiff(ListKey key)
           
 java.util.Iterator<ListKey> getListDiffs()
           
 java.util.Iterator<ImPoint> getMapDiffs()
          The iterator returns instances of java.awt.Point that store the coordinates of tiles that are different to the underlying world object.
 int getMapHeight()
          Returns the height of the map in tiles.
 int getMapWidth()
          Returns the width of the map in tiles.
 FreerailsSerializable getTile(int x, int y)
          Returns the tile at the specified position on the map.
 ReadOnlyWorld getUnderlying()
           
 boolean isDifferent()
           
 int listDiffs()
          Used by unit tests.
 int numberOfMapDifferences()
          Used by unit tests.
 void reset()
          After this method returns, all differences are cleared and calls to methods on this object should produce the same results as calls the the corresponding methods on the underlying world object.
 void setTile(int x, int y, FreerailsSerializable tile)
          Replaces the tile at the specified position on the map with the specified tile.
 
Methods inherited from class jfreerails.world.top.WorldImpl
add, add, add, addActiveEntity, addPlayer, addTransaction, boundsContain, boundsContain, boundsContain, currentTime, defensiveCopy, equals, get, get, get, getActivities, getCurrentBalance, getID, getNumberOfActiveEntities, getNumberOfPlayers, getNumberOfTransactions, getPlayer, getTransaction, getTransactionTimeStamp, hashCode, isPlayer, removeLast, removeLast, removeLastActiveEntity, removeLastActivity, removeLastPlayer, removeLastTransaction, set, set, set, setTime, setupItems, setupMap, size, size, size
 
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

listDiff

private final java.util.SortedMap<ListKey,java.lang.Object> listDiff

mapDiff

private final java.util.HashMap<ImPoint,java.lang.Object> mapDiff
Stores the differences on the map, ImPoint are used as keys.


underlying

private final WorldImpl underlying
Constructor Detail

WorldDiffs

public WorldDiffs(ReadOnlyWorld row)
Method Detail

getMapDiffs

public java.util.Iterator<ImPoint> getMapDiffs()
The iterator returns instances of java.awt.Point that store the coordinates of tiles that are different to the underlying world object.


getListDiffs

public java.util.Iterator<ListKey> getListDiffs()

getDiff

public java.lang.Object getDiff(ListKey key)

getMapHeight

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

Specified by:
getMapHeight in interface ReadOnlyWorld
Overrides:
getMapHeight in class WorldImpl

getMapWidth

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

Specified by:
getMapWidth in interface ReadOnlyWorld
Overrides:
getMapWidth in class WorldImpl

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
Overrides:
getTile in class WorldImpl

numberOfMapDifferences

public int numberOfMapDifferences()
Used by unit tests.


listDiffs

public int listDiffs()
Used by unit tests.


reset

public void reset()
After this method returns, all differences are cleared and calls to methods on this object should produce the same results as calls the the corresponding methods on the underlying world object.


setTile

public void setTile(int x,
                    int y,
                    FreerailsSerializable tile)
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
Overrides:
setTile in class WorldImpl

isDifferent

public boolean isDifferent()

getUnderlying

public ReadOnlyWorld getUnderlying()