Populate model/ hierarchy
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / meta / DefaultStatementDefinition.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.api.meta;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.QName;
18
19 /**
20  * Default implementation of the {@link StatementDefinition} contract. Instances of this class should be used as
21  * well-known singletons.
22  *
23  * @author Robert Varga
24  *
25  * @param <A> Argument type
26  * @param <D> Declared statement representation
27  * @param <E> Effective statement representation
28  */
29 @Beta
30 @NonNullByDefault
31 public final class DefaultStatementDefinition<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
32         extends AbstractStatementDefinition {
33     private final Class<E> effectiveRepresentation;
34     private final Class<D> declaredRepresentation;
35
36     DefaultStatementDefinition(final QName statementName, final Class<D> declaredRepresentation,
37             final Class<E> effectiveRepresentation, final boolean argumentYinElement,
38             final @Nullable QName argumentName) {
39         super(statementName, argumentYinElement, argumentName);
40         this.declaredRepresentation = requireNonNull(declaredRepresentation);
41         this.effectiveRepresentation = requireNonNull(effectiveRepresentation);
42
43         checkArgument(declaredRepresentation.isInterface(), "Declared representation %s is not an interface",
44             declaredRepresentation);
45         checkArgument(effectiveRepresentation.isInterface(), "Effective representation %s is not an interface",
46             effectiveRepresentation);
47     }
48
49     public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
50             DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
51                     final Class<E> effectiveRepresentation) {
52         return new DefaultStatementDefinition<>(statementName, declaredRepresentation, effectiveRepresentation, false,
53                 null);
54     }
55
56     public static <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
57             DefaultStatementDefinition<A, D, E> of(final QName statementName, final Class<D> declaredRepresentation,
58                     final Class<E> effectiveRepresentation, final QName argumentName) {
59         return of(statementName, declaredRepresentation, effectiveRepresentation, argumentName, false);
60     }
61
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                     final boolean argumentYinElement) {
66         return new DefaultStatementDefinition<>(statementName, declaredRepresentation, effectiveRepresentation,
67                 argumentYinElement, requireNonNull(argumentName));
68     }
69
70     @Override
71     public Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
72         return declaredRepresentation;
73     }
74
75     @Override
76     public Class<? extends EffectiveStatement<?, ?>> getEffectiveRepresentationClass() {
77         return effectiveRepresentation;
78     }
79
80     @Override
81     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
82         return super.addToStringAttributes(helper)
83                 .add("declared", declaredRepresentation)
84                 .add("effective", effectiveRepresentation);
85     }
86 }