|
From: Stefan F. <ste...@us...> - 2011-07-23 08:14:13
|
rails/game/GameManager.java | 24 ++++++++++++++----------
rails/game/GameManagerI.java | 6 ++++--
rails/game/Token.java | 8 ++++----
rails/game/special/SpecialProperty.java | 6 ++++--
4 files changed, 26 insertions(+), 18 deletions(-)
New commits:
commit d4b5d377e9240fc5bd3dd241fe29ccf9da0c1608
Author: Stefan Frey <ste...@we...>
Date: Sat Jul 23 10:16:07 2011 +0200
Fixed problem in new storage mechanism
diff --git a/rails/game/GameManager.java b/rails/game/GameManager.java
index 56c72ad..77b2118 100644
--- a/rails/game/GameManager.java
+++ b/rails/game/GameManager.java
@@ -225,9 +225,9 @@ public class GameManager implements ConfigurableComponentI, GameManagerI {
// 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 Map<String, Object> objectStorage = new HashMap<String, Object>();
+ protected Map<String, Integer> storageIds = new HashMap<String, Integer>();
+
protected static Logger log =
Logger.getLogger(GameManager.class.getPackage().getName());
@@ -1873,17 +1873,21 @@ public class GameManager implements ConfigurableComponentI, GameManagerI {
}
public void resetStorage() {
- objectStorage = new HashMap<Integer, Object>();
- storageId = 0;
+ objectStorage = new HashMap<String, Object>();
+ storageIds = new HashMap<String, Integer>();
}
- public int storeObject(Object object) {
- objectStorage.put(storageId++, object);
- return storageId;
+ public int storeObject(String typeName, Object object) {
+ Integer id = storageIds.get(typeName);
+ if (id == null) id = 0;
+ objectStorage.put(typeName + id, object);
+ storageIds.put(typeName, id + 1); // store next id
+ log.debug("Stores " + typeName + " + on id " + id);
+ return id;
}
- public Object retrieveObject(int id) {
- return objectStorage.get(id);
+ public Object retrieveObject(String typeName, int id) {
+ return objectStorage.get(typeName + id);
}
}
diff --git a/rails/game/GameManagerI.java b/rails/game/GameManagerI.java
index e95c71f..e9d5936 100644
--- a/rails/game/GameManagerI.java
+++ b/rails/game/GameManagerI.java
@@ -225,19 +225,21 @@ public interface GameManagerI extends MoveableHolder, ConfigurableComponentI {
/**
* store element in storage
+ * @param name to identify the type of the object to retrieve
* @param object to store
* @return unique id of the object in the storage
* TODO move to a better place
*/
- public int storeObject(Object object);
+ public int storeObject(String typeName, Object object);
/**
* ask storage for object
+ * @param name to identify the type of the object to retrieve
* @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);
+ public Object retrieveObject(String typeName, int id);
diff --git a/rails/game/Token.java b/rails/game/Token.java
index 8d21a70..ee38386 100644
--- a/rails/game/Token.java
+++ b/rails/game/Token.java
@@ -23,18 +23,18 @@ public abstract class Token implements TokenI {
protected String uniqueId;
// TODO: storing id in String is for legacy reasons
- protected static String ID_PREFIX = "Token_";
+ protected static String STORAGE_NAME = "Token";
protected static Logger log =
Logger.getLogger(Token.class.getPackage().getName());
public Token() {
- uniqueId = ID_PREFIX + GameManager.getInstance().storeObject(this);
+ uniqueId = STORAGE_NAME + "_" + GameManager.getInstance().storeObject(STORAGE_NAME, this);
}
public static TokenI getByUniqueId(String id) {
- int i = Integer.valueOf(id.replace(ID_PREFIX, ""));
- return (Token)GameManager.getInstance().retrieveObject(i);
+ int i = Integer.valueOf(id.replace(STORAGE_NAME + "_", ""));
+ return (Token)GameManager.getInstance().retrieveObject(STORAGE_NAME, i);
}
public String getUniqueId() {
diff --git a/rails/game/special/SpecialProperty.java b/rails/game/special/SpecialProperty.java
index a165f16..8994da7 100644
--- a/rails/game/special/SpecialProperty.java
+++ b/rails/game/special/SpecialProperty.java
@@ -50,6 +50,8 @@ public abstract class SpecialProperty implements SpecialPropertyI {
protected String description = "";
protected int uniqueId;
+
+ protected static final String STORAGE_NAME = "SpecialProperty";
/** To give subclasses access to the various 'managers' */
protected GameManagerI gameManager;
@@ -59,7 +61,7 @@ public abstract class SpecialProperty implements SpecialPropertyI {
public SpecialProperty() {
gameManager = GameManager.getInstance();
- uniqueId = gameManager.storeObject(this);
+ uniqueId = gameManager.storeObject(STORAGE_NAME, this);
}
public void configureFromXML(Tag tag) throws ConfigurationException {
@@ -108,7 +110,7 @@ public abstract class SpecialProperty implements SpecialPropertyI {
}
public static SpecialPropertyI getByUniqueId(int i) {
- return (SpecialPropertyI)GameManager.getInstance().retrieveObject(i);
+ return (SpecialPropertyI)GameManager.getInstance().retrieveObject(STORAGE_NAME, i);
}
public void setCompany(CompanyI company) {
|