https://kotlinlang.org logo
Title
s

snowe

05/21/2019, 2:55 PM
Is there an equivalent in kotlin to Groovy's
StringGroovyMethods#getAt(Matcher matcher, int idx)
method?
1
h

hho

05/21/2019, 4:12 PM
Not directly. Can you maybe elaborate on what you're trying to achieve?
s

snowe

05/21/2019, 4:44 PM
I use the net.researchgate gradle-release plugin. I'm trying to add it's configuration to my company plugin. My plugin is written in kotlin.
release {
    failOnPublishNeeded = true
    failOnSnapshotDependencies = true
    failOnCommitNeeded = true
    useAutomaticVersion = true

    git {
        requireBranch = 'develop'
        pushToRemote = 'origin'
        pushToBranchPrefix = 'release/'
    }
    
    // Using `useAutomaticVersion` this increments the release to the next 'minor' version.
    versionPatterns = [
            /(\d+)\.(\d+)\.(\d+)([^\d]*$)/: { Matcher m, Project p -> 
                m.replaceAll("${m[0][1]}.${(m[0][2] as int) + 1}.${m[0][3]}${m[0][4]}") 
            }
    ]
}
needs to be converted to kotlin.
currently I have
val release = extensions.findByType<ReleaseExtension>()
        release?.failOnPublishNeeded = true
        release?.failOnSnapshotDependencies = true
        release?.failOnCommitNeeded = true
        release?.useAutomaticVersion = true
        release?.closureOf<GitAdapter.GitConfig> { 
            this.requireBranch = 'develop'
            this.pushToRemote = 'origin'
            this.pushToBranchPrefix = 'release/'
        }
//        release?.versionPatterns = mapOf("(\\d+)\\.(\\d+)\\.(\\d+)([^\\d]*\$)" to closureOf<String>{ m: Matcher, p: Project -> m.replaceAll("${m[0][1]}.${(m[0][2] as int) + 1}.${m[0][3]}${m[0][4]}") })
        release?.versionPatterns = mapOf("(\\d+)\\.(\\d+)\\.(\\d+)([^\\d]*\$)" to KotlinClosure2({ m: Matcher, p: Project -> m.replaceAll("${m[0][1]}") }))
And I've added the following functions to allow it to partially work.
fun Matcher.get(idx: Int): Any? {
        var idx = idx
        try {
            val count = getCount(this)
            if (idx >= -count && idx < count) {
                idx = normaliseIndex(idx, count)
                val iter = iterator(this)
                var result: Any? = null

                for (i in 0..idx) {
                    result = iter.next()
                }

                return result
            } else {
                throw IndexOutOfBoundsException("index is out of range " + -count + ".." + (count - 1) + " (index = " + idx + ")")
            }
        } catch (var6: IllegalStateException) {
            return null
        }

    }

    private fun normaliseIndex(i: Int, size: Int): Int {
        var i = i
        val temp = i
        if (i < 0) {
            i += size
        }

        return if (i < 0) {
            throw ArrayIndexOutOfBoundsException("Negative array index [$temp] too large for array size $size")
        } else {
            i
        }
    }
    private fun getCount(matcher: Matcher): Int {
        var counter = 0
        matcher.reset()

        while (matcher.find()) {
            ++counter
        }

        return counter
    }
    fun iterator(matcher: Matcher): Iterator<*> {
        matcher.reset()
        return object : Iterator<Any> {
            private var found: Boolean = false
            private var done: Boolean = false

            override operator fun hasNext(): Boolean {
                if (this.done) {
                    return false
                } else {
                    if (!this.found) {
                        this.found = matcher.find()
                        if (!this.found) {
                            this.done = true
                        }
                    }

                    return this.found
                }
            }

            override operator fun next(): Any {
                if (!this.found && !this.hasNext()) {
                    throw NoSuchElementException()
                } else {
                    this.found = false
                    if (!StringGroovyMethods.hasGroup(matcher)) {
                        return matcher.group()
                    } else {
                        val list = ArrayList(matcher.groupCount())

                        for (i in 0..matcher.groupCount()) {
                            list.add(matcher.group(i))
                        }

                        return list
                    }
                }
            }
        }
    }
but now I need to get the second
[]
working, but that's actually a second call...
DefaultGroovyMethods#getAt
ughhhh
figured out a different way to do what I wanted.