-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/kotlin/me/xx2bab/scratchpaper/utils/AGPVersion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package me.xx2bab.scratchpaper.utils | ||
|
||
|
||
class AGPVersion(var version: String) : Comparable<AGPVersion> { | ||
|
||
init { | ||
// Any alpha/beta/rc version we deem it as the formal one | ||
if (version.contains("-")) { | ||
val indexOfDash = version.indexOf("-") | ||
version = version.substring(0, indexOfDash) | ||
} | ||
// should only include | ||
if (!version.matches("[0-9]+(\\.[0-9]+)*".toRegex())) { | ||
throw IllegalArgumentException("Invalid version format") | ||
} | ||
} | ||
|
||
override fun compareTo(other: AGPVersion): Int { | ||
val thisParts = this.get().split("\\.".toRegex()) | ||
val thatParts = other.get().split("\\.".toRegex()) | ||
val length = Math.max(thisParts.size, thatParts.size) | ||
for (i in 0 until length) { | ||
val thisPart = if (i < thisParts.size) | ||
Integer.parseInt(thisParts[i]) | ||
else | ||
0 | ||
val thatPart = if (i < thatParts.size) | ||
Integer.parseInt(thatParts[i]) | ||
else | ||
0 | ||
if (thisPart < thatPart) { | ||
return -1 | ||
} | ||
if (thisPart > thatPart) { | ||
return 1 | ||
} | ||
} | ||
return 0 | ||
} | ||
|
||
fun get(): String { | ||
return version | ||
} | ||
|
||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as AGPVersion | ||
|
||
if (this != other) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return version.hashCode() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters