2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.java.api.generator;
10 import static org.opendaylight.controller.sal.java.api.generator.Constants.*;
12 import java.io.IOException;
13 import java.io.StringWriter;
14 import java.io.Writer;
15 import java.util.LinkedHashMap;
16 import java.util.List;
19 import org.opendaylight.controller.sal.binding.model.api.CodeGenerator;
20 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
22 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.controller.sal.binding.model.api.Type;
25 public final class ClassCodeGenerator implements CodeGenerator {
27 private Map<String, LinkedHashMap<String, Integer>> imports;
30 public Writer generate(Type type) throws IOException {
31 final Writer writer = new StringWriter();
32 if (type instanceof GeneratedTransferObject) {
33 GeneratedTransferObject genTO = (GeneratedTransferObject) type;
34 imports = GeneratorUtil.createImports(genTO);
36 final String currentPkg = genTO.getPackageName();
37 final List<GeneratedProperty> fields = genTO.getProperties();
38 final List<Enumeration> enums = genTO.getEnumDefintions();
40 writer.write(GeneratorUtil.createPackageDeclaration(currentPkg));
43 List<String> importLines = GeneratorUtil.createImportLines(imports);
44 for (String line : importLines) {
45 writer.write(line + NL);
49 writer.write(GeneratorUtil.createClassDeclaration(genTO, "",
55 EnumGenerator enumGenerator = new EnumGenerator();
56 for ( Enumeration e : enums ) {
57 writer.write(enumGenerator.generateInnerEnumeration(e, TAB).toString());
63 for (GeneratedProperty field : fields) {
64 writer.write(GeneratorUtil.createField(field, TAB, imports,
68 writer.write(GeneratorUtil.createConstructor(genTO, TAB,
71 for (GeneratedProperty field : fields) {
72 writer.write(GeneratorUtil.createGetter(field, TAB,
73 imports, currentPkg) + NL);
74 if (!field.isReadOnly()) {
75 writer.write(GeneratorUtil.createSetter(field, TAB,
76 imports, currentPkg) + NL);
81 if (!genTO.getHashCodeIdentifiers().isEmpty()) {
82 writer.write(GeneratorUtil.createHashCode(
83 genTO.getHashCodeIdentifiers(), TAB)
87 if (!genTO.getEqualsIdentifiers().isEmpty()) {
88 writer.write(GeneratorUtil.createEquals(genTO,
89 genTO.getEqualsIdentifiers(), TAB)
93 if (!genTO.getToStringIdentifiers().isEmpty()) {
94 writer.write(GeneratorUtil.createToString(genTO,
95 genTO.getToStringIdentifiers(), TAB)