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 org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.common.QName;
21 * Default implementation of the {@link StatementDefinition} contract. Instances of this class should be used as
22 * well-known singletons.
24 * @author Robert Varga
26 * @param <A> Argument type
27 * @param <D> Declared statement representation
28 * @param <E> Effective statement representation
32 public final class DefaultStatementDefinition<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
33 implements StatementDefinition {
34 private final Class<E> effectiveRepresentation;
35 private final Class<D> declaredRepresentation;
36 private final QName statementName;
37 private final @Nullable QName argumentName;
38 private final boolean argumentYinElement;
40 DefaultStatementDefinition(final QName statementName, final Class<D> declaredRepresentation,
41 final Class<E> effectiveRepresentation, final boolean argumentYinElement,
42 final @Nullable QName argumentName) {
43 this.statementName = requireNonNull(statementName);
44 this.declaredRepresentation = requireNonNull(declaredRepresentation);
45 this.effectiveRepresentation = requireNonNull(effectiveRepresentation);
46 this.argumentYinElement = argumentYinElement;
47 this.argumentName = argumentName;
49 checkArgument(declaredRepresentation.isInterface(), "Declared representation %s is not an interface",
50 declaredRepresentation);
51 checkArgument(effectiveRepresentation.isInterface(), "Effective representation %s is not an interface",
52 effectiveRepresentation);
55 public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
56 DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
57 final Class<E> effectiveRepresentation) {
58 return new DefaultStatementDefinition<>(statementName, declaredRepresentation, effectiveRepresentation, false,
62 public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
63 DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
64 final Class<E> effectiveRepresentation, final QName argumentName) {
65 return of(statementName, declaredRepresentation, effectiveRepresentation, argumentName, false);
68 public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
69 DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
70 final Class<E> effectiveRepresentation, final QName argumentName,
71 final boolean argumentYinElement) {
72 return new DefaultStatementDefinition<>(statementName, declaredRepresentation, effectiveRepresentation,
73 argumentYinElement, requireNonNull(argumentName));
77 public QName getStatementName() {
82 public @Nullable QName getArgumentName() {
87 public Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
88 return declaredRepresentation;
92 public Class<? extends EffectiveStatement<?, ?>> getEffectiveRepresentationClass() {
93 return effectiveRepresentation;
97 public boolean isArgumentYinElement() {
98 return argumentYinElement;
102 public String toString() {
103 final ToStringHelper helper = MoreObjects.toStringHelper(this)
104 .add("name", statementName)
105 .add("declared", declaredRepresentation)
106 .add("effective", effectiveRepresentation);
107 if (argumentName != null) {
108 helper.add("argument", argumentName).add("yin-element", argumentYinElement);
110 return helper.toString();