Merge branch 'mdsal-trace' from controller
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / GeneratorJavaFile.java
1 /*
2  * Copyright (c) 2013 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.mdsal.binding.java.api.generator;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.HashBasedTable;
15 import com.google.common.collect.Table;
16 import java.io.File;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.function.Supplier;
21 import org.opendaylight.mdsal.binding.model.api.CodeGenerator;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Generates files with JAVA source codes for every specified type.
29  *
30  */
31 public final class GeneratorJavaFile {
32     public enum FileKind {
33         /**
34          * Transient file. It should be generated in target/generated-sources/ directory or similar.
35          */
36         TRANSIENT,
37         /**
38          * Persistent file. It should be generated in src/main/java/ directory or similar.
39          */
40         PERSISTENT,
41     }
42
43     private static final class GeneratorStringSupplier implements Supplier<String> {
44         private final CodeGenerator generator;
45         private final Type type;
46
47         GeneratorStringSupplier(final CodeGenerator generator, final Type type) {
48             this.generator = requireNonNull(generator);
49             this.type = requireNonNull(type);
50         }
51
52         @Override
53         public String get() {
54             return generator.generate(type);
55         }
56
57         @Override
58         public String toString() {
59             return MoreObjects.toStringHelper(this).add("generator", generator).add("type", type).toString();
60         }
61     }
62
63     private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
64
65     /**
66      * List of <code>CodeGenerator</code> instances.
67      */
68     private final List<CodeGenerator> generators = new ArrayList<>();
69
70     /**
71      * Set of <code>Type</code> instances for which the JAVA code is generated.
72      */
73     private final Collection<? extends Type> types;
74
75     /**
76      * Creates instance of this class with the set of <code>types</code> for
77      * which the JAVA code is generated. Generator instantiated this way uses
78      * the default build context, e.g. it will re-generate any and all files.
79      *
80      * The instances of concrete JAVA code generator are created.
81      *
82      * @param types
83      *            set of types for which JAVA code should be generated
84      */
85     public GeneratorJavaFile(final Collection<? extends Type> types) {
86         this.types = Preconditions.checkNotNull(types);
87         generators.add(new InterfaceGenerator());
88         generators.add(new TOGenerator());
89         generators.add(new EnumGenerator());
90         generators.add(new BuilderGenerator());
91     }
92
93     public Table<FileKind, String, Supplier<String>> generateFileContent(final boolean ignoreDuplicates) {
94         final Table<FileKind, String, Supplier<String>> result = HashBasedTable.create();
95         for (Type type : types) {
96             for (CodeGenerator generator : generators) {
97                 if (!generator.isAcceptable(type)) {
98                     continue;
99                 }
100
101                 final FileKind kind = type instanceof GeneratedTransferObject
102                         && ((GeneratedTransferObject) type).isUnionTypeBuilder()
103                         ? FileKind.PERSISTENT : FileKind.TRANSIENT;
104                 final String file = type.getPackageName().replace('.', File.separatorChar)
105                         +  File.separator + generator.getUnitName(type) + ".java";
106
107                 if (result.contains(kind, file)) {
108                     if (ignoreDuplicates) {
109                         LOG.warn("Naming conflict for type '{}': file with same name already exists and will not be "
110                                 + "generated.", type.getFullyQualifiedName());
111                         continue;
112                     }
113                     throw new IllegalStateException("Duplicate " + kind + " file '" + file + "' for "
114                             + type.getFullyQualifiedName());
115                 }
116
117                 result.put(kind, file, new GeneratorStringSupplier(generator, type));
118             }
119         }
120
121         return result;
122     }
123
124     /**
125      * Creates the package directory path as concatenation of
126      * <code>parentDirectory</code> and parsed <code>packageName</code>. The
127      * parsing of <code>packageName</code> is realized as replacement of the
128      * package name dots with the file system separator.
129      *
130      * @param parentDirectory
131      *            <code>File</code> object with reference to parent directory
132      * @param packageName
133      *            string with the name of the package
134      * @return <code>File</code> object which refers to the new directory for
135      *         package <code>packageName</code>
136      */
137     public static File packageToDirectory(final File parentDirectory, final String packageName) {
138         if (packageName == null) {
139             throw new IllegalArgumentException("Package Name cannot be NULL!");
140         }
141
142         return new File(parentDirectory, packageName.replace('.', File.separatorChar));
143     }
144 }