Specialize JavaFileTemplate.importedName(Type)
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / JavaFileTemplate.java
index 5387a82c5340020f862c7635de89588cdd4b8f91..58282d64e275cc6dc1ea7ae0ef110f2d05e7f0fd 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.mdsal.binding.java.api.generator;
 import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
 
+import java.util.Arrays;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.stream.Collectors;
 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
@@ -25,6 +27,9 @@ import org.opendaylight.mdsal.binding.model.util.Types;
  * Base Java file template. Contains a non-null type and imports which the generated code refers to.
  */
 class JavaFileTemplate {
+    static final JavaTypeName NONNULL = JavaTypeName.create("org.eclipse.jdt.annotation", "NonNull");
+    static final JavaTypeName NULLABLE = JavaTypeName.create("org.eclipse.jdt.annotation", "Nullable");
+
     private final AbstractJavaGeneratedType javaType;
     private final GeneratedType type;
 
@@ -66,6 +71,10 @@ class JavaFileTemplate {
         return javaType.getReferenceString(intype);
     }
 
+    final String importedName(final Type intype, final String... annotations) {
+        return javaType.getReferenceString(intype, annotations);
+    }
+
     final String importedName(final Class<?> cls) {
         return importedName(Types.typeForClass(cls));
     }
@@ -74,6 +83,14 @@ class JavaFileTemplate {
         return javaType.getReferenceString(intype);
     }
 
+    final String importedNonNull(final Type intype) {
+        return importedName(intype, importedName(NONNULL));
+    }
+
+    final String importedNullable(final Type intype) {
+        return importedName(intype, importedName(NULLABLE));
+    }
+
     final void addImport(final Class<?> cls) {
         javaType.getReferenceString(JavaTypeName.create(cls));
     }
@@ -95,6 +112,17 @@ class JavaFileTemplate {
                 : new ClassTemplate(innerJavaType, gto).generateAsInnerClass();
     }
 
+    /**
+     * Return imported name of java.util class, whose hashCode/equals methods we want to invoke on the property. Returns
+     * {@link Arrays} if the property is an array, {@link Objects} otherwise.
+     *
+     * @param property Generated property
+     * @return Imported class name
+     */
+    final String importedUtilClass(final GeneratedProperty property) {
+        return importedName(property.getReturnType().getName().indexOf('[') != -1 ? Arrays.class : Objects.class);
+    }
+
     static final Restrictions restrictionsForSetter(final Type actualType) {
         return actualType instanceof GeneratedType ? null : getRestrictions(actualType);
     }