View Javadoc

1   package org.catacomb.druid.util.tree;
2   
3   import org.catacomb.interlish.structure.Related;
4   import org.catacomb.interlish.structure.Relationship;
5   import org.catacomb.interlish.structure.SingleParent;
6   import org.catacomb.report.E;
7   
8   
9   import java.util.ArrayList;
10  import java.util.Collection;
11  import java.util.HashSet;
12  
13  
14  
15  public class Trawler {
16  
17  
18      public static Collection trawl(Related rel) {
19          HashSet<Related> hset = new HashSet<Related>();
20  
21          addIfNew(hset, rel);
22  
23          return hset;
24      }
25  
26  
27  
28      public static void addIfNew(HashSet<Related> hset, Related rel) {
29          if (hset.contains(rel)) {
30              // there already - nothing to do;
31  
32          } else {
33              hset.add(rel);
34              Relationship[] rels = rel.getRelationships();
35              for (int i = 0; i < rels.length; i++) {
36                  addIfNew(hset, rels[i].getTarget());
37              }
38          }
39      }
40  
41  
42  
43  
44      public static ArrayList<Related> trawlChildren(SingleParent sp) {
45          ArrayList<Related> arl = new ArrayList<Related>();
46          addAll(arl, sp);
47          return arl;
48      }
49  
50  
51      public static void addAll(ArrayList<Related> arl, SingleParent sp) {
52          ArrayList<? extends Object> arc = sp.getChildren();
53  
54          arl.add(sp);
55  
56          for (Object obj : arc) {
57  
58              if (obj instanceof SingleParent) {
59                  addAll(arl, (SingleParent)obj);
60  
61              } else if (obj instanceof Related) {
62                  arl.add((Related)obj);
63  
64              } else {
65                  E.error(" - Relation Tree Trawlse found unrelated elt " +
66                          obj + " " + obj.getClass());
67              }
68          }
69      }
70  
71  
72  
73  }