1 package com.bonevich.erj.model;
2
3 import com.bonevich.util.Functor;
4
5 import java.util.Iterator;
6
7 /***
8 * Utility class for traversing the tree of ModelElements and performing some
9 * function on each node visited. The traversal occurs in the following
10 * order, from left to right, top to bottom:
11 * <pre>
12 * Schema
13 * |
14 * ------------
15 * | |
16 * Domain Relation
17 * |
18 * ---------------
19 * | |
20 * Attribute KeyConstraint (UniqueKey, PrimaryKey, ForeignKey)
21 * </pre>
22 *
23 * @see ModelElementTraverser
24 * @author Jeffrey D. Bonevich <bonevich@telocity.com>
25 */
26 public final class ModelElementTraverser
27 implements ModelElementVisitor, KeyVisitor
28 {
29 //////////////////////////////////////////////////////////
30 // Attributes
31 private Functor _functor;
32
33 //////////////////////////////////////////////////////////
34 // Constructors
35 public ModelElementTraverser(Functor f)
36 {
37 _functor = f;
38 }
39
40 //////////////////////////////////////////////////////////
41 // Operations
42 public void traverse(ModelElement element)
43 {
44 element.accept(this);
45 }
46
47 //////////////////////////////////////////////////////////
48 // ModelElementVisitor implementation
49 public void visitSchema(Schema element)
50 {
51 _functor.function(element);
52
53 // now visit each Domain
54 Iterator domains = element.getDomainIterator();
55 while (domains.hasNext())
56 {
57 Domain d = (Domain) domains.next();
58 d.accept(this);
59 }
60
61 // now visit each Relation
62 Iterator relations = element.getRelationIterator();
63 while (relations.hasNext())
64 {
65 Relation r = (Relation) relations.next();
66 r.accept(this);
67 }
68 }
69
70 public void visitRelation(Relation element)
71 {
72 _functor.function(element);
73
74 // now visit each Attribute
75 Iterator attributes = element.getAttributeIterator();
76 while (attributes.hasNext())
77 {
78 Attribute a = (Attribute) attributes.next();
79 a.accept(this);
80 }
81
82 // now visit each KeyConstraint
83 Iterator keys = element.getKeyIterator();
84 while (keys.hasNext())
85 {
86 KeyConstraint k = (KeyConstraint) keys.next();
87 k.accept((ModelElementVisitor) this);
88 }
89 }
90
91 public void visitAttribute(Attribute element)
92 {
93 _functor.function(element);
94 }
95
96 public void visitDomain(Domain element)
97 {
98 _functor.function(element);
99 }
100
101 public void visitKey(KeyConstraint element)
102 {
103 element.accept((KeyVisitor) this);
104 }
105
106 //////////////////////////////////////////////////////////
107 // KeyVisitor implementation
108 public void visitUniqueKey(UniqueKey key)
109 {
110 _functor.function(key);
111 }
112
113 public void visitPrimaryKey(PrimaryKey key)
114 {
115 _functor.function(key);
116 }
117
118 public void visitForeignKey(ForeignKey key)
119 {
120 _functor.function(key);
121 }
122
123 } /* end class ModelElementTraverser */
This page was automatically generated by Maven