Eliminate YangModelDependencyInfo
[yangtools.git] / parser / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / RevisionDependencyResolver.java
1 /*
2  * Copyright (c) 2016 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.repo;
9
10 import java.util.Collection;
11 import java.util.Map;
12 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
13 import org.opendaylight.yangtools.yang.model.api.source.SourceDependency;
14 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
15 import org.opendaylight.yangtools.yang.model.spi.source.SourceInfo;
16 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
17
18 final class RevisionDependencyResolver extends DependencyResolver {
19     RevisionDependencyResolver(final Map<SourceIdentifier, SourceInfo> depInfo) {
20         super(depInfo);
21     }
22
23     @Override
24     YangParserConfiguration parserConfig() {
25         return YangParserConfiguration.DEFAULT;
26     }
27
28     @Override
29     boolean isKnown(final Collection<SourceIdentifier> haystack, final SourceDependency dependency) {
30         // Quick lookup
31         return haystack.contains(new SourceIdentifier(dependency.name(), dependency.revision()))
32             // Slow revision-less walk
33             || dependency.revision() == null && findWildcard(haystack, dependency.name()) != null;
34     }
35
36     private static SourceIdentifier findWildcard(final Collection<SourceIdentifier> haystack,
37             final Unqualified needle) {
38         for (var sourceId : haystack) {
39             if (needle.equals(sourceId.name())) {
40                 return sourceId;
41             }
42         }
43         return null;
44     }
45 }