You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iota.apache.org by to...@apache.org on 2016/11/29 23:29:25 UTC

[26/31] incubator-iota git commit: Corrected TrieNode.scala according to the comments

Corrected TrieNode.scala according to the comments


Project: http://git-wip-us.apache.org/repos/asf/incubator-iota/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-iota/commit/238cf47e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-iota/tree/238cf47e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-iota/diff/238cf47e

Branch: refs/heads/master
Commit: 238cf47e2e67b784aaf44ffb95a6857afe466899
Parents: 5b2c376
Author: Shivansh <sh...@gmail.com>
Authored: Tue Nov 15 06:46:23 2016 +0530
Committer: Shivansh <sh...@gmail.com>
Committed: Tue Nov 15 06:46:23 2016 +0530

----------------------------------------------------------------------
 .../scala/org/apache/iota/fey/TrieNode.scala    | 60 ++++++++++----------
 1 file changed, 31 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/238cf47e/fey-core/src/main/scala/org/apache/iota/fey/TrieNode.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/main/scala/org/apache/iota/fey/TrieNode.scala b/fey-core/src/main/scala/org/apache/iota/fey/TrieNode.scala
index 902e5f2..2abbb86 100644
--- a/fey-core/src/main/scala/org/apache/iota/fey/TrieNode.scala
+++ b/fey-core/src/main/scala/org/apache/iota/fey/TrieNode.scala
@@ -29,25 +29,27 @@ case class TrieNode(path: String, children: ArrayBuffer[TrieNode], events:ArrayB
 
 protected class Trie(systemName: String){
 
-  private val root: TrieNode = TrieNode(systemName, ArrayBuffer.empty, ArrayBuffer.empty)
+  private val DEFAULT_TRIE_NODE = TrieNode(systemName, ArrayBuffer.empty, ArrayBuffer.empty
+
+  private val root: Option[TrieNode] = Option(DEFAULT_TRIE_NODE)
   var elements: Int = 0
 
   def append(path: String, event: Monitor.MonitorEvent = null): Unit = {
-    append(path.replaceFirst("akka://","").split("/"),root,1,event)
+    append(path.replaceFirst("akka://", "").split("/"), root, 1, event)
   }
 
-  @tailrec private def append(path: Array[String], root: TrieNode, index: Int, event: Monitor.MonitorEvent): Unit = {
-    if (Option(root).isDefined && index < path.length) {
-      var nextRoot = root.children.filter(child => child.path == path(index))
+  @tailrec private def append(path: Array[String], root: Option[TrieNode], index: Int, event: Monitor.MonitorEvent): Unit = {
+    if (root.isDefined && index < path.length) {
+      var nextRoot = root.fold(DEFAULT_TRIE_NODE)(identity).children.filter(child => child.path == path(index))
       if(nextRoot.isEmpty){
         nextRoot = ArrayBuffer(TrieNode(path(index), ArrayBuffer.empty, ArrayBuffer.empty))
-        root.children += nextRoot(0)
+        root.fold(DEFAULT_TRIE_NODE)(identity).children += nextRoot(0)
         elements += 1
       }
       if (Option(event).isDefined && index == path.length - 1) {
         nextRoot(0).events += event
       }
-      append(path, nextRoot(0), index + 1, event)
+      append(path, nextRoot.headOption, index + 1, event)
     }
   }
 
@@ -55,13 +57,13 @@ protected class Trie(systemName: String){
     recHasPath(root, path.replaceFirst("akka://","").split("/"),1)
   }
 
-  @tailrec private def recHasPath(root: TrieNode, path: Array[String], index: Int): Boolean = {
-    if (Option(root).isDefined && index < path.length) {
-      var nextRoot = root.children.filter(child => child.path == path(index))
+  @tailrec private def recHasPath(root: Option[TrieNode], path: Array[String], index: Int): Boolean = {
+    if (root.isDefined && index < path.length) {
+      var nextRoot = root.fold(DEFAULT_TRIE_NODE)(identity).children.filter(child => child.path == path(index))
       if(nextRoot.isEmpty){
         false
       }else{
-        recHasPath(nextRoot(0), path, index + 1)
+        recHasPath(nextRoot.headOption, path, index + 1)
       }
     }else{
       true
@@ -72,16 +74,16 @@ protected class Trie(systemName: String){
     recGetNode(root, path.replaceFirst("akka://","").split("/"),1)
   }
 
-  @tailrec private def recGetNode(root: TrieNode, path: Array[String], index: Int): Option[TrieNode]= {
-    if (Option(root).isDefined && index < path.length) {
-      var nextRoot = root.children.filter(child => child.path == path(index))
+  @tailrec private def recGetNode(root: Option[TrieNode], path: Array[String], index: Int): Option[TrieNode] = {
+    if (root.isDefined && index < path.length) {
+      var nextRoot = root.fold(DEFAULT_TRIE_NODE)(identity).children.filter(child => child.path == path(index))
       if(nextRoot.isEmpty){
         None
       }else{
         if(path.length - 1 == index){
             Some(nextRoot(0))
         }else {
-          recGetNode(nextRoot(0), path, index + 1)
+          recGetNode(nextRoot.headOption, path, index + 1)
         }
       }
     }else{
@@ -91,8 +93,8 @@ protected class Trie(systemName: String){
 
   def removeAllNodes(): Unit = {
     var index = 0
-    while(index < root.children.length){
-      root.children.remove(index)
+    while (index < root.fold(DEFAULT_TRIE_NODE)(identity).children.length) {
+      root.fold(DEFAULT_TRIE_NODE)(identity).children.remove(index)
       index += 1
     }
     elements = 0
@@ -107,31 +109,31 @@ protected class Trie(systemName: String){
   }
 
   def getRootChildren():ArrayBuffer[TrieNode] = {
-    root.children
+    root.fold(DEFAULT_TRIE_NODE)(identity).children
   }
 
-  private def getObject(root: TrieNode, parent: TrieNode):JsObject = {
-    if (Option(root).isDefined) {
-     Json.obj("name" -> root.path,
-       "parent" -> (if (Option(parent).isDefined) parent.path else "null"),
-        "children" -> root.children.map(getObject(_, root))
+  private def getObject(root: Option[TrieNode], parent: Option[TrieNode]): JsObject = {
+    if (root.isDefined) {
+      Json.obj("name" -> root.fold(DEFAULT_TRIE_NODE)(identity).path,
+       "parent" -> (if (parent.isDefined) parent.fold(DEFAULT_TRIE_NODE)(identity).path else "null"),
+        "children" -> root.fold(DEFAULT_TRIE_NODE)(identity).children.map(a => getObject(Option(a), root))
      )
     }else{
       Json.obj()
     }
   }
 
-  private def getObjectEvent(root: TrieNode, parent: TrieNode):JsObject = {
-    if (Option(root).isDefined) {
-      Json.obj("name" -> root.path,
-        "parent" -> (if (Option(parent).isDefined) parent.path else "null"),
-        "events" -> root.events.map(event => {
+  private def getObjectEvent(root: Option[TrieNode], parent: Option[TrieNode]):JsObject = {
+    if (root.isDefined) {
+      Json.obj("name" -> root.fold(DEFAULT_TRIE_NODE)(identity).path,
+        "parent" -> (if (parent.isDefined) parent.fold(DEFAULT_TRIE_NODE)(identity).path else "null"),
+        "events" -> root.fold(DEFAULT_TRIE_NODE)(identity).events.map(event => {
           Json.obj("type" -> event.event,
           "timestamp" -> event.timestamp,
             "info" -> event.info
           )
         }),
-        "children" -> root.children.map(getObjectEvent(_, root))
+        "children" -> root.fold(DEFAULT_TRIE_NODE)(identity).children.map(a=>getObjectEvent(Option(a), root))
       )
     }else{
       Json.obj()