Daniel Zlotin 7 年之前
父節點
當前提交
6a25f0ad10

+ 53
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/IdStack.java 查看文件

@@ -0,0 +1,53 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import java.util.ArrayDeque;
4
+import java.util.HashMap;
5
+
6
+public class IdStack<E> {
7
+
8
+	private final ArrayDeque<String> deque = new ArrayDeque<>();
9
+	private final HashMap<String, E> map = new HashMap<>();
10
+
11
+	public void push(String id, E item) {
12
+		deque.push(id);
13
+		map.put(id, item);
14
+	}
15
+
16
+	public E peek() {
17
+		return map.get(deque.peek());
18
+	}
19
+
20
+	public E pop() {
21
+		if (deque.isEmpty()) {
22
+			return null;
23
+		}
24
+		String popped = deque.pop();
25
+		E removed = map.remove(popped);
26
+		return removed;
27
+	}
28
+
29
+	public boolean isEmpty() {
30
+		return deque.isEmpty();
31
+	}
32
+
33
+	public int size() {
34
+		return deque.size();
35
+	}
36
+
37
+	public String peekId() {
38
+		return deque.peek();
39
+	}
40
+
41
+	public void clear() {
42
+		deque.clear();
43
+		map.clear();
44
+	}
45
+
46
+	public E get(final String id) {
47
+		return map.get(id);
48
+	}
49
+
50
+	public boolean contains(final String id) {
51
+		return deque.contains(id);
52
+	}
53
+}

+ 0
- 36
lib/android/app/src/main/java/com/reactnativenavigation/utils/IndexedStack.java 查看文件

@@ -1,36 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-import java.util.ArrayDeque;
4
-import java.util.HashMap;
5
-
6
-public class IndexedStack<K, E> {
7
-
8
-	private final ArrayDeque<K> deque = new ArrayDeque<>();
9
-	private final HashMap<K, E> map = new HashMap<>();
10
-
11
-	public void push(K id, E item) {
12
-		deque.push(id);
13
-		map.put(id, item);
14
-	}
15
-
16
-	public E peek() {
17
-		return map.get(deque.peek());
18
-	}
19
-
20
-	public E pop() {
21
-		if (deque.isEmpty()) {
22
-			return null;
23
-		}
24
-		K popped = deque.pop();
25
-		E removed = map.remove(popped);
26
-		return removed;
27
-	}
28
-
29
-	public boolean isEmpty() {
30
-		return deque.isEmpty();
31
-	}
32
-
33
-	public int size() {
34
-		return deque.size();
35
-	}
36
-}

+ 33
- 2
lib/android/app/src/test/java/com/reactnativenavigation/utils/IndexedStackTest.java 查看文件

@@ -8,12 +8,12 @@ import static org.assertj.core.api.Java6Assertions.assertThat;
8 8
 
9 9
 public class IndexedStackTest extends BaseTest {
10 10
 
11
-	private IndexedStack<String, Integer> uut;
11
+	private IdStack<Integer> uut;
12 12
 
13 13
 	@Override
14 14
 	public void beforeEach() {
15 15
 		super.beforeEach();
16
-		uut = new IndexedStack<>();
16
+		uut = new IdStack<>();
17 17
 	}
18 18
 
19 19
 	@Test
@@ -45,4 +45,35 @@ public class IndexedStackTest extends BaseTest {
45 45
 		uut.push("456", 456);
46 46
 		assertThat(uut.pop()).isEqualTo(456);
47 47
 	}
48
+
49
+	@Test
50
+	public void peekId() throws Exception {
51
+		assertThat(uut.peekId()).isNull();
52
+		uut.push("123", 123);
53
+		assertThat(uut.peekId()).isEqualTo("123");
54
+	}
55
+
56
+	@Test
57
+	public void clear() throws Exception {
58
+		uut.push("123", 123);
59
+		uut.push("456", 456);
60
+		uut.clear();
61
+		assertThat(uut.isEmpty()).isTrue();
62
+	}
63
+
64
+	@Test
65
+	public void getById() throws Exception {
66
+		assertThat(uut.get("123")).isNull();
67
+		uut.push("123", 123);
68
+		uut.push("456", 456);
69
+		assertThat(uut.get("123")).isEqualTo(123);
70
+	}
71
+
72
+	@Test
73
+	public void containsId() throws Exception {
74
+		assertThat(uut.contains("123")).isFalse();
75
+		uut.push("123", 123);
76
+		assertThat(uut.contains("123")).isTrue();
77
+
78
+	}
48 79
 }