diff --git a/pom.xml b/pom.xml
index 36c092b..a4c733f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -32,5 +32,11 @@
4.12
test
+
+ org.junit.jupiter
+ junit-jupiter
+ RELEASE
+ compile
+
diff --git a/src/main/java/de/comparus/opensource/longmap/LongMapImpl.java b/src/main/java/de/comparus/opensource/longmap/LongMapImpl.java
index 2f0b54b..97e4fe2 100644
--- a/src/main/java/de/comparus/opensource/longmap/LongMapImpl.java
+++ b/src/main/java/de/comparus/opensource/longmap/LongMapImpl.java
@@ -1,43 +1,179 @@
package de.comparus.opensource.longmap;
public class LongMapImpl implements LongMap {
+ private Node node;
+ private int size;
+ private V[] values;
+
+ private static class Node {
+ private long key;
+ private V value;
+ private Node next;
+
+ public Node(long key, V value) {
+ this.value = value;
+ this.key = key;
+ this.next = next;
+ }
+
+ @Override
+ public String toString() {
+ return "Node{" +
+ "key=" + key +
+ ", value=" + value +
+ ", next=" + next +
+ '}';
+ }
+ }
+
+ @Override
public V put(long key, V value) {
- return null;
+ if (node == null) {
+ node = new Node(key, value);
+ size++;
+ return value;
+ }
+ writeToNext(key, value);
+ return value;
+ }
+
+ private Node writeToNext(long key, V value) {
+ Node bufferNode = node;
+ while (bufferNode.next != null) {
+ bufferNode = bufferNode.next;
+ }
+ bufferNode.next = new Node(key, value);
+ size++;
+ return bufferNode;
}
+ @Override
public V get(long key) {
+ Node bufferNode = node;
+ if (node == null) return null;
+ if (node.key == key) return node.value;
+ while (bufferNode.next != null) {
+ if (bufferNode.next.key == key) {
+ return bufferNode.next.value;
+ }
+ bufferNode = bufferNode.next;
+ }
return null;
}
+ @Override
public V remove(long key) {
- return null;
+ if (node == null) return null;
+ if (node.key == key) {
+ V temp = node.value;
+ node = node.next;
+ size--;
+ return temp;
+ }
+ return searchInNext(key);
}
+ private V searchInNext(long key) {
+ Node bufferNode = node;
+ V temp = node.value;
+ while (bufferNode.next != null) {
+ if (bufferNode.next.key == key) {
+ temp = bufferNode.next.value;
+ bufferNode.next = bufferNode.next.next;
+ size--;
+ return temp;
+ }
+ bufferNode = bufferNode.next;
+ }
+ return temp;
+ }
+
+ @Override
public boolean isEmpty() {
- return false;
+ return size == 0;
}
+ @Override
public boolean containsKey(long key) {
+ Node bufferNode = node;
+ if (node == null) return false;
+ if (node.key == key) return true;
+ while (bufferNode.next != null) {
+ if (bufferNode.next.key == key) {
+ return true;
+ }
+ bufferNode = bufferNode.next;
+ }
return false;
}
+ @Override
public boolean containsValue(V value) {
+ Node bufferNode = node;
+ if (node == null) return false;
+ if (node.value == value) return true;
+ while (bufferNode.next != null) {
+ if (bufferNode.next.value == value) {
+ return true;
+ }
+ bufferNode = bufferNode.next;
+ }
return false;
}
+ @Override
public long[] keys() {
- return null;
+ long[] keys = new long[size];
+ int index = 0;
+ Node bufferNode = node;
+ keys[index] = bufferNode.key;
+ index++;
+ while (bufferNode.next != null) {
+ keys[index] = bufferNode.next.key;
+ index++;
+ bufferNode = bufferNode.next;
+ }
+ return keys;
}
+ @Override
public V[] values() {
- return null;
+ int index = 0;
+ Node bufferNode = node;
+ values[index] = bufferNode.value;
+ index++;
+ while (bufferNode.next != null) {
+ values[index] = bufferNode.next.value;
+ index++;
+ bufferNode = bufferNode.next;
+ }
+ return values;
+ }
+
+ public void setArray(V[] values) {
+ this.values = values;
}
+ @Override
public long size() {
- return 0;
+ return size;
}
+ @Override
public void clear() {
+ if (node == null) return;
+ Node bufferNode = node;
+ while (bufferNode.next != null) {
+ bufferNode.next = bufferNode.next.next;
+ }
+ node = node.next;
+ size = 0;
+ }
+ @Override
+ public String toString() {
+ return "LongMapImpl{"
+ + node +
+ '}';
}
}
diff --git a/src/main/java/de/comparus/opensource/longmap/LongMapImplTest.java b/src/main/java/de/comparus/opensource/longmap/LongMapImplTest.java
new file mode 100644
index 0000000..7752edb
--- /dev/null
+++ b/src/main/java/de/comparus/opensource/longmap/LongMapImplTest.java
@@ -0,0 +1,137 @@
+package de.comparus.opensource.longmap;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Assertions;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+
+public class LongMapImplTest {
+ Render render = new Render();
+
+ @Test
+ public void customTestMap() {
+ LongMapImpl longMap = new LongMapImpl<>();
+ longMap.put(11, "A");
+ longMap.put(22, "B");
+ longMap.put(33, "C");
+ longMap.put(44, "D");
+ longMap.put(55, "E");
+ longMap.put(66, "F");
+ longMap.put(77, "G");
+ System.out.println(longMap);
+ System.out.println(longMap.size());
+ longMap.remove(44);
+ System.out.println(longMap);
+ System.out.println(longMap.size());
+ System.out.println(longMap.isEmpty());
+ System.out.println(longMap.containsKey(77));
+ System.out.println(longMap.containsValue("D"));
+ render.printKeys(longMap);
+ render.printStringValues(longMap);
+ longMap.clear();
+ System.out.println(longMap);
+ System.out.println(longMap.size());
+ }
+
+ @Test
+ public void testPut() {
+ LongMapImpl testMap = new LongMapImpl<>();
+ assertEquals("1", testMap.put(11, "1"));
+ assertEquals("2", testMap.put(22, "2"));
+ assertEquals("3", testMap.put(33, "3"));
+ assertEquals("4", testMap.put(44, "4"));
+ }
+
+ @Test
+ public void testGet() {
+ LongMapImpl testMap = new LongMapImpl<>();
+ assertEquals(testMap.put(11, "1"), testMap.get(11));
+ assertEquals(testMap.put(22, "2"), testMap.get(22));
+ assertEquals(testMap.put(33, "3"), testMap.get(33));
+ assertEquals(testMap.put(44, "4"), testMap.get(44));
+ }
+
+ @Test
+ public void testRemove() {
+ LongMapImpl testMap = new LongMapImpl<>();
+ assertNull(testMap.remove(123));
+ testMap.put(11, "1");
+ testMap.put(22, "2");
+ assertEquals(testMap.put(11, "1"), testMap.remove(11));
+ }
+
+ @Test
+ public void testIsEmpty() {
+ LongMapImpl testMap = new LongMapImpl<>();
+ assertTrue(testMap.isEmpty());
+ testMap.put(11, "1");
+ assertFalse(testMap.isEmpty());
+ }
+
+ @Test
+ public void testContainsKey() {
+ LongMapImpl testMap = new LongMapImpl<>();
+ assertFalse(testMap.containsKey(22));
+ testMap.put(11, "11");
+ assertTrue(testMap.containsKey(11));
+ }
+
+ @Test
+ public void testContainsValue() {
+ LongMapImpl testMap = new LongMapImpl<>();
+ assertFalse(testMap.containsValue("22"));
+ testMap.put(11, "11");
+ assertTrue(testMap.containsValue("11"));
+ }
+
+ @Test
+ public void testKeys() {
+ LongMapImpl testMap = new LongMapImpl<>();
+ long[] exptd = new long[]{11, 22, 33, 44};
+ long[] actl;
+ testMap.put(11, "1");
+ testMap.put(22, "2");
+ testMap.put(33, "3");
+ testMap.put(44, "3");
+ actl = testMap.keys();
+ Assertions.assertArrayEquals(exptd, actl);
+ }
+
+ @Test
+ public void testValues() {
+ Integer[] exptd = new Integer[16];
+ LongMapImpl testMap = new LongMapImpl<>();
+ for (int index = 0; index < 16; index++) {
+ testMap.put(index, index);
+ exptd[index] = index;
+ }
+ testMap.setArray(exptd);
+ Assertions.assertArrayEquals(exptd, testMap.values());
+ }
+
+ @Test
+ public void testSize() {
+ LongMapImpl testMap = new LongMapImpl<>();
+ testMap.put(11, "1");
+ assertEquals(1, testMap.size());
+ testMap.put(22, "2");
+ assertEquals(2, testMap.size());
+ testMap.put(33, "3");
+ assertEquals(3, testMap.size());
+ testMap.put(44, "4");
+ assertEquals(4, testMap.size());
+ }
+
+ @Test
+ public void testClear() {
+ LongMapImpl testMap = new LongMapImpl<>();
+ testMap.put(11, "1");
+ testMap.put(22, "2");
+ assertNotEquals(0, testMap.size());
+ testMap.clear();
+ assertEquals(0, testMap.size());
+ }
+}
diff --git a/src/main/java/de/comparus/opensource/longmap/Render.java b/src/main/java/de/comparus/opensource/longmap/Render.java
new file mode 100644
index 0000000..6cbaf78
--- /dev/null
+++ b/src/main/java/de/comparus/opensource/longmap/Render.java
@@ -0,0 +1,16 @@
+package de.comparus.opensource.longmap;
+
+public class Render {
+ public void printStringValues(LongMapImpl longMap){
+ longMap.setArray(new String[(int)longMap.size()]);
+ for(String line: longMap.values()) {
+ System.out.println(line);
+ }
+ }
+
+ public void printKeys(LongMapImpl longMap){
+ for(long index: longMap.keys()) {
+ System.out.println(index);
+ }
+ }
+}