diff --git a/dispatch/src/main/java/com/tonyodev/dispatch/DispatchObservable.kt b/dispatch/src/main/java/com/tonyodev/dispatch/DispatchObservable.kt deleted file mode 100644 index 08f6c35..0000000 --- a/dispatch/src/main/java/com/tonyodev/dispatch/DispatchObservable.kt +++ /dev/null @@ -1,164 +0,0 @@ -package com.tonyodev.dispatch - -import com.tonyodev.dispatch.thread.ThreadHandler -import com.tonyodev.dispatch.utils.INVALID_RESULT -import com.tonyodev.dispatch.utils.Threader -import com.tonyodev.dispatch.utils.startThreadHandlerIfNotActive - -/** - * Class that allows for the attaching of dispatch observers and publishing results to them. - * */ -class DispatchObservable constructor(private val threadHandler: ThreadHandler?, - private val shouldNotifyOnHandler: Boolean) { - - /** - * Notifies the attached Observers on the passed in threadHandler. - * */ - constructor(threadHandler: ThreadHandler): this(threadHandler, true) - - /** - * Notifies the attached Observers on the main thread. - * */ - constructor(): this(Threader.getHandlerThreadInfo(ThreadType.MAIN).threadHandler, true) - - private val dispatchObserversSet = mutableSetOf>() - private var result: Any? = INVALID_RESULT - - init { - if (shouldNotifyOnHandler && threadHandler != null) { - startThreadHandlerIfNotActive(threadHandler) - } - } - - /** - * Adds a dispatch observer. - * @param dispatchObserver the observer. - * @return the dispatchObservable. - * */ - fun addObserver(dispatchObserver: DispatchObserver): DispatchObservable { - synchronized(dispatchObserversSet) { - dispatchObserversSet.add(dispatchObserver) - } - if (result != INVALID_RESULT) { - if (shouldNotifyOnHandler) { - threadHandler?.post(Runnable { notifyObserver(dispatchObserver) }) - } else { - notifyObserver(dispatchObserver) - } - } - return this - } - - /** - * Adds a list of dispatch observers. - * @param dispatchObservers the list of observers. - * @return the dispatchObservable. - * */ - fun addObservers(dispatchObservers: List>): DispatchObservable { - synchronized(dispatchObserversSet) { - dispatchObserversSet.addAll(dispatchObservers) - } - if (result != INVALID_RESULT) { - if (shouldNotifyOnHandler) { - threadHandler?.post(Runnable { - for (dispatchObserver in dispatchObservers) { - notifyObserver(dispatchObserver) - } - }) - } else { - for (dispatchObserver in dispatchObservers) { - notifyObserver(dispatchObserver) - } - } - } - return this - } - - /** - * Removes a dispatch observer. - * @param dispatchObserver the observer to be removed. - * @return the dispatchObservable. - * */ - fun removeObserver(dispatchObserver: DispatchObserver): DispatchObservable { - synchronized(dispatchObserversSet) { - val iterator = dispatchObserversSet.iterator() - while (iterator.hasNext()) { - if (dispatchObserver == iterator.next()) { - iterator.remove() - break - } - } - } - return this - } - - /** - * Remove a list of dispatch observers. - * @param dispatchObservers the list of observers to be removed. - * @return the dispatchObservable. - * */ - fun removeObservers(dispatchObservers: List>): DispatchObservable { - synchronized(dispatchObserversSet) { - val iterator = dispatchObserversSet.iterator() - var count = 0 - while (iterator.hasNext()) { - if (dispatchObservers.contains(iterator.next())) { - iterator.remove() - ++count - if (count == dispatchObservers.size) { - break - } - } - } - } - return this - } - - /** Removes all observers attached this observable.*/ - fun removeObservers() { - synchronized(dispatchObserversSet) { - val iterator = dispatchObserversSet.iterator() - while (iterator.hasNext()) { - iterator.next() - iterator.remove() - } - } - } - - /** Notifies observers of the passed in result - * @param result the result - * @return the dispatchObservable. - * */ - fun notify(result: R): DispatchObservable { - if (shouldNotifyOnHandler) { - threadHandler?.post(Runnable { notifyObservers(result) }) - } else { - notifyObservers(result) - } - return this - } - - private fun notifyObservers(result: R) { - this.result = result - synchronized(dispatchObserversSet) { - val iterator = dispatchObserversSet.iterator() - while (iterator.hasNext()) { - iterator.next().onChanged(result) - } - } - } - - @Suppress("UNCHECKED_CAST") - private fun notifyObserver(dispatchObserver: DispatchObserver) { - val r = result - if (r != INVALID_RESULT) { - dispatchObserver.onChanged(r as R) - } - } - - /** Get all the observers attached to this observable*/ - fun getObservers(): List> { - return synchronized(dispatchObserversSet) { dispatchObserversSet.toList() } - } - -} \ No newline at end of file diff --git a/dispatch/src/main/java/com/tonyodev/dispatch/DispatchQueue.kt b/dispatch/src/main/java/com/tonyodev/dispatch/DispatchQueue.kt index f434d0d..5684aff 100644 --- a/dispatch/src/main/java/com/tonyodev/dispatch/DispatchQueue.kt +++ b/dispatch/src/main/java/com/tonyodev/dispatch/DispatchQueue.kt @@ -156,41 +156,41 @@ interface DispatchQueue { fun map(func: (R) -> T): DispatchQueue /** - * Gets the backing dispatch queue object DispatchObservable. - * @return DispatchObservable + * Gets the backing dispatch queue object DispatchQueueObservable. + * @return DispatchQueueObservable * */ - fun getDispatchObservable(): DispatchObservable + fun getDispatchQueueObservable(): DispatchQueueObservable /** - * Adds a dispatch observer. - * @param dispatchObserver the observer. + * Adds a dispatch queue observer. + * @param dispatchQueueObserver the observer. * @return the dispatch queue. * */ - fun addObserver(dispatchObserver: DispatchObserver): DispatchQueue + fun addObserver(dispatchQueueObserver: DispatchQueueObserver): DispatchQueue /** - * Adds a list of dispatch observers. - * @param dispatchObservers the list of observers. + * Adds a list of dispatch queue observers. + * @param dispatchQueueObservers the list of observers. * @return the dispatch queue. * */ - fun addObservers(dispatchObservers: List>): DispatchQueue + fun addObservers(dispatchQueueObservers: List>): DispatchQueue /** - * Removes a dispatch observer. - * @param dispatchObserver the observer to be removed. + * Removes a dispatch queue observer. + * @param dispatchQueueObserver the observer to be removed. * @return the dispatch queue. * */ - fun removeObserver(dispatchObserver: DispatchObserver): DispatchQueue + fun removeObserver(dispatchQueueObserver: DispatchQueueObserver): DispatchQueue /** - * Remove a list of dispatch observers. - * @param dispatchObservers the list of observers to be removed. + * Remove a list of dispatch queue observers. + * @param dispatchQueueObservers the list of observers to be removed. * @return the dispatch queue. * */ - fun removeObservers(dispatchObservers: List>): DispatchQueue + fun removeObservers(dispatchQueueObservers: List>): DispatchQueue /** - * Removes all dispatch observers. + * Removes all dispatch queue observers. * @return the dispatch queue. * */ fun removeObservers(): DispatchQueue diff --git a/dispatch/src/main/java/com/tonyodev/dispatch/DispatchQueueObservable.kt b/dispatch/src/main/java/com/tonyodev/dispatch/DispatchQueueObservable.kt new file mode 100644 index 0000000..b068412 --- /dev/null +++ b/dispatch/src/main/java/com/tonyodev/dispatch/DispatchQueueObservable.kt @@ -0,0 +1,164 @@ +package com.tonyodev.dispatch + +import com.tonyodev.dispatch.thread.ThreadHandler +import com.tonyodev.dispatch.utils.INVALID_RESULT +import com.tonyodev.dispatch.utils.Threader +import com.tonyodev.dispatch.utils.startThreadHandlerIfNotActive + +/** + * Class that allows for the attaching of dispatch queue observers and publishing results to them. + * */ +class DispatchQueueObservable constructor(private val threadHandler: ThreadHandler?, + private val shouldNotifyOnHandler: Boolean) { + + /** + * Notifies the attached Observers on the passed in threadHandler. + * */ + constructor(threadHandler: ThreadHandler): this(threadHandler, true) + + /** + * Notifies the attached Observers on the main thread. + * */ + constructor(): this(Threader.getHandlerThreadInfo(ThreadType.MAIN).threadHandler, true) + + private val dispatchQueueObserversSet = mutableSetOf>() + private var result: Any? = INVALID_RESULT + + init { + if (shouldNotifyOnHandler && threadHandler != null) { + startThreadHandlerIfNotActive(threadHandler) + } + } + + /** + * Adds a dispatch queue observer. + * @param dispatchQueueObserver the observer. + * @return the dispatchQueueObservable. + * */ + fun addObserver(dispatchQueueObserver: DispatchQueueObserver): DispatchQueueObservable { + synchronized(dispatchQueueObserversSet) { + dispatchQueueObserversSet.add(dispatchQueueObserver) + } + if (result != INVALID_RESULT) { + if (shouldNotifyOnHandler) { + threadHandler?.post(Runnable { notifyObserver(dispatchQueueObserver) }) + } else { + notifyObserver(dispatchQueueObserver) + } + } + return this + } + + /** + * Adds a list of dispatch queue observers. + * @param dispatchQueueObservers the list of observers. + * @return the dispatchQueueObservable. + * */ + fun addObservers(dispatchQueueObservers: List>): DispatchQueueObservable { + synchronized(dispatchQueueObserversSet) { + dispatchQueueObserversSet.addAll(dispatchQueueObservers) + } + if (result != INVALID_RESULT) { + if (shouldNotifyOnHandler) { + threadHandler?.post(Runnable { + for (dispatchObserver in dispatchQueueObservers) { + notifyObserver(dispatchObserver) + } + }) + } else { + for (dispatchObserver in dispatchQueueObservers) { + notifyObserver(dispatchObserver) + } + } + } + return this + } + + /** + * Removes a dispatch queue observer. + * @param dispatchQueueObserver the observer to be removed. + * @return the dispatchQueueObservable. + * */ + fun removeObserver(dispatchQueueObserver: DispatchQueueObserver): DispatchQueueObservable { + synchronized(dispatchQueueObserversSet) { + val iterator = dispatchQueueObserversSet.iterator() + while (iterator.hasNext()) { + if (dispatchQueueObserver == iterator.next()) { + iterator.remove() + break + } + } + } + return this + } + + /** + * Remove a list of dispatch queue observers. + * @param dispatchQueueObservers the list of observers to be removed. + * @return the dispatchQueueObservable. + * */ + fun removeObservers(dispatchQueueObservers: List>): DispatchQueueObservable { + synchronized(dispatchQueueObserversSet) { + val iterator = dispatchQueueObserversSet.iterator() + var count = 0 + while (iterator.hasNext()) { + if (dispatchQueueObservers.contains(iterator.next())) { + iterator.remove() + ++count + if (count == dispatchQueueObservers.size) { + break + } + } + } + } + return this + } + + /** Removes all observers attached this observable.*/ + fun removeObservers() { + synchronized(dispatchQueueObserversSet) { + val iterator = dispatchQueueObserversSet.iterator() + while (iterator.hasNext()) { + iterator.next() + iterator.remove() + } + } + } + + /** Notifies observers of the passed in result + * @param result the result + * @return the dispatchQueueObservable. + * */ + fun notify(result: R): DispatchQueueObservable { + if (shouldNotifyOnHandler) { + threadHandler?.post(Runnable { notifyObservers(result) }) + } else { + notifyObservers(result) + } + return this + } + + private fun notifyObservers(result: R) { + this.result = result + synchronized(dispatchQueueObserversSet) { + val iterator = dispatchQueueObserversSet.iterator() + while (iterator.hasNext()) { + iterator.next().onChanged(result) + } + } + } + + @Suppress("UNCHECKED_CAST") + private fun notifyObserver(dispatchQueueObserver: DispatchQueueObserver) { + val r = result + if (r != INVALID_RESULT) { + dispatchQueueObserver.onChanged(r as R) + } + } + + /** Get all the observers attached to this observable*/ + fun getObservers(): List> { + return synchronized(dispatchQueueObserversSet) { dispatchQueueObserversSet.toList() } + } + +} \ No newline at end of file diff --git a/dispatch/src/main/java/com/tonyodev/dispatch/DispatchObserver.kt b/dispatch/src/main/java/com/tonyodev/dispatch/DispatchQueueObserver.kt similarity index 61% rename from dispatch/src/main/java/com/tonyodev/dispatch/DispatchObserver.kt rename to dispatch/src/main/java/com/tonyodev/dispatch/DispatchQueueObserver.kt index 2ad2756..9903418 100644 --- a/dispatch/src/main/java/com/tonyodev/dispatch/DispatchObserver.kt +++ b/dispatch/src/main/java/com/tonyodev/dispatch/DispatchQueueObserver.kt @@ -1,16 +1,16 @@ package com.tonyodev.dispatch /** - * DispatchObservers listen for data changes on a dispatch object. + * DispatchQueueObservers listen for data changes on a dispatch queue object. * */ @FunctionalInterface -interface DispatchObserver { +interface DispatchQueueObserver { /** * Method called when the data on the observing object has changed. - * Called on the thread that belongs to the dispatch object. If post + * Called on the thread that belongs to the dispatch queue object. If post * was called the onChanged will be called on the main thread. If async - * was called the onChange is called on the thread that the dispatch object uses. + * was called the onChange is called on the thread that the dispatch queue object uses. * @param data the data. * */ fun onChanged(data: R) diff --git a/dispatch/src/main/java/com/tonyodev/dispatch/internals/DispatchQueueImpl.kt b/dispatch/src/main/java/com/tonyodev/dispatch/internals/DispatchQueueImpl.kt index 7cb9222..2fcf8fa 100644 --- a/dispatch/src/main/java/com/tonyodev/dispatch/internals/DispatchQueueImpl.kt +++ b/dispatch/src/main/java/com/tonyodev/dispatch/internals/DispatchQueueImpl.kt @@ -16,7 +16,7 @@ internal class DispatchQueueImpl(override var blockLabel: String, private val threadHandlerInfo: Threader.ThreadHandlerInfo): DispatchQueue { private val dispatchSources = ArrayList?>(3) - private val dispatchObservable = DispatchObservable(null, false) + private val dispatchQueueObservable = DispatchQueueObservable(null, false) private var doOnErrorWorker: ((throwable: Throwable) -> R)? = null override val id: Int @@ -113,7 +113,7 @@ internal class DispatchQueueImpl(override var blockLabel: String, @Suppress("UNCHECKED_CAST") private fun notifyDispatchObservers() { if (!isCancelled) { - dispatchObservable.notify(results as R) + dispatchQueueObservable.notify(results as R) } } @@ -238,7 +238,7 @@ internal class DispatchQueueImpl(override var blockLabel: String, } iterator.remove() } - dispatchObservable.removeObservers() + dispatchQueueObservable.removeObservers() doOnErrorWorker = null worker = null dispatchQueueInfo.errorHandler = null @@ -385,7 +385,7 @@ internal class DispatchQueueImpl(override var blockLabel: String, threadHandlerInfo = newDispatchQueueInfo.threadHandlerInfo) newDispatchQueue.results = results newDispatchQueue.doOnErrorWorker = doOnErrorWorker - newDispatchQueue.dispatchObservable.addObservers(dispatchObservable.getObservers()) + newDispatchQueue.dispatchQueueObservable.addObservers(dispatchQueueObservable.getObservers()) for (dispatchSource in dispatchSources) { val source = dispatchSource as DispatchQueueImpl<*, *> newDispatchQueue.dispatchSources.add(source.cloneTo(newDispatchQueueInfo = newDispatchQueueInfo)) @@ -397,33 +397,33 @@ internal class DispatchQueueImpl(override var blockLabel: String, return newDispatchQueue } - override fun addObserver(dispatchObserver: DispatchObserver): DispatchQueue { - dispatchObservable.addObserver(dispatchObserver) + override fun addObserver(dispatchQueueObserver: DispatchQueueObserver): DispatchQueue { + dispatchQueueObservable.addObserver(dispatchQueueObserver) return this } - override fun addObservers(dispatchObservers: List>): DispatchQueue { - dispatchObservable.addObservers(dispatchObservers) + override fun addObservers(dispatchQueueObservers: List>): DispatchQueue { + dispatchQueueObservable.addObservers(dispatchQueueObservers) return this } - override fun removeObserver(dispatchObserver: DispatchObserver): DispatchQueue { - dispatchObservable.removeObserver(dispatchObserver) + override fun removeObserver(dispatchQueueObserver: DispatchQueueObserver): DispatchQueue { + dispatchQueueObservable.removeObserver(dispatchQueueObserver) return this } - override fun removeObservers(dispatchObservers: List>): DispatchQueue { - dispatchObservable.removeObservers(dispatchObservers) + override fun removeObservers(dispatchQueueObservers: List>): DispatchQueue { + dispatchQueueObservable.removeObservers(dispatchQueueObservers) return this } override fun removeObservers(): DispatchQueue { - dispatchObservable.removeObservers() + dispatchQueueObservable.removeObservers() return this } - override fun getDispatchObservable(): DispatchObservable { - return dispatchObservable + override fun getDispatchQueueObservable(): DispatchQueueObservable { + return dispatchQueueObservable } override fun setBlockLabel(blockLabel: String): DispatchQueue {