Daniel Zlotin 7 years ago
parent
commit
f97f063d14

+ 31
- 6
lib/android/app/src/main/java/com/reactnativenavigation/utils/IdStack.java View File

@@ -1,9 +1,12 @@
1 1
 package com.reactnativenavigation.utils;
2 2
 
3 3
 import java.util.ArrayDeque;
4
+import java.util.Deque;
4 5
 import java.util.HashMap;
6
+import java.util.Iterator;
7
+import java.util.Map;
5 8
 
6
-public class IdStack<E> {
9
+public class IdStack<E> implements Iterable<String> {
7 10
 
8 11
 	private final ArrayDeque<String> deque = new ArrayDeque<>();
9 12
 	private final HashMap<String, E> map = new HashMap<>();
@@ -14,16 +17,17 @@ public class IdStack<E> {
14 17
 	}
15 18
 
16 19
 	public E peek() {
20
+		if (isEmpty()) {
21
+			return null;
22
+		}
17 23
 		return map.get(deque.peek());
18 24
 	}
19 25
 
20 26
 	public E pop() {
21
-		if (deque.isEmpty()) {
27
+		if (isEmpty()) {
22 28
 			return null;
23 29
 		}
24
-		String popped = deque.pop();
25
-		E removed = map.remove(popped);
26
-		return removed;
30
+		return map.remove(deque.pop());
27 31
 	}
28 32
 
29 33
 	public boolean isEmpty() {
@@ -47,7 +51,28 @@ public class IdStack<E> {
47 51
 		return map.get(id);
48 52
 	}
49 53
 
50
-	public boolean contains(final String id) {
54
+	public boolean containsId(final String id) {
51 55
 		return deque.contains(id);
52 56
 	}
57
+
58
+	public E remove(final String id) {
59
+		if (!containsId(id)) {
60
+			return null;
61
+		}
62
+		deque.remove(id);
63
+		return map.remove(id);
64
+	}
65
+
66
+	@Override
67
+	public Iterator<String> iterator() {
68
+		return deque.iterator();
69
+	}
70
+
71
+	public Deque<String> getIds() {
72
+		return deque;
73
+	}
74
+
75
+	public Map<String, E> getMap() {
76
+		return map;
77
+	}
53 78
 }

+ 33
- 2
lib/android/app/src/test/java/com/reactnativenavigation/utils/IndexedStackTest.java View File

@@ -4,6 +4,9 @@ import com.reactnativenavigation.BaseTest;
4 4
 
5 5
 import org.junit.Test;
6 6
 
7
+import java.util.Deque;
8
+import java.util.Map;
9
+
7 10
 import static org.assertj.core.api.Java6Assertions.assertThat;
8 11
 
9 12
 public class IndexedStackTest extends BaseTest {
@@ -71,9 +74,37 @@ public class IndexedStackTest extends BaseTest {
71 74
 
72 75
 	@Test
73 76
 	public void containsId() throws Exception {
74
-		assertThat(uut.contains("123")).isFalse();
77
+		assertThat(uut.containsId("123")).isFalse();
78
+		uut.push("123", 123);
79
+		assertThat(uut.containsId("123")).isTrue();
80
+	}
81
+
82
+	@Test
83
+	public void remove() throws Exception {
84
+		assertThat(uut.remove("123")).isNull();
85
+
86
+		uut.push("123", 123);
87
+		uut.push("456", 456);
88
+
89
+		assertThat(uut.remove("123")).isEqualTo(123);
90
+	}
91
+
92
+	@Test
93
+	public void iterableIds() throws Exception {
94
+		assertThat(uut).isInstanceOf(Iterable.class);
95
+		assertThat(uut).isEmpty();
75 96
 		uut.push("123", 123);
76
-		assertThat(uut.contains("123")).isTrue();
97
+		uut.push("456", 456);
98
+		assertThat(uut).containsExactly("456", "123");
99
+	}
77 100
 
101
+	@Test
102
+	public void getIdDeque() throws Exception {
103
+		assertThat(uut.getIds()).isNotNull().isInstanceOf(Deque.class).isEmpty();
104
+	}
105
+
106
+	@Test
107
+	public void getMap() throws Exception {
108
+		assertThat(uut.getMap()).isNotNull().isInstanceOf(Map.class).isEmpty();
78 109
 	}
79 110
 }