Remove @ThreadSafe annotations
[yangtools.git] / yang / 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;
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;
19
20 /**
21  * Default implementation of the {@link StatementDefinition} contract. Instances of this class should be used as
22  * well-known singletons.
23  *
24  * @author Robert Varga
25  *
26  * @param <A> Argument type
27  * @param <D> Declared statement representation
28  * @param <E> Effective statement representation
29  */
30 @Beta
31 @NonNullByDefault
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;
39
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;
48
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);
53     }
54
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,
59                 null);
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         return of(statementName, declaredRepresentation, effectiveRepresentation, argumentName, false);
66     }
67
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));
74     }
75
76     @Override
77     public QName getStatementName() {
78         return statementName;
79     }
80
81     @Override
82     public @Nullable QName getArgumentName() {
83         return argumentName;
84     }
85
86     @Override
87     public Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
88         return declaredRepresentation;
89     }
90
91     @Override
92     public Class<? extends EffectiveStatement<?, ?>> getEffectiveRepresentationClass() {
93         return effectiveRepresentation;
94     }
95
96     @Override
97     public boolean isArgumentYinElement() {
98         return argumentYinElement;
99     }
100
101     @Override
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);
109         }
110         return helper.toString();
111     }
112 }