2 * Copyright (c) 2015 Cisco Systems, Inc. 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.parser.rfc7950.stmt.module;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import com.google.common.collect.Maps;
16 import java.util.Map.Entry;
17 import java.util.Objects;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
30 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
32 final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleStatement>
33 implements ModuleEffectiveStatement {
34 private final Map<String, ModuleEffectiveStatement> prefixToModule;
35 private final Map<QNameModule, String> namespaceToPrefix;
36 private final @NonNull QNameModule qnameModule;
38 ModuleEffectiveStatementImpl(
39 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
41 qnameModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx));
43 final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
44 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
45 prefixToModuleBuilder.put(localPrefix, this);
47 streamEffectiveSubstatements(ImportEffectiveStatement.class)
48 .map(imp -> imp.findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get())
50 final StmtContext<?, ?, ?> importedCtx =
51 verifyNotNull(ctx.getFromNamespace(ImportPrefixToModuleCtx.class, prefix),
52 "Failed to resolve prefix %s", prefix);
53 prefixToModuleBuilder.put(prefix, (ModuleEffectiveStatement) importedCtx.buildEffective());
55 prefixToModule = prefixToModuleBuilder.build();
57 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size() + 1);
58 tmp.put(qnameModule, localPrefix);
59 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
60 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
62 namespaceToPrefix = ImmutableMap.copyOf(tmp);
66 public @NonNull QNameModule localQNameModule() {
71 public @NonNull QNameModule getQNameModule() {
76 @SuppressWarnings("unchecked")
77 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
78 final @NonNull Class<N> namespace) {
79 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
80 return Optional.of((Map<K, V>) prefixToModule);
82 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
83 return Optional.of((Map<K, V>) namespaceToPrefix);
85 return super.getNamespaceContents(namespace);
89 public int hashCode() {
92 result = prime * result + Objects.hashCode(getName());
93 result = prime * result + Objects.hashCode(getYangVersion());
94 result = prime * result + Objects.hashCode(qnameModule);
99 public boolean equals(final Object obj) {
103 if (!(obj instanceof ModuleEffectiveStatementImpl)) {
106 ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
107 if (!Objects.equals(getName(), other.getName())) {
110 if (!qnameModule.equals(other.qnameModule)) {
113 if (!Objects.equals(getYangVersion(), other.getYangVersion())) {