Add DefaultStatementDefinition 38/67938/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 5 Feb 2018 05:01:34 +0000 (06:01 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 5 Feb 2018 17:12:00 +0000 (18:12 +0100)
Implementing StatementDefinition contract can be tricky and it is
often misused by using implementation classes. Introduce a utility
default implementation, which enforces statement representations
being interfaces.

Change-Id: Icd2ce998dc8001cfa4a1d44f48017204c6dd02f4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/meta/DefaultStatementDefinition.java [new file with mode: 0644]

diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/meta/DefaultStatementDefinition.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/meta/DefaultStatementDefinition.java
new file mode 100644 (file)
index 0000000..c3e2f90
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.api.meta;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
+import javax.annotation.concurrent.ThreadSafe;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.yang.common.QName;
+
+/**
+ * Default implementation of the {@link StatementDefinition} contract. Instances of this class should be used as
+ * well-known singletons.
+ *
+ * @author Robert Varga
+ *
+ * @param <A> Argument type
+ * @param <D> Declared statement representation
+ * @param <E> Effective statement representation
+ */
+@Beta
+@NonNullByDefault
+@ThreadSafe
+public final class DefaultStatementDefinition<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
+        implements StatementDefinition {
+    private final Class<E> effectiveRepresentation;
+    private final Class<D> declaredRepresentation;
+    private final QName statementName;
+    private final @Nullable QName argumentName;
+    private final boolean argumentYinElement;
+
+    DefaultStatementDefinition(final QName statementName, final Class<D> declaredRepresentation,
+            final Class<E> effectiveRepresentation, final boolean argumentYinElement,
+            final @Nullable QName argumentName) {
+        this.statementName = requireNonNull(statementName);
+        this.declaredRepresentation = requireNonNull(declaredRepresentation);
+        this.effectiveRepresentation = requireNonNull(effectiveRepresentation);
+        this.argumentYinElement = argumentYinElement;
+        this.argumentName = argumentName;
+
+        checkArgument(declaredRepresentation.isInterface(), "Declared representation %s is not an interface",
+            declaredRepresentation);
+        checkArgument(effectiveRepresentation.isInterface(), "Effective representation %s is not an interface",
+            effectiveRepresentation);
+    }
+
+    public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
+            DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
+                    final Class<E> effectiveRepresentation) {
+        return new DefaultStatementDefinition<>(statementName, declaredRepresentation, effectiveRepresentation, false,
+                null);
+    }
+
+    public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
+            DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
+                    final Class<E> effectiveRepresentation, final QName argumentName) {
+        return of(statementName, declaredRepresentation, effectiveRepresentation, argumentName, false);
+    }
+
+    public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
+            DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
+                    final Class<E> effectiveRepresentation, final QName argumentName,
+                    final boolean argumentYinElement) {
+        return new DefaultStatementDefinition<>(statementName, declaredRepresentation, effectiveRepresentation,
+                argumentYinElement, requireNonNull(argumentName));
+    }
+
+    @Override
+    public QName getStatementName() {
+        return statementName;
+    }
+
+    @Override
+    public @Nullable QName getArgumentName() {
+        return argumentName;
+    }
+
+    @Override
+    public Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
+        return declaredRepresentation;
+    }
+
+    @Override
+    public Class<? extends EffectiveStatement<?, ?>> getEffectiveRepresentationClass() {
+        return effectiveRepresentation;
+    }
+
+    @Override
+    public boolean isArgumentYinElement() {
+        return argumentYinElement;
+    }
+
+    @Override
+    public String toString() {
+        final ToStringHelper helper = MoreObjects.toStringHelper(this)
+                .add("name", statementName)
+                .add("declared", declaredRepresentation)
+                .add("effective", effectiveRepresentation);
+        if (argumentName != null) {
+            helper.add("argument", argumentName).add("yin-element", argumentYinElement);
+        }
+        return helper.toString();
+    }
+}