b80fe4d5b701b12c4d60bff635128b3dbefc385c
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / notification / NotificationEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.parser.rfc7950.stmt.notification;
9
10 import com.google.common.collect.ImmutableSet;
11 import java.lang.invoke.MethodHandles;
12 import java.lang.invoke.VarHandle;
13 import java.util.Collection;
14 import java.util.LinkedHashSet;
15 import java.util.Objects;
16 import java.util.Set;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
21 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationStatement;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDocumentedDataNodeContainer;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30
31 final class NotificationEffectiveStatementImpl
32         extends AbstractEffectiveDocumentedDataNodeContainer<QName, NotificationStatement>
33         implements NotificationDefinition, NotificationEffectiveStatement {
34     private static final VarHandle MUST_CONSTRAINTS;
35
36     static {
37         try {
38             MUST_CONSTRAINTS = MethodHandles.lookup().findVarHandle(
39                 NotificationEffectiveStatementImpl.class, "mustConstraints", ImmutableSet.class);
40         } catch (NoSuchFieldException | IllegalAccessException e) {
41             throw new ExceptionInInitializerError(e);
42         }
43     }
44
45     private final @NonNull QName qname;
46     private final @NonNull SchemaPath path;
47     private final ImmutableSet<AugmentationSchemaNode> augmentations;
48     private final boolean augmenting;
49     private final boolean addedByUses;
50
51     @SuppressWarnings("unused")
52     private volatile ImmutableSet<MustDefinition> mustConstraints;
53
54     NotificationEffectiveStatementImpl(
55             final StmtContext<QName, NotificationStatement, NotificationEffectiveStatement> ctx) {
56         super(ctx);
57         this.qname = ctx.coerceStatementArgument();
58         this.path = ctx.getSchemaPath().get();
59
60         // initSubstatementCollections
61         final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
62         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
63             if (effectiveStatement instanceof AugmentationSchemaNode) {
64                 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
65                 augmentationsInit.add(augmentationSchema);
66             }
67         }
68         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
69
70         // initCopyType
71         final CopyHistory copyTypesFromOriginal = ctx.getCopyHistory();
72         if (copyTypesFromOriginal.contains(CopyType.ADDED_BY_USES_AUGMENTATION)) {
73             this.augmenting = true;
74             this.addedByUses = true;
75         } else {
76             this.augmenting = copyTypesFromOriginal.contains(CopyType.ADDED_BY_AUGMENTATION);
77             this.addedByUses = copyTypesFromOriginal.contains(CopyType.ADDED_BY_USES);
78         }
79     }
80
81     @Override
82     public QName getQName() {
83         return qname;
84     }
85
86     @Override
87     public SchemaPath getPath() {
88         return path;
89     }
90
91     @Override
92     public Collection<? extends MustDefinition> getMustConstraints() {
93         return derivedSet(MUST_CONSTRAINTS, MustDefinition.class);
94     }
95
96     @Override
97     public Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
98         return augmentations;
99     }
100
101     @Deprecated
102     @Override
103     public boolean isAugmenting() {
104         return augmenting;
105     }
106
107     @Deprecated
108     @Override
109     public boolean isAddedByUses() {
110         return addedByUses;
111     }
112
113     @Override
114     public int hashCode() {
115         final int prime = 31;
116         int result = 1;
117         result = prime * result + Objects.hashCode(qname);
118         result = prime * result + Objects.hashCode(path);
119         return result;
120     }
121
122     @Override
123     public boolean equals(final Object obj) {
124         if (this == obj) {
125             return true;
126         }
127         if (obj == null) {
128             return false;
129         }
130         if (getClass() != obj.getClass()) {
131             return false;
132         }
133         final NotificationEffectiveStatementImpl other = (NotificationEffectiveStatementImpl) obj;
134         return Objects.equals(qname, other.qname) && Objects.equals(path, other.path);
135     }
136
137     @Override
138     public String toString() {
139         return NotificationEffectiveStatementImpl.class.getSimpleName() + "[qname=" + qname + ", path=" + path + "]";
140     }
141 }