From 5ca21e258188c07c43b6bda0557e8205739c3b11 Mon Sep 17 00:00:00 2001 From: Ziggy Date: Wed, 6 May 2015 11:43:57 +0200 Subject: [PATCH 1/8] Removed unncessary cast from SortedLinkedList.add() and refactored the exceptions. Created a unit test that tests SortedLinkedList. --- src/resources/SortedLinkedList.java | 20 ++++++------ test/resources/SortedLinkedListTest.java | 40 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 test/resources/SortedLinkedListTest.java diff --git a/src/resources/SortedLinkedList.java b/src/resources/SortedLinkedList.java index 17529e33f..ee465f590 100644 --- a/src/resources/SortedLinkedList.java +++ b/src/resources/SortedLinkedList.java @@ -34,18 +34,15 @@ import java.util.ListIterator; public class SortedLinkedList> extends LinkedList { private static final long serialVersionUID = -6776628467181994889L; - - public SortedLinkedList() { - - } + private static final UnsupportedOperationException OPERATIONEXCEPTION = + new UnsupportedOperationException("The sorted nature of the list prohibits this action."); @Override public boolean add(E e) { - Comparable element = (Comparable) e; - ListIterator iter = listIterator(); + ListIterator iter = super.listIterator(); + while (iter.hasNext()) { - E item = iter.next(); - if (element.compareTo(item) <= 0) { + if (e.compareTo(iter.next()) <= 0) { iter.previous(); iter.add(e); return true; @@ -57,7 +54,7 @@ public class SortedLinkedList> extends LinkedList { @Override public void add(int index, E element) { - throw new UnsupportedOperationException("The sorted nature of the list prohibits this action."); + throw OPERATIONEXCEPTION; } @Override @@ -65,17 +62,18 @@ public class SortedLinkedList> extends LinkedList { for (E item : c) { add(item); } + return true; } @Override public boolean addAll(int index, Collection c) { - throw new UnsupportedOperationException("The sorted nature of the list prohibits this action."); + throw OPERATIONEXCEPTION; } @Override public E set(int index, E element) { - throw new UnsupportedOperationException("The sorted nature of the list prohibits this action."); + throw OPERATIONEXCEPTION; } } diff --git a/test/resources/SortedLinkedListTest.java b/test/resources/SortedLinkedListTest.java new file mode 100644 index 000000000..c93dca5d1 --- /dev/null +++ b/test/resources/SortedLinkedListTest.java @@ -0,0 +1,40 @@ +package resources; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.*; + +public class SortedLinkedListTest { + + private static SortedLinkedList list; + private static final char TESTCHAR1 = 'a'; + private static final char TESTCHAR2 = 'b'; + private static final char TESTCHAR3 = 'c'; + + /** + * Characters are compared by numerical value and they + * should therefore be stored in such order within the list. + */ + @Test + public void testSorting() { + resetList(); + Iterator it; + + list.add(TESTCHAR2); + list.add(TESTCHAR3); + list.add(TESTCHAR1); + + it = list.listIterator(); + + assertTrue(it.next().equals(TESTCHAR1)); + assertTrue(it.next().equals(TESTCHAR2)); + assertTrue(it.next().equals(TESTCHAR3)); + } + + private static void resetList() { + list = new SortedLinkedList<>(); + } + +} From 3985528665318c0fbae00ccde668a2b4db63b53d Mon Sep 17 00:00:00 2001 From: Ziggeh Date: Wed, 6 May 2015 11:44:17 +0200 Subject: [PATCH 2/8] Removed unnecessary field encapsulation from resources.Attributes --- src/resources/Attributes.java | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/src/resources/Attributes.java b/src/resources/Attributes.java index 8dc69e56e..efb192afb 100644 --- a/src/resources/Attributes.java +++ b/src/resources/Attributes.java @@ -27,27 +27,13 @@ ***********************************************************************************/ package resources; - public class Attributes { - private int agility = 0; - private int constitution = 0; - private int luck = 0; - private int precision = 0; - private int stamina = 0; - private int strength = 0; + public int agility; + public int constitution; + public int luck; + public int precision; + public int stamina; + public int strength; - public void setAgility(int agility) { this.agility = agility; } - public void setConstitution(int constitution) { this.constitution = constitution; } - public void setLuck(int luck) { this.luck = luck; } - public void setPrecision(int precision) { this.precision = precision; } - public void setStamina(int stamina) { this.stamina = stamina; } - public void setStrength(int strength) { this.strength = strength; } - - public int getAgility() { return agility; } - public int getConstitution() { return constitution; } - public int getLuck() { return luck; } - public int getPrecision() { return precision; } - public int getStamina() { return stamina; } - public int getStrength() { return strength; } } From fabd4702a0a32377c058d087597703bad6361701 Mon Sep 17 00:00:00 2001 From: Ziggeh Date: Wed, 6 May 2015 11:55:48 +0200 Subject: [PATCH 3/8] Refactored the Scripts class to take varargs, effectively reducing the amount of "duplicate" methods --- src/resources/utilities/Scripts.java | 46 ++++------------------------ 1 file changed, 6 insertions(+), 40 deletions(-) diff --git a/src/resources/utilities/Scripts.java b/src/resources/utilities/Scripts.java index 7d9fe337a..bbfcfeeb2 100644 --- a/src/resources/utilities/Scripts.java +++ b/src/resources/utilities/Scripts.java @@ -35,54 +35,20 @@ import org.python.util.PythonInterpreter; public final class Scripts { private static final String SCRIPTS_PATH = "scripts/"; - // TODO: Variable arguments? - - public static void execute(String script, String method) { - if (!scriptExists(script)) - return; + public static void execute(String script, String method, Object... args) { + PythonInterpreter interp; - PythonInterpreter interp = new PythonInterpreter(); - interp.execfile(SCRIPTS_PATH + script); - interp.get(method).__call__(); - } - - public static void execute(String script, String method, Object arg1) { if (!scriptExists(script)) - return; + throw new IllegalArgumentException("Script " + SCRIPTS_PATH + script + " does not exist."); - PythonInterpreter interp = new PythonInterpreter(); - interp.execfile(SCRIPTS_PATH + script); - interp.get(method).__call__(Py.java2py(arg1)); - } - - public static void execute(String script, String method, Object arg1, Object arg2) { - if (!scriptExists(script)) - return; + interp = new PythonInterpreter(); - PythonInterpreter interp = new PythonInterpreter(); interp.execfile(SCRIPTS_PATH + script); - interp.get(method).__call__(Py.java2py(arg1), Py.java2py(arg2)); - } - - public static void execute(String script, String method, Object arg1, Object arg2, Object arg3) { - if (!scriptExists(script)) - return; - - PythonInterpreter interp = new PythonInterpreter(); - interp.execfile(SCRIPTS_PATH + script); - interp.get(method).__call__(Py.java2py(arg1), Py.java2py(arg2), Py.java2py(arg3)); - } - - public static void execute(String script, String method, Object arg1, Object arg2, Object arg3, Object arg4) { - if (!scriptExists(script)) - return; - - PythonInterpreter interp = new PythonInterpreter(); - interp.execfile(SCRIPTS_PATH + script); - interp.get(method).__call__(Py.java2py(arg1), Py.java2py(arg2), Py.java2py(arg3), Py.java2py(arg4)); + interp.get(method).__call__(Py.javas2pys(args)); } private static boolean scriptExists(String file) { return new File(SCRIPTS_PATH + file).exists(); } + } From fc74904d47f020689f3a76bd4124de194c522f92 Mon Sep 17 00:00:00 2001 From: Ziggeh Date: Wed, 6 May 2015 12:07:10 +0200 Subject: [PATCH 4/8] Changed resources.server_info.RelationalDatabase to be abstract --- src/resources/server_info/RelationalDatabase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resources/server_info/RelationalDatabase.java b/src/resources/server_info/RelationalDatabase.java index 451f72bf7..247039243 100644 --- a/src/resources/server_info/RelationalDatabase.java +++ b/src/resources/server_info/RelationalDatabase.java @@ -35,7 +35,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -public class RelationalDatabase { +public abstract class RelationalDatabase { private DatabaseMetaData metaData; private Connection connection; From 69dc2ebf318a0268da0763bac13ee0e77ec7c85e Mon Sep 17 00:00:00 2001 From: Ziggeh Date: Wed, 6 May 2015 19:47:29 +0200 Subject: [PATCH 5/8] Readded Field Encapsulation for resources.Attributes Changed each unsupported operation method in resources.SortedLinkedList to create new exception objects Renamed SortedLinkedListTest to TestSortedLinkedList --- src/resources/Attributes.java | 26 ++++++++++++++----- src/resources/SortedLinkedList.java | 10 +++---- ...istTest.java => TestSortedLinkedList.java} | 2 +- 3 files changed, 25 insertions(+), 13 deletions(-) rename test/resources/{SortedLinkedListTest.java => TestSortedLinkedList.java} (95%) diff --git a/src/resources/Attributes.java b/src/resources/Attributes.java index efb192afb..8dc69e56e 100644 --- a/src/resources/Attributes.java +++ b/src/resources/Attributes.java @@ -27,13 +27,27 @@ ***********************************************************************************/ package resources; + public class Attributes { - public int agility; - public int constitution; - public int luck; - public int precision; - public int stamina; - public int strength; + private int agility = 0; + private int constitution = 0; + private int luck = 0; + private int precision = 0; + private int stamina = 0; + private int strength = 0; + public void setAgility(int agility) { this.agility = agility; } + public void setConstitution(int constitution) { this.constitution = constitution; } + public void setLuck(int luck) { this.luck = luck; } + public void setPrecision(int precision) { this.precision = precision; } + public void setStamina(int stamina) { this.stamina = stamina; } + public void setStrength(int strength) { this.strength = strength; } + + public int getAgility() { return agility; } + public int getConstitution() { return constitution; } + public int getLuck() { return luck; } + public int getPrecision() { return precision; } + public int getStamina() { return stamina; } + public int getStrength() { return strength; } } diff --git a/src/resources/SortedLinkedList.java b/src/resources/SortedLinkedList.java index ee465f590..a1e335240 100644 --- a/src/resources/SortedLinkedList.java +++ b/src/resources/SortedLinkedList.java @@ -34,9 +34,7 @@ import java.util.ListIterator; public class SortedLinkedList> extends LinkedList { private static final long serialVersionUID = -6776628467181994889L; - private static final UnsupportedOperationException OPERATIONEXCEPTION = - new UnsupportedOperationException("The sorted nature of the list prohibits this action."); - + @Override public boolean add(E e) { ListIterator iter = super.listIterator(); @@ -54,7 +52,7 @@ public class SortedLinkedList> extends LinkedList { @Override public void add(int index, E element) { - throw OPERATIONEXCEPTION; + throw new UnsupportedOperationException("The sorted nature of the list prohibits this action."); } @Override @@ -68,12 +66,12 @@ public class SortedLinkedList> extends LinkedList { @Override public boolean addAll(int index, Collection c) { - throw OPERATIONEXCEPTION; + throw new UnsupportedOperationException("The sorted nature of the list prohibits this action."); } @Override public E set(int index, E element) { - throw OPERATIONEXCEPTION; + throw new UnsupportedOperationException("The sorted nature of the list prohibits this action."); } } diff --git a/test/resources/SortedLinkedListTest.java b/test/resources/TestSortedLinkedList.java similarity index 95% rename from test/resources/SortedLinkedListTest.java rename to test/resources/TestSortedLinkedList.java index c93dca5d1..eb8d49785 100644 --- a/test/resources/SortedLinkedListTest.java +++ b/test/resources/TestSortedLinkedList.java @@ -6,7 +6,7 @@ import java.util.Iterator; import org.junit.*; -public class SortedLinkedListTest { +public class TestSortedLinkedList { private static SortedLinkedList list; private static final char TESTCHAR1 = 'a'; From cf560ac2cf5a5b9dfab92e15112b16d630fe1cbf Mon Sep 17 00:00:00 2001 From: Ziggeh Date: Wed, 6 May 2015 21:13:40 +0200 Subject: [PATCH 6/8] Changed wildcard imports and moved class fields into the test method in TestSortedLinkedList --- test/resources/TestSortedLinkedList.java | 32 +++++++++--------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/test/resources/TestSortedLinkedList.java b/test/resources/TestSortedLinkedList.java index eb8d49785..dbcb506f1 100644 --- a/test/resources/TestSortedLinkedList.java +++ b/test/resources/TestSortedLinkedList.java @@ -1,40 +1,32 @@ package resources; -import static org.junit.Assert.*; - +import static org.junit.Assert.assertTrue; import java.util.Iterator; - -import org.junit.*; +import org.junit.Test; public class TestSortedLinkedList { - private static SortedLinkedList list; - private static final char TESTCHAR1 = 'a'; - private static final char TESTCHAR2 = 'b'; - private static final char TESTCHAR3 = 'c'; - /** * Characters are compared by numerical value and they * should therefore be stored in such order within the list. */ @Test public void testSorting() { - resetList(); + SortedLinkedList list = new SortedLinkedList<>(); + final char testchar1 = 'a'; + final char testchar2 = 'b'; + final char testchar3 = 'c'; Iterator it; - list.add(TESTCHAR2); - list.add(TESTCHAR3); - list.add(TESTCHAR1); + list.add(testchar2); + list.add(testchar3); + list.add(testchar1); it = list.listIterator(); - assertTrue(it.next().equals(TESTCHAR1)); - assertTrue(it.next().equals(TESTCHAR2)); - assertTrue(it.next().equals(TESTCHAR3)); - } - - private static void resetList() { - list = new SortedLinkedList<>(); + assertTrue(it.next().equals(testchar1)); + assertTrue(it.next().equals(testchar2)); + assertTrue(it.next().equals(testchar3)); } } From 5eace038003cafaa68c55659493b04b7fc66706a Mon Sep 17 00:00:00 2001 From: Ziggeh Date: Thu, 7 May 2015 08:43:33 +0200 Subject: [PATCH 7/8] Changed Scripts.execute() to no longer throw an exception when attempting to call a script that doesn't exist --- src/resources/utilities/Scripts.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resources/utilities/Scripts.java b/src/resources/utilities/Scripts.java index bbfcfeeb2..204721a4b 100644 --- a/src/resources/utilities/Scripts.java +++ b/src/resources/utilities/Scripts.java @@ -39,7 +39,7 @@ public final class Scripts { PythonInterpreter interp; if (!scriptExists(script)) - throw new IllegalArgumentException("Script " + SCRIPTS_PATH + script + " does not exist."); + return; // Ziggy: For now, this method gives no indication as to whether the script that was called actually was called or not. interp = new PythonInterpreter(); From 830b433e3797a9daa70e079d6ddc344b8068bc03 Mon Sep 17 00:00:00 2001 From: Obique Date: Thu, 7 May 2015 07:53:11 -0500 Subject: [PATCH 8/8] Changed Posture's hash map to Hashtable and Fixed a bug where DEAD isn't accessible in the enum's map --- src/resources/Posture.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/resources/Posture.java b/src/resources/Posture.java index dcc249b66..9b8479784 100644 --- a/src/resources/Posture.java +++ b/src/resources/Posture.java @@ -27,8 +27,8 @@ ***********************************************************************************/ package resources; +import java.util.Hashtable; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; public enum Posture { UPRIGHT (0x00), @@ -48,12 +48,14 @@ public enum Posture { DEAD (0x0E), INVALID (0x0E); - private static final Map POSTURE_MAP = new ConcurrentHashMap(); + private static final Map POSTURE_MAP = new Hashtable(15); private byte id; static { - for (Posture p : values()) - POSTURE_MAP.put(p.getId(), p); + for (Posture p : values()) { + if (p != INVALID) + POSTURE_MAP.put(p.getId(), p); + } } Posture(int id) { @@ -63,7 +65,10 @@ public enum Posture { public byte getId() { return id; } public static final Posture getFromId(byte id) { - Posture p = POSTURE_MAP.get(id); + Posture p = null; + synchronized (POSTURE_MAP) { + p = POSTURE_MAP.get(id); + } if (p == null) return INVALID; return p;