Daniel Zlotin 7 years ago
parent
commit
f97f063d14

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

1
 package com.reactnativenavigation.utils;
1
 package com.reactnativenavigation.utils;
2
 
2
 
3
 import java.util.ArrayDeque;
3
 import java.util.ArrayDeque;
4
+import java.util.Deque;
4
 import java.util.HashMap;
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
 	private final ArrayDeque<String> deque = new ArrayDeque<>();
11
 	private final ArrayDeque<String> deque = new ArrayDeque<>();
9
 	private final HashMap<String, E> map = new HashMap<>();
12
 	private final HashMap<String, E> map = new HashMap<>();
14
 	}
17
 	}
15
 
18
 
16
 	public E peek() {
19
 	public E peek() {
20
+		if (isEmpty()) {
21
+			return null;
22
+		}
17
 		return map.get(deque.peek());
23
 		return map.get(deque.peek());
18
 	}
24
 	}
19
 
25
 
20
 	public E pop() {
26
 	public E pop() {
21
-		if (deque.isEmpty()) {
27
+		if (isEmpty()) {
22
 			return null;
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
 	public boolean isEmpty() {
33
 	public boolean isEmpty() {
47
 		return map.get(id);
51
 		return map.get(id);
48
 	}
52
 	}
49
 
53
 
50
-	public boolean contains(final String id) {
54
+	public boolean containsId(final String id) {
51
 		return deque.contains(id);
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
 
4
 
5
 import org.junit.Test;
5
 import org.junit.Test;
6
 
6
 
7
+import java.util.Deque;
8
+import java.util.Map;
9
+
7
 import static org.assertj.core.api.Java6Assertions.assertThat;
10
 import static org.assertj.core.api.Java6Assertions.assertThat;
8
 
11
 
9
 public class IndexedStackTest extends BaseTest {
12
 public class IndexedStackTest extends BaseTest {
71
 
74
 
72
 	@Test
75
 	@Test
73
 	public void containsId() throws Exception {
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
 		uut.push("123", 123);
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
 }