View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the 'License')
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an 'AS IS' BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  		//return visitor.result();
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  }