Cleaned up Java Binding code from YANG Tools
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / BuilderGenerator.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.sal.java.api.generator;
9
10 import org.opendaylight.yangtools.sal.binding.model.api.CodeGenerator;
11 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
12 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
13 import org.opendaylight.yangtools.sal.binding.model.api.Type;
14 import org.opendaylight.yangtools.yang.binding.Augmentable;
15 import org.opendaylight.yangtools.yang.binding.Augmentation;
16
17 /**
18  * 
19  * Transformator of the data from the virtual form to JAVA programming language.
20  * The result source code represent java class. For generation of the source
21  * code is used the template written in XTEND language.
22  * 
23  */
24 public final class BuilderGenerator implements CodeGenerator {
25
26     /**
27      * Constant used as sufix for builder name.
28      */
29     public static final String BUILDER = "Builder";
30
31     /**
32      * Passes via list of implemented types in <code>type</code>.
33      * 
34      * @param type
35      *            JAVA <code>Type</code>
36      * @return boolean value which is true if any of implemented types is of the
37      *         type <code>Augmentable</code>.
38      */
39     @Override
40     public boolean isAcceptable(Type type) {
41         if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
42             for (Type t : ((GeneratedType) type).getImplements()) {
43                 // "rpc" and "grouping" elements do not implement Augmentable
44                 if (t.getFullyQualifiedName().equals(Augmentable.class.getName())) {
45                     return true;
46                 } else if (t.getFullyQualifiedName().equals(Augmentation.class.getName())) {
47                     return true;
48                 }
49
50             }
51         }
52         return false;
53     }
54
55     /**
56      * Generates JAVA source code for generated type <code>Type</code>. The code
57      * is generated according to the template source code template which is
58      * written in XTEND language.
59      */
60     @Override
61     public String generate(Type type) {
62         if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
63             final GeneratedType genType = (GeneratedType) type;
64             final BuilderTemplate template = new BuilderTemplate(genType);
65             return template.generate();
66         }
67         return "";
68     }
69
70     @Override
71     public String getUnitName(Type type) {
72         return type.getName() + BUILDER;
73     }
74
75 }