Introduce Identifiables
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / util / YangSourceFromDependencyInfoResolver.java
1 /*
2  * Copyright (c) 2014 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/eplv10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.impl.util;
9
10 import java.util.Map;
11 import java.util.Map.Entry;
12
13 import org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier;
14
15 import com.google.common.base.Optional;
16 import com.google.common.collect.ImmutableMap;
17
18 public final class YangSourceFromDependencyInfoResolver extends YangSourceContextResolver {
19
20     private final Map<SourceIdentifier, YangModelDependencyInfo> dependencyInfo;
21
22     public YangSourceFromDependencyInfoResolver(final Map<SourceIdentifier, YangModelDependencyInfo> moduleDependencies) {
23         dependencyInfo = ImmutableMap.copyOf(moduleDependencies);
24     }
25
26     @Override
27     public Optional<YangModelDependencyInfo> getDependencyInfo(final SourceIdentifier identifier) {
28         if (identifier.getRevision() != null) {
29             return Optional.fromNullable(dependencyInfo.get(identifier));
30         }
31         YangModelDependencyInfo potential = dependencyInfo.get(identifier);
32         if (potential == null) {
33             for (Entry<SourceIdentifier, YangModelDependencyInfo> newPotential : dependencyInfo.entrySet()) {
34                 String newPotentialName = newPotential.getKey().getName();
35
36                 if (newPotentialName.equals(identifier.getName())) {
37                     String newPotentialRevision = newPotential.getKey().getRevision();
38                     if (potential == null || 1 == newPotentialRevision.compareTo(potential.getFormattedRevision())) {
39                         potential = newPotential.getValue();
40                     }
41                 }
42             }
43         }
44         return Optional.fromNullable(potential);
45     }
46
47     @Override
48     public YangSourceContext resolveContext() {
49         for (SourceIdentifier source : dependencyInfo.keySet()) {
50             resolveSource(source);
51         }
52         return createSourceContext();
53     }
54 }