Populate model/ hierarchy
[yangtools.git] / model / yang-model-ri / src / main / java / org / opendaylight / yangtools / yang / model / ri / stmt / impl / eff / ImportEffectiveStatementImpl.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.model.ri.stmt.impl.eff;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.collect.ImmutableList;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.SemVer;
16 import org.opendaylight.yangtools.yang.common.Revision;
17 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
21 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.spi.meta.AbstractDeclaredEffectiveStatement.DefaultArgument.WithSubstatements;
24 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.DocumentedNodeMixin;
25
26 public final class ImportEffectiveStatementImpl extends WithSubstatements<String, ImportStatement>
27         implements ImportEffectiveStatement, ModuleImport, DocumentedNodeMixin<String, ImportStatement> {
28     private final @Nullable Revision revision;
29     private final @Nullable SemVer semVer;
30
31     public ImportEffectiveStatementImpl(final ImportStatement declared,
32             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
33             final @NonNull SourceIdentifier importedSource) {
34         super(declared, substatements);
35         this.revision = importedSource.getRevision().orElse(null);
36         this.semVer = importedSource instanceof SemVerSourceIdentifier
37             ? ((SemVerSourceIdentifier) importedSource).getSemanticVersion().orElse(null)
38                 : null;
39     }
40
41     @Override
42     public Optional<Revision> getRevision() {
43         return Optional.ofNullable(revision);
44     }
45
46     @Override
47     public Optional<SemVer> getSemanticVersion() {
48         return Optional.ofNullable(semVer);
49     }
50
51     @Override
52     public String getPrefix() {
53         return getDeclared().getPrefix().argument();
54     }
55
56     @Override
57     public ImportEffectiveStatement asEffectiveStatement() {
58         return this;
59     }
60
61     @Override
62     public String toString() {
63         return MoreObjects.toStringHelper(this).omitNullValues()
64                 .add("moduleName", getModuleName())
65                 .add("revision", revision)
66                 .add("version", semVer)
67                 .add("prefix", getPrefix())
68                 .add("description", getDescription().orElse(null))
69                 .add("reference", getReference().orElse(null))
70                 .toString();
71     }
72 }