-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched ArrayList with costum Linked List and IdentityHashMap
- Loading branch information
1 parent
cff2279
commit e47fbce
Showing
6 changed files
with
173 additions
and
72 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package="com.gazman.signals" | ||
xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.gazman.signals"> | ||
|
||
<application> | ||
<provider | ||
android:name=".context.ContextProvider" | ||
android:authorities="com.gazman.signals.context.ContextProvider" | ||
android:exported="false" | ||
android:name=".context.ContextProvider" /> | ||
android:exported="false" /> | ||
</application> | ||
</manifest> |
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,67 @@ | ||
package com.gazman.signals | ||
|
||
import java.util.* | ||
|
||
internal class ListenersList<T> : Iterable<T?> { | ||
private val none = Node<T>(null) | ||
private var head: Node<T>? = null | ||
private var tail: Node<T>? = null | ||
private var map = IdentityHashMap<T, Node<T>>() | ||
|
||
fun isNotEmpty(): Boolean { | ||
return map.isNotEmpty() | ||
} | ||
|
||
fun add(listener: T) { | ||
synchronized(this) { | ||
if (map.containsKey(listener)) { | ||
return | ||
} | ||
val node = Node(listener) | ||
map[listener] = node | ||
|
||
if (tail == null) { | ||
head = node | ||
tail = node | ||
} else { | ||
node.previous = tail | ||
tail?.next = node | ||
tail = node | ||
} | ||
} | ||
} | ||
|
||
fun remove(listener: T) { | ||
synchronized(this) { | ||
val node = map[listener] | ||
node?.previous?.next = node?.next | ||
node?.next?.previous = node?.previous | ||
} | ||
} | ||
|
||
override fun iterator(): Iterator<T?> { | ||
return object : Iterator<T?> { | ||
var node: Node<T>? = none | ||
|
||
override fun hasNext() = node != null && node != tail | ||
|
||
override fun next(): T? { | ||
node = if (node == none) { | ||
this@ListenersList.head | ||
} else { | ||
node?.next | ||
} | ||
return node?.value | ||
} | ||
|
||
} | ||
} | ||
|
||
fun clear() { | ||
synchronized(this) { | ||
head = null | ||
tail = null | ||
map.clear() | ||
} | ||
} | ||
} |
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,6 @@ | ||
package com.gazman.signals | ||
|
||
internal class Node<T>(val value: T?) { | ||
var previous: Node<T>? = null | ||
var next: Node<T>? = null | ||
} |
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
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