();
+ for (int i = 0; i < 10; i++) {
+ values.add(null);
+ }
+ this.keys = new String[10];
+ this.usedSlots = new boolean[] { false, false, false, false, false, false, false, false, false, false };
+ this.storedElements = 0;
+ }
+
+ /**
+ * Add an element
+ *
+ * @param key
+ * @param value
+ * @throws DuplicateKeyException
+ */
+ public void add(String key, T value) {
+ // duplicate check
+ for (int i = 0; i < this.keys.length; i++) {
+ if (usedSlots[i]) {
+ if (keys[i].equals(key)) throw new DuplicateKeyException(key);
+ }
+ }
+
+ // Make bigger arrays when running out of space
+ if (this.storedElements == this.keys.length) {
+ String[] keys = this.keys;
+ boolean[] usedSlots = this.usedSlots;
+ this.keys = new String[keys.length + 10];
+ this.usedSlots = new boolean[usedSlots.length + 10];
+ for (int i = 0; i < keys.length; i++) {
+ this.keys[i] = keys[i];
+ this.usedSlots[i] = usedSlots[i];
+ }
+ for (int i = 0; i < 10; i++) {
+ // ensure capacity
+ this.values.add(null);
+ }
+ }
+
+ // Find next unused slot
+ int unusedSlot = 0;
+ for (int i = 0; i < this.usedSlots.length; i++) {
+ if (!this.usedSlots[i]) {
+ unusedSlot = i;
+ break;
+ }
+ }
+
+ this.keys[unusedSlot] = key;
+ this.values.set(unusedSlot, value);
+ usedSlots[unusedSlot] = true;
+ this.storedElements++;
+ }
+
+ /**
+ * Get a value by key
+ *
+ * @param key
+ * @return value (null if key not found)
+ */
+ public T get(String key) {
+ for (int i = 0; i < usedSlots.length; i++) {
+ if (usedSlots[i]) {
+ if (keys[i].equals(key)) return values.get(i);
+ }
+ }
+ return null;
+ }
+
+ public void set(String key, T value) {
+ for (int i = 0; i < usedSlots.length; i++) {
+ if (usedSlots[i]) {
+ if (keys[i].equals(key)) values.set(i, value);
+ }
+ }
+ }
+
+ /**
+ *
+ * Remove an element from the LUT
+ *
+ *
+ *
+ * This function marks the position of key as unused. If you actually want to
+ * ensure that the object is removed from the LUT, use
+ * {@link LookUpTable#fullRemove(String)} instead.
+ *
+ *
+ * @param key
+ */
+ public void remove(String key) {
+ for (int i = 0; i < usedSlots.length; i++) {
+ if (usedSlots[i]) {
+ if (keys[i].equals(key)) usedSlots[i] = false;
+ }
+ }
+ storedElements--;
+ }
+
+ /**
+ * Remove an element from the LUT by actually setting it to null.
+ *
+ * @param key
+ */
+ public void fullRemove(String key) {
+ for (int i = 0; i < usedSlots.length; i++) {
+ if (usedSlots[i]) {
+ if (keys[i].equals(key)) {
+ values.set(i, null);
+ usedSlots[i] = false;
+ }
+ }
+ }
+ storedElements--;
+ }
+
+ public int storedElements() {
+ return this.storedElements;
+ }
+
+ /**
+ * Gets a key by its position in the LUT. Unused slots are ignored and do not
+ * count towards the position.
+ *
+ * @param index
+ * @return key
+ */
+ public String getKey(int index) {
+ int i = 0;
+ int j = 0;
+ while (i < index) {
+ j++;
+ if (usedSlots[j]) i++;
+ }
+ return keys[j];
+ }
+
+ public static class DuplicateKeyException extends RuntimeException {
+ public DuplicateKeyException() {
+ super("Tried adding an element with a duplicte key to a LookUpTable.");
+ }
+
+ public DuplicateKeyException(String key) {
+ super("Tried adding an element with a duplicte key to a LookUpTable: " + key);
+ }
+ }
+}
diff --git a/src/test/TestLUT.java b/src/test/TestLUT.java
new file mode 100644
index 0000000..59071d8
--- /dev/null
+++ b/src/test/TestLUT.java
@@ -0,0 +1,41 @@
+import lostcave.deathchests.util.LookUpTable;
+import lostcave.deathchests.util.LookUpTable.DuplicateKeyException;
+
+public class TestLUT {
+ public static void main(String[] args) {
+ System.out.println("Testing LookUpTable...");
+
+ LookUpTable table = new LookUpTable<>();
+
+ table.add("one", 1);
+ table.add("two", 2);
+ table.add("three", 3);
+
+ if (table.get("one") != 1 || table.get("two") != 2 || table.get("three") != 3) System.err.println("Failed add test.");
+ else System.out.println("Passed add test.");
+
+ if (table.getKey(1).equals("two")) System.out.println("Passed getKey test (1/2).");
+ else System.err.println("Failed getKey test (1/2).");
+
+ table.remove("two");
+ if (table.get("two") == null) System.out.println("Passed remove test.");
+ else System.err.println("Failed remove test");
+
+ if (table.getKey(1).equals("three")) System.out.println("Passed getKey test (2/2).");
+ else System.err.println("Failed getKey test (2/2): " + table.getKey(1));
+
+ if (table.storedElements()==2) System.out.println("Passes storedElements test.");
+ else System.out.println("Failed storedElements test.");
+
+ table.set("three", 33);
+ if (table.get("three")==33) System.out.println("Passed set test.");
+ else System.out.println("Failed set test.");
+
+ try {
+ table.add("three", 2);
+ System.err.println("Failed duplicate key test.");
+ } catch (DuplicateKeyException e) {
+ System.out.println("Passed duplicate key test.");
+ }
+ }
+}