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.GeneratedProperty;
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.controller.sal.binding.model.api.Type;
24 public class ClassCodeGenerator implements CodeGenerator {
26 private Map<String, LinkedHashMap<String, Integer>> imports;
29 public Writer generate(Type type) throws IOException {
30 final Writer writer = new StringWriter();
31 if (type instanceof GeneratedTransferObject) {
32 GeneratedTransferObject genTO = (GeneratedTransferObject) type;
33 imports = GeneratorUtil.createImports(genTO);
35 final String currentPkg = genTO.getPackageName();
36 final List<GeneratedProperty> fields = genTO.getProperties();
38 writer.write(GeneratorUtil.createPackageDeclaration(currentPkg));
41 List<String> importLines = GeneratorUtil.createImportLines(imports);
42 for (String line : importLines) {
43 writer.write(line + NL);
47 writer.write(GeneratorUtil.createClassDeclaration(genTO, "",
53 for (GeneratedProperty field : fields) {
54 writer.write(GeneratorUtil.createField(field, TAB, imports,
58 writer.write(GeneratorUtil.createConstructor(genTO, TAB,
61 for (GeneratedProperty field : fields) {
62 writer.write(GeneratorUtil.createGetter(field, TAB,
63 imports, currentPkg) + NL);
64 if (!field.isReadOnly()) {
65 writer.write(GeneratorUtil.createSetter(field, TAB,
66 imports, currentPkg) + NL);
71 if (!genTO.getHashCodeIdentifiers().isEmpty()) {
72 writer.write(GeneratorUtil.createHashCode(
73 genTO.getHashCodeIdentifiers(), TAB)
77 if (!genTO.getEqualsIdentifiers().isEmpty()) {
78 writer.write(GeneratorUtil.createEquals(genTO,
79 genTO.getEqualsIdentifiers(), TAB)
83 if (!genTO.getToStringIdentifiers().isEmpty()) {
84 writer.write(GeneratorUtil.createToString(genTO,
85 genTO.getToStringIdentifiers(), TAB)