BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / NamespaceKeyCriterion.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.spi.meta;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
17
18 /**
19  * Namespace key matching criterion.
20  *
21  * @param <K> Key type
22  *
23  * @author Robert Varga
24  */
25 @Beta
26 public abstract class NamespaceKeyCriterion<K> {
27     private static final class LatestRevisionModule extends NamespaceKeyCriterion<ModuleIdentifier> {
28         private final String moduleName;
29
30         LatestRevisionModule(final String moduleName) {
31             this.moduleName = requireNonNull(moduleName);
32         }
33
34         @Override
35         public boolean match(final ModuleIdentifier key) {
36             return moduleName.equals(key.getName());
37         }
38
39         @Override
40         public ModuleIdentifier select(final ModuleIdentifier first, final ModuleIdentifier second) {
41             return ModuleIdentifier.compareRevisions(first.getRevision(), second.getRevision()) >= 0 ? first : second;
42         }
43
44         @Override
45         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
46             return toStringHelper.add("moduleName", moduleName);
47         }
48     }
49
50     /**
51      * Return a criterion which selects the latest known revision of a particular module.
52      *
53      * @param moduleName Module name
54      * @return A criterion object.
55      */
56     public static NamespaceKeyCriterion<ModuleIdentifier> latestRevisionModule(final String moduleName) {
57         return new LatestRevisionModule(moduleName);
58     }
59
60     /**
61      * Match a key against this criterion.
62      *
63      * @param key Key to be matched
64      * @return True if the key matches this criterion, false otherwise.
65      */
66     public abstract boolean match(@Nonnull K key);
67
68     /**
69      * Select the better match from two candidate keys.
70      *
71      * @param first First key
72      * @param second Second key
73      * @return Selected key, must be either first or second key, by identity.
74      */
75     public abstract K select(@Nonnull K first, @Nonnull K second);
76
77     @Override
78     public final String toString() {
79         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
80     }
81
82     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
83         return toStringHelper;
84     }
85
86     @Override
87     public final int hashCode() {
88         return super.hashCode();
89     }
90
91     @Override
92     public final boolean equals(final Object obj) {
93         return super.equals(obj);
94     }
95 }