2018-01-04 19:52:25 +01:00
|
|
|
|
package jp.juggler.subwaytooter.util
|
|
|
|
|
|
|
|
|
|
import java.math.BigInteger
|
|
|
|
|
import java.util.ArrayList
|
2018-05-14 16:41:13 +02:00
|
|
|
|
import java.util.regex.Pattern
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2018-05-14 16:41:13 +02:00
|
|
|
|
class VersionString(src : String?) : Comparable<VersionString> {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
private val src : String
|
|
|
|
|
|
|
|
|
|
private val node_list = ArrayList<Any>()
|
|
|
|
|
|
|
|
|
|
val isEmpty : Boolean
|
|
|
|
|
get() = node_list.isEmpty()
|
|
|
|
|
|
|
|
|
|
override fun toString() : String {
|
|
|
|
|
return src
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 16:41:13 +02:00
|
|
|
|
private class RC(val x : Int) : Comparable<RC> {
|
|
|
|
|
override fun compareTo(other : RC) : Int {
|
|
|
|
|
val i = x - other.x
|
|
|
|
|
return if(i > 0) 1 else if(i < 0) - 1 else 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun toString() : String {
|
|
|
|
|
return "RC($x)"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun compareTo(other : VersionString) : Int {
|
|
|
|
|
return compare(this, other)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
init {
|
|
|
|
|
this.src = src ?: ""
|
2018-12-08 07:26:19 +01:00
|
|
|
|
if(src?.isNotEmpty() == true) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
val end = src.length
|
|
|
|
|
var next = 0
|
|
|
|
|
while(next < end) {
|
|
|
|
|
var c = src[next]
|
|
|
|
|
|
2018-05-14 16:41:13 +02:00
|
|
|
|
when {
|
|
|
|
|
isDelimiter(c) -> {
|
|
|
|
|
// 先頭の区切り文字を無視する
|
2018-01-04 19:52:25 +01:00
|
|
|
|
++ next
|
|
|
|
|
}
|
2018-05-14 16:41:13 +02:00
|
|
|
|
|
|
|
|
|
Character.isDigit(c) -> {
|
|
|
|
|
// 数字列のノード
|
|
|
|
|
val start = next ++
|
|
|
|
|
while(next < end && Character.isDigit(src[next])) ++ next
|
|
|
|
|
val value = BigInteger(src.substring(start, next))
|
|
|
|
|
node_list.add(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else -> {
|
|
|
|
|
val m = reRcX.matcher(src)
|
|
|
|
|
|
|
|
|
|
if(DUMP) {
|
|
|
|
|
if(m.find(next)) {
|
|
|
|
|
println("next=$next, matct_start=${m.start()}")
|
|
|
|
|
} else {
|
|
|
|
|
println("next=$next, not match.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(m.find(next) && m.start() == next) {
|
|
|
|
|
// RCノード
|
|
|
|
|
next = m.end()
|
|
|
|
|
val numStr = m.group(1)
|
|
|
|
|
val num = if(numStr?.isNotEmpty() == true) {
|
|
|
|
|
numStr.toInt()
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
node_list.add(RC(num))
|
|
|
|
|
} else {
|
|
|
|
|
// 区切り文字と数字以外の文字が並ぶノード
|
|
|
|
|
val start = next ++
|
|
|
|
|
while(next < end) {
|
|
|
|
|
c = src[next]
|
|
|
|
|
if(isDelimiter(c)) break
|
|
|
|
|
if(Character.isDigit(c)) break
|
|
|
|
|
++ next
|
|
|
|
|
}
|
|
|
|
|
val value = src.substring(start, next)
|
|
|
|
|
node_list.add(value)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 16:41:13 +02:00
|
|
|
|
companion object : Comparator<VersionString> {
|
|
|
|
|
|
|
|
|
|
private const val DUMP = false
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
private fun isDelimiter(c : Char) : Boolean {
|
|
|
|
|
return c == '.' || c == ' '
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 16:41:13 +02:00
|
|
|
|
private val reRcX = Pattern.compile("rc(\\d*)")
|
|
|
|
|
|
|
|
|
|
private fun checkTail(b : Any) : Int {
|
|
|
|
|
// 1.0 < 1.0.n => -1
|
|
|
|
|
// 1.0 < 1.0xxx => -1
|
|
|
|
|
// 1.0 > 1.0rc1 => 1
|
|
|
|
|
return if(b is RC) 1 else - 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun checkBigInteger(a : BigInteger, b : Any) : Int {
|
|
|
|
|
if(b is BigInteger) return a.compareTo(b)
|
|
|
|
|
// 数字 > 数字以外
|
|
|
|
|
// 1.5.n > 1.5xxx
|
|
|
|
|
// 1.5.n > 1.5rc1
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun checkRc(a : RC, b : Any) : Int {
|
|
|
|
|
if(b is RC) return a.compareTo(b)
|
|
|
|
|
// RC < string
|
|
|
|
|
// 1.5rc < 1.5xxx
|
|
|
|
|
return - 1
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
// return -1 if a<b , return 1 if a>b , return 0 if a==b
|
2018-05-14 16:41:13 +02:00
|
|
|
|
override fun compare(a : VersionString, b : VersionString) : Int {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
var idx = 0
|
2018-05-14 16:41:13 +02:00
|
|
|
|
loop@ while(true) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
val ao = if(idx >= a.node_list.size) null else a.node_list[idx]
|
|
|
|
|
val bo = if(idx >= b.node_list.size) null else b.node_list[idx]
|
2018-05-14 16:41:13 +02:00
|
|
|
|
if(DUMP) println("a=$ao,b=$bo")
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2018-05-14 16:41:13 +02:00
|
|
|
|
val i = when {
|
|
|
|
|
ao == null -> {
|
|
|
|
|
if(bo == null) return 0
|
|
|
|
|
checkTail(bo)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
2018-05-14 16:41:13 +02:00
|
|
|
|
|
|
|
|
|
bo == null -> - checkTail(ao)
|
|
|
|
|
ao is BigInteger -> checkBigInteger(ao, bo)
|
|
|
|
|
bo is BigInteger -> - checkBigInteger(bo, ao)
|
|
|
|
|
ao is RC -> checkRc(ao, bo)
|
|
|
|
|
bo is RC -> - checkRc(bo, ao)
|
|
|
|
|
else -> a.toString().compareTo(b.toString())
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
2018-05-14 16:41:13 +02:00
|
|
|
|
if(i == 0) {
|
|
|
|
|
++ idx
|
|
|
|
|
continue@loop
|
|
|
|
|
}
|
|
|
|
|
return i
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|