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.List;
18 import org.opendaylight.controller.binding.generator.util.TypeConstants;
19 import org.opendaylight.controller.sal.binding.model.api.*;
21 public final class ClassCodeGenerator implements CodeGenerator {
23 private Map<String, String> imports;
26 public Writer generate(Type type) throws IOException {
27 final Writer writer = new StringWriter();
29 if (type instanceof GeneratedTransferObject) {
30 GeneratedTransferObject genTO = (GeneratedTransferObject) type;
31 imports = GeneratorUtil.createImports(genTO);
33 final String currentPkg = genTO.getPackageName();
34 final List<GeneratedProperty> fields = genTO.getProperties();
35 final List<Enumeration> enums = genTO.getEnumerations();
36 final List<Constant> consts = genTO.getConstantDefinitions();
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, "", imports, genTO.isAbstract()));
52 for (Constant con : consts) {
53 writer.write(GeneratorUtil.createConstant(con, TAB, imports, currentPkg));
59 EnumGenerator enumGenerator = new EnumGenerator();
60 for (Enumeration e : enums) {
61 writer.write(enumGenerator.generateInnerEnumeration(e, TAB).toString());
66 boolean memberPatternListCodeRequired = false;
67 memberPatternListCodeRequired = (GeneratorUtil.isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, genTO));
68 if (fields != null || memberPatternListCodeRequired) {
70 for (GeneratedProperty field : fields) {
71 writer.write(GeneratorUtil.createField(field, TAB, imports, currentPkg) + NL);
74 if (memberPatternListCodeRequired) {
75 writer.write(TAB + PRIVATE + GAP + "List<Pattern>" + GAP + MEMBER_PATTERN_LIST + GAP + ASSIGN + GAP
76 + "new ArrayList<Pattern>()" + GAP + SC + NL);
80 writer.write(GeneratorUtil.createConstructor(genTO, TAB, imports, genTO.isAbstract()) + NL);
83 for (GeneratedProperty field : fields) {
84 writer.write(GeneratorUtil.createGetter(field, TAB, imports, currentPkg) + NL);
85 if (!field.isReadOnly()) {
86 writer.write(GeneratorUtil.createSetter(field, TAB, imports, currentPkg) + NL);
91 if (!genTO.getHashCodeIdentifiers().isEmpty()) {
92 writer.write(GeneratorUtil.createHashCode(genTO.getHashCodeIdentifiers(), TAB) + NL);
95 if (!genTO.getEqualsIdentifiers().isEmpty()) {
96 writer.write(GeneratorUtil.createEquals(genTO, genTO.getEqualsIdentifiers(), TAB) + NL);
99 if (!genTO.getToStringIdentifiers().isEmpty()) {
100 writer.write(GeneratorUtil.createToString(genTO, genTO.getToStringIdentifiers(), TAB) + NL);