Bump yangtools to 13.0.0
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractExplicitGenerator.java
index ad1f91154f1c173f2d10ea14f710d5eacfc025f7..7e3ad91645b342b847ad2053b4282ada3ebca9e8 100644 (file)
@@ -7,16 +7,16 @@
  */
 package org.opendaylight.mdsal.binding.generator.impl.reactor;
 
-import static com.google.common.base.Verify.verify;
 import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects.ToStringHelper;
-import java.util.Optional;
+import com.google.common.base.VerifyException;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.generator.impl.reactor.CollisionDomain.Member;
 import org.opendaylight.mdsal.binding.generator.impl.tree.StatementRepresentation;
+import org.opendaylight.mdsal.binding.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics;
 import org.opendaylight.mdsal.binding.model.api.Type;
 import org.opendaylight.mdsal.binding.model.api.TypeMemberComment;
@@ -24,7 +24,7 @@ import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotableTypeBuilde
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
-import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
+import org.opendaylight.yangtools.yang.binding.contract.Naming;
 import org.opendaylight.yangtools.yang.common.AbstractQName;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
@@ -38,6 +38,7 @@ import org.slf4j.LoggerFactory;
 /**
  * An explicit {@link Generator}, associated with a particular {@link EffectiveStatement}.
  */
+// FIXME: unify this with Generator
 public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?, ?>, R extends RuntimeType>
         extends Generator implements CopyableNode, StatementRepresentation<S> {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractExplicitGenerator.class);
@@ -79,12 +80,17 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
     }
 
     /**
-     * Return the {@link RuntimeType} associated with this object, if applicable. This represents the
-     * externally-accessible view of this object when considered outside the schema tree or binding tree hierarchy.
+     * Return the {@link RuntimeType} associated with this objec, if applicablet. This represents
+     * the externally-accessible view of this object when considered outside the schema tree or binding tree hierarchy.
      *
-     * @return Associated run-time type, or empty
+     * @return Associated run-time type, or {@code null}
      */
-    public final Optional<R> runtimeType() {
+    public final @Nullable R generatedRuntimeType() {
+        final var type = generatedType();
+        return type == null ? null : runtimeType();
+    }
+
+    private @Nullable R runtimeType() {
         if (!runtimeTypeInitialized) {
             final var type = runtimeJavaType();
             if (type != null) {
@@ -92,7 +98,21 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
             }
             runtimeTypeInitialized = true;
         }
-        return Optional.ofNullable(runtimeType);
+        return runtimeType;
+    }
+
+    /**
+     * Return the {@link RuntimeType} associated with this object. This represents the externally-accessible view of
+     * this object when considered outside the schema tree or binding tree hierarchy.
+     *
+     * @return Associated run-time type
+     */
+    public final @NonNull R getRuntimeType() {
+        final var local = runtimeType();
+        if (local == null) {
+            throw new VerifyException(this + " does not have a run-time type");
+        }
+        return local;
     }
 
     /**
@@ -113,7 +133,7 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
     // FIXME: this should be a generic class argument
     // FIXME: this needs a better name, but 'runtimeType' is already taken.
     @Nullable Type runtimeJavaType() {
-        return generatedType().orElse(null);
+        return generatedType();
     }
 
     /**
@@ -159,12 +179,12 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
 
     @Override
     public final boolean isAddedByUses() {
-        return statement instanceof AddedByUsesAware && ((AddedByUsesAware) statement).isAddedByUses();
+        return statement instanceof AddedByUsesAware aware && aware.isAddedByUses();
     }
 
     @Override
     public final boolean isAugmenting() {
-        return statement instanceof CopyableNode && ((CopyableNode) statement).isAugmenting();
+        return statement instanceof CopyableNode copyable && copyable.isAugmenting();
     }
 
     /**
@@ -257,28 +277,29 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
 
     final @Nullable AbstractExplicitGenerator<?, ?> findLocalSchemaTreeGenerator(final QName qname) {
         for (Generator child : this) {
-            if (child instanceof AbstractExplicitGenerator) {
-                final AbstractExplicitGenerator<?, ?> gen = (AbstractExplicitGenerator<?, ?>) child;
-                final EffectiveStatement<?, ?> stmt = gen.statement();
-                if (stmt instanceof SchemaTreeEffectiveStatement && qname.equals(stmt.argument())) {
-                    return gen;
-                }
+            if (child instanceof AbstractExplicitGenerator<?, ?> gen
+                && gen.statement() instanceof SchemaTreeEffectiveStatement<?> stmt && qname.equals(stmt.argument())) {
+                return gen;
             }
         }
         return null;
     }
 
     final @NonNull QName getQName() {
-        final Object arg = statement.argument();
-        verify(arg instanceof QName, "Unexpected argument %s", arg);
-        return (QName) arg;
+        final var arg = statement.argument();
+        if (arg instanceof QName qname) {
+            return qname;
+        }
+        throw new VerifyException("Unexpected argument " + arg);
     }
 
     @NonNull AbstractQName localName() {
         // FIXME: this should be done in a nicer way
-        final Object argument = statement.argument();
-        verify(argument instanceof AbstractQName, "Illegal argument %s", argument);
-        return (AbstractQName) argument;
+        final var arg = statement.argument();
+        if (arg instanceof AbstractQName aqn) {
+            return aqn;
+        }
+        throw new VerifyException("Illegal argument " + arg);
     }
 
     @Override
@@ -308,18 +329,18 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
             return;
         }
 
-        final Type returnType = methodReturnType(builderFactory);
+        final var returnType = methodReturnType(builderFactory);
         constructGetter(builder, returnType);
         constructRequire(builder, returnType);
     }
 
     MethodSignatureBuilder constructGetter(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
-        return constructGetter(builder, returnType, BindingMapping.getGetterMethodName(localName().getLocalName()));
+        return constructGetter(builder, returnType, Naming.getGetterMethodName(localName().getLocalName()));
     }
 
     final MethodSignatureBuilder constructGetter(final GeneratedTypeBuilderBase<?> builder,
             final Type returnType, final String methodName) {
-        final MethodSignatureBuilder getMethod = builder.addMethod(methodName).setReturnType(returnType);
+        final var getMethod = builder.addMethod(methodName).setReturnType(returnType);
 
         annotateDeprecatedIfNecessary(getMethod);
 
@@ -334,7 +355,7 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
     }
 
     final void constructRequireImpl(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
-        constructGetter(builder, returnType, BindingMapping.getRequireMethodName(localName().getLocalName()))
+        constructGetter(builder, returnType, Naming.getRequireMethodName(localName().getLocalName()))
             .setDefault(true)
             .setMechanics(ValueMechanics.NONNULL);
     }
@@ -364,4 +385,11 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
         }
         return helper;
     }
+
+    static final @NonNull GeneratedType verifyGeneratedType(final Type type) {
+        if (type instanceof GeneratedType ret) {
+            return ret;
+        }
+        throw new VerifyException("Unexpected type " + type);
+    }
 }