import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
+
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.binding.javav2.generator.util.Types;
import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil;
-import org.opendaylight.mdsal.binding.javav2.model.api.Constant;
import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedProperty;
import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTransferObject;
import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType;
import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
import org.opendaylight.mdsal.binding.javav2.model.api.Type;
import org.opendaylight.mdsal.binding.javav2.model.api.WildcardType;
-import org.opendaylight.yangtools.yang.common.QName;
+
+
public abstract class BaseRenderer {
private static final String COMMA = ",";
private static final String DOT = ".";
- private final GeneratedType type;
+ private final GeneratedType genType;
private final Map<String, String> importMap;
+ /**
+ * list of all imported names for template.
+ */
+ private final Map<String, String> importedNames = new HashMap<>();
protected BaseRenderer(final GeneratedType type) {
- this.type = Preconditions.checkNotNull(type);
+ this.genType = Preconditions.checkNotNull(type);
this.importMap = new HashMap<>();
}
/**
- * Implementation needs to call Scala template render() method to generate string body
+ * Implementation needs to call Scala template render() method to generate string body.
* @return rendered body
*/
protected abstract String body();
protected GeneratedType getType() {
- return type;
+ return genType;
+ }
+
+ protected Map<String, String> getImportedNames() {
+ return importedNames;
}
- protected String getFromImportMap(@NonNull String typeName) {
+ protected String getFromImportMap(@NonNull final String typeName) {
return importMap.get(typeName);
}
- protected void putToImportMap(@NonNull String typeName, String typePackageName) {
+ protected void putToImportMap(@NonNull final String typeName, final String typePackageName) {
importMap.put(typeName, typePackageName);
}
- protected void putAllToImportMap(@NonNull Map<String, String> imports) {
+ protected void putAllToImportMap(@NonNull final Map<String, String> imports) {
importMap.putAll(imports);
}
}
/**
+ * Return string with type name for type in the full format or in the short format.
* @param intype type to format and add to imports
* @return formatted type
*/
protected String importedName(final Type intype) {
- putTypeIntoImports(type, intype);
- return getExplicitType(type, intype);
+ putTypeIntoImports(genType, intype);
+ return getExplicitType(genType, intype);
}
protected String importedName(final Class<?> cls) {
}
/**
+ * Generate package definition for template.
* @return package definition for template
*/
- private String packageDefinition() {
+ protected String packageDefinition() {
final StringBuilder sb = new StringBuilder();
sb.append("package ")
- .append(type.getPackageName())
+ .append(genType.getPackageName())
.append(";\n\n");
return sb.toString();
}
/**
- * walks through map of imports
+ * walks through map of imports.
* @return string of imports for template
*/
private String imports() {
}
/**
- * Checks if package of generated type and imported type is the same
+ * Checks if package of generated type and imported type is the same.
* @param importedTypePackageName imported types package name
* @return equals packages
*/
- private boolean hasSamePackage(final String importedTypePackageName) {
- return type.getPackageName().equals(importedTypePackageName);
+ protected boolean hasSamePackage(final String importedTypePackageName) {
+ return genType.getPackageName().equals(importedTypePackageName);
}
/**
* @param parentGenType generated type which contains type
* @return adds actual type parameters to builder
*/
- private StringBuilder addActualTypeParameters(final StringBuilder sb, final Type type, final GeneratedType parentGenType) {
+ private StringBuilder addActualTypeParameters(final StringBuilder sb, final Type type,
+ final GeneratedType parentGenType) {
if (type instanceof ParameterizedType) {
final ParameterizedType pType = (ParameterizedType) type;
final Type[] pTypes = pType.getActualTypeArguments();
return sb;
}
- protected GeneratedProperty findProperty(final GeneratedTransferObject gto, String name) {
+ protected GeneratedProperty findProperty(final GeneratedTransferObject gto, final String name) {
for (GeneratedProperty prop : gto.getProperties()) {
if (name.equals(prop.getName())) {
return prop;
}
/**
- * @param constant
- * @return string with constant wrapped in code
- */
- protected String emitConstant(final Constant constant) {
- final StringBuilder sb = new StringBuilder();
- final Object qname = constant.getValue();
- sb.append("public static final ")
- .append(importedName(constant.getType()))
- .append(' ')
- .append(constant.getName())
- .append(" = ");
- if (qname instanceof QName) {
- sb.append(QName.class.getName())
- .append(".create(\"")
- .append(((QName) qname).getNamespace().toString())
- .append("\", \"")
- .append(((QName) qname).getFormattedRevision())
- .append("\", \"")
- .append(((QName) qname).getLocalName())
- .append("\").intern()");
- } else {
- sb.append(qname);
- }
- sb.append(";\n");
- return sb.toString();
- }
-
- /**
- * Generates the string with all actual type parameters from
+ * Generates the string with all actual type parameters from.
*
* @param parentGenType generated type for which is the JAVA code generated
- * @param pTypes array of Type instances = actual type parameters
- * @return string with all actual type parameters from pTypes
+ * @param types array of Type instances = actual type parameters
+ * @return string with all actual type parameters from types
*/
- private String getParameters(final GeneratedType parentGenType, final Type[] pTypes) {
- if (pTypes == null || pTypes.length == 0) {
+ private String getParameters(final GeneratedType parentGenType, final Type[] types) {
+ if (types == null || types.length == 0) {
return "?";
}
final StringBuilder sb = new StringBuilder();
- for (int i = 0; i < pTypes.length; i++) {
- final Type t = pTypes[i];
+ for (int i = 0; i < types.length; i++) {
+ final Type t = types[i];
String separator = COMMA;
- if (i == (pTypes.length - 1)) {
+ if (i == types.length - 1) {
separator = "";
}
}
/**
- * Template method which generates the getter method for field
+ * Template method which generates the getter method for field.
*
* @param field generated property with data about field which is generated as the getter method
* @return string with the getter method source code in JAVA format
.append("() {")
.append("return ")
.append(name);
- if (importedName.contains("[]")) {
+ if (!(field.getReturnType() instanceof ParameterizedType)
+ && importedName.contains("[]")) {
sb.append(" == null ? null : ")
.append(name)
.append(".clone()");
}
- sb.append(";}");
+ sb.append(";}\n");
return sb.toString();
}
/**
- * builds template
+ * builds template.
* @return generated final template
*/
public String generateTemplate() {