From 0a845d99117e4d4186d5be5a63cd8719901caafb Mon Sep 17 00:00:00 2001
From: Pacien TRAN-GIRARD
Date: Sat, 3 May 2014 19:06:37 +0200
Subject: Use IntrinsicMap (custom HashMap)
---
src/esieequest/controller/GameEngine.java | 2 +-
src/esieequest/esieequest.gwt.xml | 1 +
src/esieequest/model/Player.java | 39 ++++++--------------
src/esieequest/model/items/Item.java | 8 ++++-
src/net/pacien/util/IntrinsicMap.java | 59 +++++++++++++++++++++++++++++++
src/net/pacien/util/Mappable.java | 7 ++++
src/net/pacien/util/pacienutils.gwt.xml | 3 ++
src/net/pacien/util/package-info.java | 8 +++++
8 files changed, 96 insertions(+), 31 deletions(-)
create mode 100644 src/net/pacien/util/IntrinsicMap.java
create mode 100644 src/net/pacien/util/Mappable.java
create mode 100644 src/net/pacien/util/pacienutils.gwt.xml
create mode 100644 src/net/pacien/util/package-info.java
diff --git a/src/esieequest/controller/GameEngine.java b/src/esieequest/controller/GameEngine.java
index fed79d6..c3a8725 100644
--- a/src/esieequest/controller/GameEngine.java
+++ b/src/esieequest/controller/GameEngine.java
@@ -41,7 +41,7 @@ public class GameEngine {
* the command String
*/
public void interpret(final String inputString) {
- final Input input = new Input(inputString);
+ final Input input = new Input(inputString.toLowerCase());
final Command command = input.getCommand();
if (command == null) {
diff --git a/src/esieequest/esieequest.gwt.xml b/src/esieequest/esieequest.gwt.xml
index 1a5c10c..b7dbcbf 100644
--- a/src/esieequest/esieequest.gwt.xml
+++ b/src/esieequest/esieequest.gwt.xml
@@ -19,6 +19,7 @@ so that your app can take advantage of the latest GWT module capabilities.
+
diff --git a/src/esieequest/model/Player.java b/src/esieequest/model/Player.java
index 4060367..2ab8190 100644
--- a/src/esieequest/model/Player.java
+++ b/src/esieequest/model/Player.java
@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
+import net.pacien.util.IntrinsicMap;
import esieequest.controller.Utils;
import esieequest.model.items.Item;
import esieequest.model.map.Direction;
@@ -22,7 +23,7 @@ public class Player {
private final Stack previousRooms;
private Direction currentDirection;
- private final List- items;
+ private final IntrinsicMap items;
private final int carryWeightLimit;
private int nbSteps;
@@ -43,9 +44,9 @@ public class Player {
*/
private Player(final Room currentRoom, final Direction currentDirection, final int carryWeightLimit, final int nbStepsLimit) {
this.currentRoom = currentRoom;
- this.previousRooms = new Stack();
+ this.previousRooms = new Stack<>();
this.currentDirection = currentDirection;
- this.items = new ArrayList
- ();
+ this.items = new IntrinsicMap<>();
this.carryWeightLimit = carryWeightLimit;
this.nbSteps = 0;
this.nbStepsLimit = nbStepsLimit;
@@ -166,7 +167,7 @@ public class Player {
* @return if the Player have the Item
*/
public boolean hasItem(final Item item) {
- return this.items.contains(item);
+ return this.items.containsValue(item);
}
/**
@@ -178,7 +179,7 @@ public class Player {
* @return if the Player have the Item
*/
public boolean hasItem(final String itemName) {
- return this.getItem(itemName) != null;
+ return this.items.containsKey(itemName);
}
/**
@@ -186,7 +187,7 @@ public class Player {
*/
public int getItemsWeight() {
int totalWeight = 0;
- for (final Item item : this.items) {
+ for (final Item item : this.items.values()) {
totalWeight += item.getWeight();
}
return totalWeight;
@@ -199,7 +200,7 @@ public class Player {
* the Item to add
*/
public void addItem(final Item item) {
- this.items.add(item);
+ this.items.put(item);
}
/**
@@ -222,28 +223,13 @@ public class Player {
this.removeItem(this.getItem(itemName));
}
- /**
- * Lists all the Player's Item-s names.
- *
- * @return the list of all the names of the Item-s
- */
- public List listItemsNames() {
- final ArrayList itemsNamesList = new ArrayList();
-
- for (final Item item : this.items) {
- itemsNamesList.add(item.getName());
- }
-
- return itemsNamesList;
- }
-
/**
* Lists all the Player's Item-s names in a String.
*
* @return the list of the Item-s in a String
*/
public String listItemsNamesString() {
- return Utils.listToString(this.listItemsNames(), Text.INVENTORY_PREFIX.getText(), Text.ITEMS_NO_ITEM.getText(), Text.INVENTORY_SUFFIX.getText());
+ return Utils.listToString(new ArrayList(this.items.keySet()), Text.INVENTORY_PREFIX.getText(), Text.ITEMS_NO_ITEM.getText(), Text.INVENTORY_SUFFIX.getText());
}
/**
@@ -262,12 +248,7 @@ public class Player {
* @return the Item
*/
public Item getItem(final String itemName) {
- for (final Item item : this.items) {
- if (item.getName().toLowerCase().equals(itemName.toLowerCase())) {
- return item;
- }
- }
- return null;
+ return this.items.get(itemName);
}
/**
diff --git a/src/esieequest/model/items/Item.java b/src/esieequest/model/items/Item.java
index 2a0aa2c..491b3ef 100644
--- a/src/esieequest/model/items/Item.java
+++ b/src/esieequest/model/items/Item.java
@@ -1,9 +1,10 @@
package esieequest.model.items;
+import net.pacien.util.Mappable;
import esieequest.model.Game;
import esieequest.view.Viewable;
-public enum Item {
+public enum Item implements Mappable {
// @formatter:off
@@ -64,4 +65,9 @@ public enum Item {
this.item.use(game, view);
}
+ @Override
+ public Object getKey() {
+ return this.item.getName().toLowerCase();
+ }
+
}
diff --git a/src/net/pacien/util/IntrinsicMap.java b/src/net/pacien/util/IntrinsicMap.java
new file mode 100644
index 0000000..a68dcbe
--- /dev/null
+++ b/src/net/pacien/util/IntrinsicMap.java
@@ -0,0 +1,59 @@
+/**
+ *
+ */
+package net.pacien.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author pacien
+ *
+ */
+public class IntrinsicMap extends HashMap {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ /**
+ *
+ */
+ public IntrinsicMap() {
+ super();
+ }
+
+ /**
+ * @param initialCapacity
+ */
+ public IntrinsicMap(int initialCapacity) {
+ super(initialCapacity);
+ }
+
+ /**
+ * @param m
+ */
+ public IntrinsicMap(Map extends K, ? extends V> m) {
+ super(m);
+ }
+
+ /**
+ * @param initialCapacity
+ * @param loadFactor
+ */
+ public IntrinsicMap(int initialCapacity, float loadFactor) {
+ super(initialCapacity, loadFactor);
+ }
+
+ @SuppressWarnings("unchecked")
+ public V put(V value) {
+ return super.put((K) value.getKey(), value);
+ }
+
+ @Override
+ public V put(K key, V value) {
+ return this.put(value);
+ }
+
+}
diff --git a/src/net/pacien/util/Mappable.java b/src/net/pacien/util/Mappable.java
new file mode 100644
index 0000000..b5bbf06
--- /dev/null
+++ b/src/net/pacien/util/Mappable.java
@@ -0,0 +1,7 @@
+package net.pacien.util;
+
+public interface Mappable {
+
+ public Object getKey();
+
+}
diff --git a/src/net/pacien/util/pacienutils.gwt.xml b/src/net/pacien/util/pacienutils.gwt.xml
new file mode 100644
index 0000000..d2a88de
--- /dev/null
+++ b/src/net/pacien/util/pacienutils.gwt.xml
@@ -0,0 +1,3 @@
+
+
+
\ No newline at end of file
diff --git a/src/net/pacien/util/package-info.java b/src/net/pacien/util/package-info.java
new file mode 100644
index 0000000..924b56f
--- /dev/null
+++ b/src/net/pacien/util/package-info.java
@@ -0,0 +1,8 @@
+/**
+ *
+ */
+/**
+ * @author pacien
+ *
+ */
+package net.pacien.util;
\ No newline at end of file
--
cgit v1.2.3