From: Stefan F. <ste...@us...> - 2011-07-23 07:37:24
|
rails/game/GameManager.java | 23 +++++++++++++++++++++-- rails/game/GameManagerI.java | 28 +++++++++++++++++++++++++++- rails/game/Token.java | 12 ++++++------ rails/game/special/SpecialProperty.java | 9 ++------- 4 files changed, 56 insertions(+), 16 deletions(-) New commits: commit c9b5d4d6a3884eddf0546b69883e1a2c34bd1966 Author: Stefan Frey <ste...@we...> Date: Sat Jul 23 09:15:29 2011 +0200 Removed static class variables and moved that to a centralize storage in GameManager diff --git a/rails/game/GameManager.java b/rails/game/GameManager.java index 3e16978..56c72ad 100644 --- a/rails/game/GameManager.java +++ b/rails/game/GameManager.java @@ -67,7 +67,7 @@ public class GameManager implements ConfigurableComponentI, GameManagerI { // map of correctionManagers protected Map<CorrectionType, CorrectionManagerI> correctionManagers = new HashMap<CorrectionType, CorrectionManagerI>(); - + protected String gameName; protected Map<String, String> gameOptions; @@ -223,6 +223,11 @@ public class GameManager implements ConfigurableComponentI, GameManagerI { */ protected GameDef.OrStep skippedStep = null; + // storage to replace static class variables + // TODO: Move that to a better place + protected Map<Integer, Object> objectStorage = new HashMap<Integer, Object>(); + protected int storageId = 0; + protected static Logger log = Logger.getLogger(GameManager.class.getPackage().getName()); @@ -1866,6 +1871,20 @@ public class GameManager implements ConfigurableComponentI, GameManagerI { return players.get(0); } - + + public void resetStorage() { + objectStorage = new HashMap<Integer, Object>(); + storageId = 0; + } + + public int storeObject(Object object) { + objectStorage.put(storageId++, object); + return storageId; + } + + public Object retrieveObject(int id) { + return objectStorage.get(id); + } + } diff --git a/rails/game/GameManagerI.java b/rails/game/GameManagerI.java index 8555a07..e95c71f 100644 --- a/rails/game/GameManagerI.java +++ b/rails/game/GameManagerI.java @@ -215,5 +215,31 @@ public interface GameManagerI extends MoveableHolder, ConfigurableComponentI { public void setSkipDone (GameDef.OrStep step); public Player reorderPlayersByCash(boolean high); - //public void reorderPlayersByCash(boolean high); + + /** + * reset the storage for other elements like tokens, special property + * that a referred by unique ids + * TODO + */ + public void resetStorage(); + + /** + * store element in storage + * @param object to store + * @return unique id of the object in the storage + * TODO move to a better place + */ + public int storeObject(Object object); + + /** + * ask storage for object + * @param identifier in storage + * @return object stored under the id (null if none is stored) + * TODO move to a better place + */ + public Object retrieveObject(int id); + + + + } \ No newline at end of file diff --git a/rails/game/Token.java b/rails/game/Token.java index 97b19d1..8d21a70 100644 --- a/rails/game/Token.java +++ b/rails/game/Token.java @@ -21,20 +21,20 @@ public abstract class Token implements TokenI { protected TokenHolder holder = null; protected String description = ""; protected String uniqueId; - - private static Map<String, TokenI> tokenMap = new HashMap<String, TokenI>(); - private static int index = 0; + + // TODO: storing id in String is for legacy reasons + protected static String ID_PREFIX = "Token_"; protected static Logger log = Logger.getLogger(Token.class.getPackage().getName()); public Token() { - uniqueId = "Token_" + (index++); - tokenMap.put(uniqueId, this); + uniqueId = ID_PREFIX + GameManager.getInstance().storeObject(this); } public static TokenI getByUniqueId(String id) { - return tokenMap.get(id); + int i = Integer.valueOf(id.replace(ID_PREFIX, "")); + return (Token)GameManager.getInstance().retrieveObject(i); } public String getUniqueId() { diff --git a/rails/game/special/SpecialProperty.java b/rails/game/special/SpecialProperty.java index 0daeb23..a165f16 100644 --- a/rails/game/special/SpecialProperty.java +++ b/rails/game/special/SpecialProperty.java @@ -54,17 +54,12 @@ public abstract class SpecialProperty implements SpecialPropertyI { /** To give subclasses access to the various 'managers' */ protected GameManagerI gameManager; - protected static Map<Integer, SpecialPropertyI> spMap = new HashMap<Integer, SpecialPropertyI>(); - - protected static int lastIndex = 0; - protected static Logger log = Logger.getLogger(SpecialProperty.class.getPackage().getName()); public SpecialProperty() { - uniqueId = ++lastIndex; - spMap.put(uniqueId, this); gameManager = GameManager.getInstance(); + uniqueId = gameManager.storeObject(this); } public void configureFromXML(Tag tag) throws ConfigurationException { @@ -113,7 +108,7 @@ public abstract class SpecialProperty implements SpecialPropertyI { } public static SpecialPropertyI getByUniqueId(int i) { - return spMap.get(i); + return (SpecialPropertyI)GameManager.getInstance().retrieveObject(i); } public void setCompany(CompanyI company) { |