1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.javagen.agile.core.visitor;
17
18 import java.util.List;
19 import java.util.Set;
20
21 import org.javagen.agile.core.model.Model;
22
23 /***
24 * Traversal part of the visitor pattern implements a depth-first walk across generic model trees.
25 * An optional itinerary can be set on the visitor or passed explicitly to limit visitation to a
26 * subset of model instances.
27 * Children are determined by calling <code>parentModel.allOwnedModels()</code>.
28 *
29 * @author Richard Easterling
30 */
31 public final class GenericWalker {
32
33 /***
34 * Traverse the whole model tree. Visitation will be limited if an
35 * itinerary is specified by the visitor.
36 */
37 public static void walk(Model model, Visitor visitor) {
38 if (model==null)
39 return;
40 Set<String> itinerary = visitor.itinerary();
41 walk(model, visitor, itinerary);
42
43 }
44
45 /***
46 * Traverse the whole model tree. Visitation will be limited if an
47 * itinerary is specified.
48 */
49 public static void walk(Model model, Visitor visitor, Set<String> itinerary) {
50 if (itinerary==null || itinerary.contains(model.getModelType()))
51 visitor.visit(model);
52 List<Model> children = model.allOwnedModels();
53 if ( children != null) {
54 for(Model child : children) {
55 walk(child, visitor, itinerary);
56 }
57 }
58 }
59
60 }