Use abstract readonly collections as base classes for implementing collection properties of regex MatchResult

This commit is contained in:
Ilya Gorbunov
2016-10-13 22:56:40 +03:00
parent 703ee6bd78
commit e6887ea4e5
2 changed files with 4 additions and 11 deletions
+1 -7
View File
@@ -197,15 +197,9 @@ private fun RegExp.findNext(input: String, from: Int): MatchResult? {
override val value: String
get() = match[0]!!
override val groups: MatchGroupCollection = object : MatchGroupCollection {
override val groups: MatchGroupCollection = object : MatchGroupCollection, AbstractCollection<MatchGroup?>() {
override val size: Int get() = match.length
override fun isEmpty(): Boolean = size == 0
override fun contains(element: MatchGroup?): Boolean = this.any { it == element }
override fun containsAll(elements: Collection<MatchGroup?>): Boolean = elements.all { contains(it) }
override fun iterator(): Iterator<MatchGroup?> = indices.asSequence().map { this[it] }.iterator()
override fun get(index: Int): MatchGroup? = match[index]?.let { MatchGroup(it) }
}
@@ -16,7 +16,8 @@
@file:JvmVersion
package kotlin.text
import java.util.*
import java.util.Collections
import java.util.EnumSet
import java.util.regex.Matcher
import java.util.regex.Pattern
import kotlin.internal.IMPLEMENTATIONS
@@ -232,11 +233,9 @@ private class MatcherMatchResult(private val matcher: Matcher, private val input
override val value: String
get() = matchResult.group()
override val groups: MatchGroupCollection = object : MatchNamedGroupCollection {
override val groups: MatchGroupCollection = object : MatchNamedGroupCollection, AbstractCollection<MatchGroup?>() {
override val size: Int get() = matchResult.groupCount() + 1
override fun isEmpty(): Boolean = false
override fun contains(element: MatchGroup?): Boolean = this.any { it == element }
override fun containsAll(elements: Collection<MatchGroup?>): Boolean = elements.all { contains(it) }
override fun iterator(): Iterator<MatchGroup?> = indices.asSequence().map { this[it] }.iterator()
override fun get(index: Int): MatchGroup? {