2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.api.meta;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.base.MoreObjects.ToStringHelper;
16 import javax.annotation.concurrent.ThreadSafe;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.common.QName;
22 * Default implementation of the {@link StatementDefinition} contract. Instances of this class should be used as
23 * well-known singletons.
25 * @author Robert Varga
27 * @param <A> Argument type
28 * @param <D> Declared statement representation
29 * @param <E> Effective statement representation
34 public final class DefaultStatementDefinition<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
35 implements StatementDefinition {
36 private final Class<E> effectiveRepresentation;
37 private final Class<D> declaredRepresentation;
38 private final QName statementName;
39 private final @Nullable QName argumentName;
40 private final boolean argumentYinElement;
42 DefaultStatementDefinition(final QName statementName, final Class<D> declaredRepresentation,
43 final Class<E> effectiveRepresentation, final boolean argumentYinElement,
44 final @Nullable QName argumentName) {
45 this.statementName = requireNonNull(statementName);
46 this.declaredRepresentation = requireNonNull(declaredRepresentation);
47 this.effectiveRepresentation = requireNonNull(effectiveRepresentation);
48 this.argumentYinElement = argumentYinElement;
49 this.argumentName = argumentName;
51 checkArgument(declaredRepresentation.isInterface(), "Declared representation %s is not an interface",
52 declaredRepresentation);
53 checkArgument(effectiveRepresentation.isInterface(), "Effective representation %s is not an interface",
54 effectiveRepresentation);
57 public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
58 DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
59 final Class<E> effectiveRepresentation) {
60 return new DefaultStatementDefinition<>(statementName, declaredRepresentation, effectiveRepresentation, false,
64 public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
65 DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
66 final Class<E> effectiveRepresentation, final QName argumentName) {
67 return of(statementName, declaredRepresentation, effectiveRepresentation, argumentName, false);
70 public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
71 DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
72 final Class<E> effectiveRepresentation, final QName argumentName,
73 final boolean argumentYinElement) {
74 return new DefaultStatementDefinition<>(statementName, declaredRepresentation, effectiveRepresentation,
75 argumentYinElement, requireNonNull(argumentName));
79 public QName getStatementName() {
84 public @Nullable QName getArgumentName() {
89 public Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
90 return declaredRepresentation;
94 public Class<? extends EffectiveStatement<?, ?>> getEffectiveRepresentationClass() {
95 return effectiveRepresentation;
99 public boolean isArgumentYinElement() {
100 return argumentYinElement;
104 public String toString() {
105 final ToStringHelper helper = MoreObjects.toStringHelper(this)
106 .add("name", statementName)
107 .add("declared", declaredRepresentation)
108 .add("effective", effectiveRepresentation);
109 if (argumentName != null) {
110 helper.add("argument", argumentName).add("yin-element", argumentYinElement);
112 return helper.toString();