All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ru.casperix.signals.collection.ObservableMutableSet.kt Maven / Gradle / Ivy

package ru.casperix.signals.collection

import ru.casperix.signals.concrete.Future
import ru.casperix.signals.concrete.Signal

class ObservableMutableSet : MutableSet, ObservableCollection {
	private val items = mutableSetOf()

	private val addSignal = Signal()
	private val removeSignal = Signal()

	override fun addFuture(): Future {
		return addSignal
	}

	override fun removeFuture(): Future {
		return removeSignal
	}

	override fun add(element: Item): Boolean {
		if (items.add(element)) {
			addSignal.set(element)
			return true
		}
		return false
	}

	override fun remove(element: Item): Boolean {
		if (items.remove(element)) {
			removeSignal.set(element)
			return true
		}
		return false
	}

	override fun addAll(elements: Collection): Boolean {
		var result = false
		for (element in elements) {
			val next = add(element)
			result = result || next
		}
		return result
	}

	override fun clear() {
		removeAll(items.toList())
	}

	override fun iterator(): MutableIterator {
		return items.iterator()
	}

	override fun removeAll(elements: Collection): Boolean {
		var result = false
		for (element in elements) {
			val next = remove(element)
			result = result || next
		}
		return result
	}

	override fun retainAll(elements: Collection): Boolean {
		throw Error("No implementation")
	}

	override val size: Int
		get() = items.size

	override fun contains(element: Item): Boolean {
		return items.contains(element)
	}

	override fun containsAll(elements: Collection): Boolean {
		return items.containsAll(elements)
	}

	override fun isEmpty(): Boolean {
		return items.isEmpty()
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy