Update the API generation code and code generation sample
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / controller / sal / java / api / generator / CompositeKeyGenerator.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.java.api.generator;\r
9 \r
10 import static org.opendaylight.controller.sal.java.api.generator.Constants.NL;\r
11 import static org.opendaylight.controller.sal.java.api.generator.Constants.RCB;\r
12 import static org.opendaylight.controller.sal.java.api.generator.Constants.TAB;\r
13 \r
14 import java.io.IOException;\r
15 import java.io.StringWriter;\r
16 import java.io.Writer;\r
17 import java.util.List;\r
18 \r
19 import org.opendaylight.controller.sal.binding.model.api.CodeGenerator;\r
20 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;\r
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;\r
22 import org.opendaylight.controller.sal.binding.model.api.Type;\r
23 \r
24 public class CompositeKeyGenerator implements CodeGenerator {\r
25 \r
26     @Override\r
27     public Writer generate(Type type) throws IOException {\r
28         final Writer writer = new StringWriter();\r
29         if (type instanceof GeneratedTransferObject) {\r
30             GeneratedTransferObject genTO = (GeneratedTransferObject)type;\r
31             final List<GeneratedProperty> fields = genTO.getProperties();\r
32 \r
33             writer.write(GeneratorUtil.createClassDeclarationWithPkgName(\r
34                     type.getPackageName(), type.getName(), ""));\r
35             writer.write(NL);\r
36             writer.write(NL);\r
37             \r
38             if (fields != null) {\r
39                 for (GeneratedProperty field : fields) {\r
40                     writer.write(GeneratorUtil.createField(field, TAB) + NL);\r
41                 }\r
42                 writer.write(NL);\r
43                 writer.write(GeneratorUtil.createConstructor(genTO, TAB) + NL);\r
44                 writer.write(NL);\r
45                 for (GeneratedProperty field : fields) {\r
46                     writer.write(GeneratorUtil.createGetter(field, TAB) + NL);\r
47                 }\r
48                 writer.write(NL);\r
49 \r
50                 writer.write(GeneratorUtil.createHashCode(genTO.getHashCodeIdentifiers(), TAB) + NL);\r
51                 writer.write(GeneratorUtil.createEquals(genTO, genTO.getEqualsIdentifiers(), TAB) + NL);\r
52                 writer.write(GeneratorUtil.createToString(genTO, genTO.getToStringIdentifiers(), TAB) + NL);\r
53 \r
54                 writer.write(RCB);\r
55             }\r
56         }\r
57         return writer;\r
58     }\r
59 \r
60 }\r

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.