-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support backstack read operations from a background thread
- Loading branch information
Showing
3 changed files
with
404 additions
and
1 deletion.
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
magellan-library/src/main/java/com/wealthfront/magellan/navigation/CopyOnWriteArrayDeque.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package com.wealthfront.magellan.navigation | ||
|
||
import java.util.Deque | ||
import java.util.concurrent.CopyOnWriteArrayList | ||
|
||
internal class CopyOnWriteArrayDeque<E> : Deque<E> { | ||
|
||
private val list = CopyOnWriteArrayList<E>() | ||
|
||
override fun add(element: E): Boolean { | ||
return list.add(element) | ||
} | ||
|
||
override fun addAll(elements: Collection<E>): Boolean { | ||
return list.addAll(elements) | ||
} | ||
|
||
override fun clear() { | ||
list.clear() | ||
} | ||
|
||
override fun iterator(): MutableIterator<E> { | ||
return list.iterator() | ||
} | ||
|
||
override fun remove(): E { | ||
return list.removeFirst() | ||
} | ||
|
||
override fun remove(element: E?): Boolean { | ||
return list.remove(element) | ||
} | ||
|
||
override fun removeAll(elements: Collection<E>): Boolean { | ||
return list.removeAll(elements.toSet()) | ||
} | ||
|
||
override fun retainAll(elements: Collection<E>): Boolean { | ||
return list.retainAll(elements.toSet()) | ||
} | ||
|
||
override fun contains(element: E?): Boolean { | ||
return list.contains(element) | ||
} | ||
|
||
override fun containsAll(elements: Collection<E>): Boolean { | ||
return list.containsAll(elements) | ||
} | ||
|
||
override fun isEmpty(): Boolean { | ||
return list.isEmpty() | ||
} | ||
|
||
override fun offer(e: E): Boolean { | ||
return list.add(e) | ||
} | ||
|
||
override fun poll(): E? { | ||
return list.removeFirstOrNull() | ||
} | ||
|
||
override fun element(): E { | ||
return list.first() | ||
} | ||
|
||
override fun peek(): E? { | ||
return list.firstOrNull() | ||
} | ||
|
||
override fun addFirst(e: E) { | ||
list.add(0, e) | ||
} | ||
|
||
override fun addLast(e: E) { | ||
add(e) | ||
} | ||
|
||
override fun offerFirst(e: E): Boolean { | ||
list.add(0, e) | ||
return true | ||
} | ||
|
||
override fun offerLast(e: E): Boolean { | ||
return list.add(e) | ||
} | ||
|
||
override fun removeFirst(): E { | ||
return list.removeFirst() | ||
} | ||
|
||
override fun removeLast(): E { | ||
return list.removeLast() | ||
} | ||
|
||
override fun pollFirst(): E? { | ||
return list.removeFirstOrNull() | ||
} | ||
|
||
override fun pollLast(): E? { | ||
return list.removeLastOrNull() | ||
} | ||
|
||
override fun getFirst(): E { | ||
return list.first() | ||
} | ||
|
||
override fun getLast(): E { | ||
return list.last() | ||
} | ||
|
||
override fun peekFirst(): E? { | ||
return list.firstOrNull() | ||
} | ||
|
||
override fun peekLast(): E? { | ||
return list.lastOrNull() | ||
} | ||
|
||
override fun removeFirstOccurrence(o: Any?): Boolean { | ||
return list.remove(o) | ||
} | ||
|
||
override fun removeLastOccurrence(o: Any?): Boolean { | ||
val lastIndex = list.lastIndexOf(o) | ||
return if (lastIndex >= 0) { | ||
list.removeAt(lastIndex) | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
override fun push(e: E) { | ||
addFirst(e) | ||
} | ||
|
||
override fun pop(): E { | ||
return removeFirst() | ||
} | ||
|
||
override fun descendingIterator(): MutableIterator<E> { | ||
throw NotImplementedError("descendingIterator() is not supported.") | ||
} | ||
|
||
override val size: Int | ||
get() = list.size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.