Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
148 changes: 142 additions & 6 deletions src/main/java/de/comparus/opensource/longmap/LongMapImpl.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,179 @@
package de.comparus.opensource.longmap;

public class LongMapImpl<V> implements LongMap<V> {
private Node<V> node;
private int size;
private V[] values;

private static class Node<V> {
private long key;
private V value;
private Node<V> 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<V>(key, value);
size++;
return value;
}
writeToNext(key, value);
return value;
}

private Node<V> writeToNext(long key, V value) {
Node<V> bufferNode = node;
while (bufferNode.next != null) {
bufferNode = bufferNode.next;
}
bufferNode.next = new Node<V>(key, value);
size++;
return bufferNode;
}

@Override
public V get(long key) {
Node<V> 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<V> 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<V> 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<V> 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<V> 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<V> 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<V> bufferNode = node;
while (bufferNode.next != null) {
bufferNode.next = bufferNode.next.next;
}
node = node.next;
size = 0;
}

@Override
public String toString() {
return "LongMapImpl{"
+ node +
'}';
}
}
137 changes: 137 additions & 0 deletions src/main/java/de/comparus/opensource/longmap/LongMapImplTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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<String> 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<String> testMap = new LongMapImpl<>();
assertTrue(testMap.isEmpty());
testMap.put(11, "1");
assertFalse(testMap.isEmpty());
}

@Test
public void testContainsKey() {
LongMapImpl<String> testMap = new LongMapImpl<>();
assertFalse(testMap.containsKey(22));
testMap.put(11, "11");
assertTrue(testMap.containsKey(11));
}

@Test
public void testContainsValue() {
LongMapImpl<String> testMap = new LongMapImpl<>();
assertFalse(testMap.containsValue("22"));
testMap.put(11, "11");
assertTrue(testMap.containsValue("11"));
}

@Test
public void testKeys() {
LongMapImpl<String> 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<Integer> 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<String> 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<String> testMap = new LongMapImpl<>();
testMap.put(11, "1");
testMap.put(22, "2");
assertNotEquals(0, testMap.size());
testMap.clear();
assertEquals(0, testMap.size());
}
}
16 changes: 16 additions & 0 deletions src/main/java/de/comparus/opensource/longmap/Render.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.comparus.opensource.longmap;

public class Render {
public void printStringValues(LongMapImpl<String> longMap){
longMap.setArray(new String[(int)longMap.size()]);
for(String line: longMap.values()) {
System.out.println(line);
}
}

public void printKeys(LongMapImpl<String> longMap){
for(long index: longMap.keys()) {
System.out.println(index);
}
}
}