You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.3 KiB
Kotlin
91 lines
2.3 KiB
Kotlin
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<V : Version<V>>(val constraints: List<VersionRange<V>>) : VersionConstraint<V> {
|
|
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<V>): Boolean {
|
|
return constraints.all {
|
|
it.allowsAll(rhs)
|
|
}
|
|
}
|
|
|
|
override fun allowsAny(rhs: VersionConstraint<V>): Boolean {
|
|
return constraints.any {
|
|
it.allowsAny(rhs)
|
|
}
|
|
}
|
|
|
|
override fun intersect(rhs: VersionConstraint<V>): VersionConstraint<V> {
|
|
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<V>): VersionConstraint<V> {
|
|
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<V>): VersionConstraint<V> {
|
|
if (rhs !is VersionUnion) {
|
|
return rhs.union(this)
|
|
}
|
|
|
|
return VersionUnion(rhs.constraints + this.constraints)
|
|
}
|
|
} |