1 19 20 package org.polepos.teams.hibernate.data; 21 22 23 public class HibernateTree { 24 25 public long id; 26 public HibernateTree preceding; 27 public HibernateTree subsequent; 28 public String name; 29 public int depth; 30 31 public HibernateTree(){ 32 } 33 34 public HibernateTree(String name, int depth){ 35 this.name = name; 36 this.depth = depth; 37 } 38 39 public long getId(){ 40 return id; 41 } 42 43 public void setId(long id){ 44 this.id = id;; 45 } 46 47 public HibernateTree getPreceding(){ 48 return preceding; 49 } 50 51 public void setPreceding(HibernateTree tree){ 52 preceding = tree; 53 } 54 55 public HibernateTree getSubsequent(){ 56 return subsequent; 57 } 58 59 public void setSubsequent(HibernateTree tree){ 60 subsequent = tree; 61 } 62 63 public String getName(){ 64 return name; 65 } 66 67 public void setName(String name){ 68 this.name = name; 69 } 70 71 public int getDepth(){ 72 return depth; 73 } 74 75 public void setDepth(int depth){ 76 this.depth = depth; 77 } 78 79 public static HibernateTree createTree(int depth){ 80 return createTree(depth, 0); 81 } 82 83 private static HibernateTree createTree(int maxDepth, int currentDepth){ 84 85 if(maxDepth <= 0){ 86 return null; 87 } 88 89 HibernateTree tree = new HibernateTree(); 90 if(currentDepth == 0){ 91 tree.name = "root"; 92 }else{ 93 tree.name = "node at depth " + currentDepth; 94 } 95 tree.depth = currentDepth; 96 tree.preceding = createTree(maxDepth - 1, currentDepth + 1); 97 tree.subsequent = createTree(maxDepth - 1, currentDepth + 1); 98 return tree; 99 } 100 101 public static void traverse(HibernateTree tree, HibernateTreeVisitor visitor){ 102 if(tree == null){ 103 return; 104 } 105 traverse(tree.preceding, visitor); 106 traverse(tree.subsequent, visitor); 107 visitor.visit(tree); 108 } 109 110 } 111 | Popular Tags |