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.HashMap;
19  import java.util.Map;
20  
21  import org.javagen.agile.core.context.Keys;
22  import org.javagen.agile.core.model.Model;
23  import org.javagen.agile.core.util.ModelUtil;
24  
25  /***
26   * Create a ID map for each Model instance and optionaly place resulting index in root node.
27   *
28   * @author Richard Easterling
29   */
30  public class IndexBuilderVisitor extends DefaultVisitor {
31  
32      private Map<String, Model> index;
33      private boolean placeIndexInRootNode = true;
34      
35      ////////////////////////////////////////////////////////////////////////////
36      // Generator interface:
37      ////////////////////////////////////////////////////////////////////////////
38      
39      public Model gen(Model model) {
40         index = new HashMap<String, Model>();
41         Model root = ModelUtil.getRootNode(model);
42          if (placeIndexInRootNode)
43              root.put(Keys.MODEL_ID_INDEX.toString(), index);
44          super.gen(root);
45          return model;
46      }
47      ////////////////////////////////////////////////////////////////////////////
48      // Visitor interface:
49      ////////////////////////////////////////////////////////////////////////////
50      
51      public void visit(Model model) {
52          if (model.getId()==null)
53              throw new IllegalStateException("Model node '"+model.getName()+"' does not contain an ID");
54          if (index.get(model.getId())!=null)
55              throw new IllegalStateException("ID not unique for Model ("+model+").  Another Model exists with the same ID: "+index.get(model.getId()));
56          index.put(model.getId(), model);
57      }
58  
59      ////////////////////////////////////////////////////////////////////////////
60      // getters and setters:
61      ////////////////////////////////////////////////////////////////////////////
62      
63      public Map<String, Model> getIndex() {
64          return index;
65      }
66  
67      public void setIndex(Map<String, Model> index) {
68          this.index = index;
69      }
70  
71      /***
72       * If true puts resulting index map into context of root model node 
73       * under <code>modelIdIndex</code> key.  Default is <code>true</code>.
74       */
75      public boolean isPlaceIndexInRootNode() {
76          return placeIndexInRootNode;
77      }
78  
79      public void setPlaceIndexInRootNode(boolean placeIndexInRootNode) {
80          this.placeIndexInRootNode = placeIndexInRootNode;
81      }
82  
83  }