package net.cijber.pubgrub.version import net.cijber.pubgrub.stubs.Version /// /// TODO: could be optimized to be sorted by version, which would make the function O((N+M)-1) /// class VersionUnion>(val constraints: List>) : VersionConstraint { override val isEmpty: Boolean get() = constraints.isEmpty() override val isAny: Boolean get() = false override fun allows(version: V): Boolean { return constraints.any { it.allows(version) } } override fun allowsAll(rhs: VersionConstraint): Boolean { return constraints.all { it.allowsAll(rhs) } } override fun allowsAny(rhs: VersionConstraint): Boolean { return constraints.any { it.allowsAny(rhs) } } override fun intersect(rhs: VersionConstraint): VersionConstraint { if (rhs.isAny) { return this } if (rhs.isEmpty) { return rhs } val found = constraints .flatMap { when (val diff = it.intersect(rhs)) { is VersionRange -> listOf(diff) is VersionUnion -> diff.constraints else -> listOf() } } return when (found.count()) { 0 -> VersionEmpty() 1 -> found.first() else -> VersionUnion(found) } } override fun difference(rhs: VersionConstraint): VersionConstraint { if (rhs.isAny) { return VersionEmpty() } if (rhs.isEmpty) { return this } val found = constraints .flatMap { when (val diff = it.difference(rhs)) { is VersionRange -> listOf(diff) is VersionUnion -> diff.constraints else -> listOf() } } return when (found.count()) { 0 -> VersionEmpty() 1 -> found.first() else -> VersionUnion(found) } } override fun union(rhs: VersionConstraint): VersionConstraint { if (rhs !is VersionUnion) { return rhs.union(this) } return VersionUnion(rhs.constraints + this.constraints) } }