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.

81 lines
2.2 KiB
Kotlin

package net.cijber.pubgrub.version
import net.cijber.pubgrub.stubs.Version
data class VersionBorder<V : Version<V>>(val version: V, val inclusive: Boolean, val location: Location) :
Comparable<VersionBorder<V>> {
enum class Location {
Top,
Bottom;
}
fun above(otherVersion: V, inside: Boolean = true): Boolean =
when (otherVersion.compareTo(version)) {
-1 -> true
0 -> inside == inclusive
else -> false
}
fun beneath(otherVersion: V, inside: Boolean = true): Boolean =
when (otherVersion.compareTo(version)) {
1 -> true
0 -> inside == inclusive
else -> false
}
override operator fun compareTo(other: VersionBorder<V>): Int {
val diff = version.compareTo(other.version)
if (diff == 0) {
if (other.inclusive == inclusive) {
return 0
}
if (inclusive) {
if (location == Location.Top) {
return 1
}
return -1
}
if (location == Location.Bottom) {
return -1
}
return 1
}
return diff
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as VersionBorder<*>
if (version != other.version) return false
if (inclusive != other.inclusive) return false
return true
}
override fun hashCode(): Int {
var result = version.hashCode()
result = 31 * result + inclusive.hashCode()
return result
}
fun reversed(): VersionBorder<V> {
return VersionBorder(version, !inclusive, if (location == Location.Top) Location.Bottom else Location.Top)
}
companion object {
fun <V : Version<V>> top(version: V, inclusive: Boolean = true) =
VersionBorder(version, inclusive, Location.Top)
fun <V : Version<V>> bottom(version: V, inclusive: Boolean = true) =
VersionBorder(version, inclusive, Location.Bottom)
}
}