working filterIsInstance() helper method for filtering on a sub type
This commit is contained in:
@@ -5,14 +5,14 @@ import kotlin.support.AbstractIterator
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.Iterator
|
import java.util.Iterator
|
||||||
|
|
||||||
/** Filters the iterator for all elements of a certain kind */
|
/** Filters the iterator for all elements of a certain sub class */
|
||||||
inline fun <T, R: T> java.util.Iterator<T>.filterIs(): Iterator<R> = FilterIsIterator<T,R>(this)
|
inline fun <T, R: T> java.util.Iterator<T>.filterIsInstance(klass: Class<R>): Iterator<R> = FilterIsIterator<T,R>(this, klass)
|
||||||
|
|
||||||
private class FilterIsIterator<T, R :T>(val iter: java.util.Iterator<T>) : AbstractIterator<R>() {
|
private class FilterIsIterator<T, R :T>(val iter: java.util.Iterator<T>, val klass: Class<R>) : AbstractIterator<R>() {
|
||||||
override fun computeNext(): R? {
|
override fun computeNext(): R? {
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
val next = iter.next()
|
val next = iter.next()
|
||||||
if (next is R) {
|
if (klass.isInstance(next)) {
|
||||||
return next as R
|
return next as R
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -225,10 +225,10 @@ protected class PreviousSiblingIterator(var node: Node) : AbstractIterator<Node>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Returns an [[Iterator]] of all the next [[Element]] siblings */
|
/** Returns an [[Iterator]] of all the next [[Element]] siblings */
|
||||||
fun Node.nextElements(): Iterator<Element> = nextSiblings().filterIs<Node, Element>()
|
fun Node.nextElements(): Iterator<Element> = nextSiblings().filterIsInstance<Node, Element>(javaClass<Element>())
|
||||||
|
|
||||||
/** Returns an [[Iterator]] of all the previous [[Element]] siblings */
|
/** Returns an [[Iterator]] of all the previous [[Element]] siblings */
|
||||||
fun Node.previousElements(): Iterator<Element> = previousSiblings().filterIs<Node, Element>()
|
fun Node.previousElements(): Iterator<Element> = previousSiblings().filterIsInstance<Node, Element>(javaClass<Element>())
|
||||||
|
|
||||||
/** Returns the attribute value or empty string if its not present */
|
/** Returns the attribute value or empty string if its not present */
|
||||||
inline fun Element.attribute(name: String): String {
|
inline fun Element.attribute(name: String): String {
|
||||||
|
|||||||
@@ -61,7 +61,9 @@ class DomBuilderTest() : TestCase() {
|
|||||||
assertEquals(1, doc["#id3"].size())
|
assertEquals(1, doc["#id3"].size())
|
||||||
|
|
||||||
val root = doc.rootElement
|
val root = doc.rootElement
|
||||||
if (root != null) {
|
if (root == null) {
|
||||||
|
fail("No root!")
|
||||||
|
} else {
|
||||||
assertTrue {
|
assertTrue {
|
||||||
root.hasClass("bar")
|
root.hasClass("bar")
|
||||||
}
|
}
|
||||||
@@ -82,10 +84,13 @@ class DomBuilderTest() : TestCase() {
|
|||||||
assertEquals(1, root["#id1"].size())
|
assertEquals(1, root["#id1"].size())
|
||||||
assertEquals(1, root["#id2"].size())
|
assertEquals(1, root["#id2"].size())
|
||||||
assertEquals(1, root["#id3"].size())
|
assertEquals(1, root["#id3"].size())
|
||||||
} else {
|
|
||||||
fail("No root!")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// iterating through next element siblings
|
||||||
|
for (e in root.nextElements()) {
|
||||||
|
println("found element: $e")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
val grandChild = doc["grandChild"].first
|
val grandChild = doc["grandChild"].first
|
||||||
if (grandChild != null) {
|
if (grandChild != null) {
|
||||||
println("got element ${grandChild.toXmlString()} with text '${grandChild.text}`")
|
println("got element ${grandChild.toXmlString()} with text '${grandChild.text}`")
|
||||||
@@ -119,5 +124,6 @@ class DomBuilderTest() : TestCase() {
|
|||||||
val xml = nodesToXmlString(children)
|
val xml = nodesToXmlString(children)
|
||||||
println("root element has children: ${xml}")
|
println("root element has children: ${xml}")
|
||||||
assertEquals(1, children.size())
|
assertEquals(1, children.size())
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user