Implemented iterator. Kotlin extensions.

This commit is contained in:
Adrian Kuta 2020-01-11 00:41:36 +01:00
parent 0a04ecd532
commit 0ece34daf2
5 changed files with 44 additions and 12 deletions

View File

@ -0,0 +1,21 @@
package com.github.adriankuta.datastructure.tree
interface ChildDeclarationInterface<T> {
/**
* This method is used to easily create child in node.
* ```
* val root = treeNode("World") {
* treeNode("North America") {
* treeNode("USA")
* }
* treeNode("Europe") {
* treeNode("Poland")
* treeNode("Germany")
* }
* }
* ```
*/
@JvmSynthetic
fun treeNode(value: T, childDeclaration: ChildDeclaration<T>? = null)
}

View File

@ -0,0 +1,9 @@
package com.github.adriankuta.datastructure.tree;
public class Temp {
public static void main(String[] args) {
TreeNode<String> temp = new TreeNode<>("Test");
}
}

View File

@ -26,7 +26,8 @@ open class TreeNode<T>(val value: T) : Iterable<TreeNode<T>>, ChildDeclarationIn
_children.add(child) _children.add(child)
} }
override fun child(value: T, childDeclaration: ChildDeclaration<T>?) { @JvmSynthetic
override fun treeNode(value: T, childDeclaration: ChildDeclaration<T>?) {
val newChild = TreeNode(value) val newChild = TreeNode(value)
if(childDeclaration != null) if(childDeclaration != null)
newChild.childDeclaration() newChild.childDeclaration()

View File

@ -2,14 +2,16 @@ package com.github.adriankuta.datastructure.tree
typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit typealias ChildDeclaration<T> = ChildDeclarationInterface<T>.() -> Unit
/**
* This method can be used to initialize new tree.
* ```
* val root = treeNode("World") { ... }
* ```
* @see [ChildDeclarationInterface.treeNode]
*/
@JvmSynthetic @JvmSynthetic
inline fun<reified T> treeNode(value: T, childDeclaration: ChildDeclaration<T>): TreeNode<T> { inline fun<reified T> treeNode(value: T, childDeclaration: ChildDeclaration<T>): TreeNode<T> {
val treeNode = TreeNode(value) val treeNode = TreeNode(value)
treeNode.childDeclaration() treeNode.childDeclaration()
return treeNode return treeNode
}
interface ChildDeclarationInterface<T> {
fun child(value: T, childDeclaration: ChildDeclaration<T>? = null)
} }

View File

@ -115,15 +115,14 @@ class TreeNodeTest {
@Test @Test
fun kotlinExtTest() { fun kotlinExtTest() {
val root = treeNode("World") { val root = treeNode("World") {
child("North America") { treeNode("North America") {
child("USA") treeNode("USA")
} }
child("Europe") { treeNode("Europe") {
child("Poland") treeNode("Poland")
child("Germany") treeNode("Germany")
} }
} }
println(root) println(root)
} }