package net.cijber.pubgrub.version import net.cijber.pubgrub.stubs.Version data class VersionBorder>(val version: V, val inclusive: Boolean, val location: Location) : Comparable> { 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): 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 { return VersionBorder(version, !inclusive, if (location == Location.Top) Location.Bottom else Location.Top) } companion object { fun > top(version: V, inclusive: Boolean = true) = VersionBorder(version, inclusive, Location.Top) fun > bottom(version: V, inclusive: Boolean = true) = VersionBorder(version, inclusive, Location.Bottom) } }