Define a feature-parent
[yangtools.git] / parser / 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 org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.Revision;
17 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
18 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
19
20 /**
21  * Namespace key matching criterion.
22  *
23  * @param <K> Key type
24  */
25 @Beta
26 public abstract class NamespaceKeyCriterion<K> {
27     private static final class LatestRevisionModule extends NamespaceKeyCriterion<SourceIdentifier> {
28         private final Unqualified moduleName;
29
30         LatestRevisionModule(final Unqualified moduleName) {
31             this.moduleName = requireNonNull(moduleName);
32         }
33
34         @Override
35         public boolean match(final SourceIdentifier key) {
36             return moduleName.equals(key.name());
37         }
38
39         @Override
40         public SourceIdentifier select(final SourceIdentifier first, final SourceIdentifier second) {
41             return Revision.compare(first.revision(), second.revision()) >= 0 ? first : second;
42         }
43
44         @Override
45         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
46             return toStringHelper.add("moduleName", moduleName.getLocalName());
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<SourceIdentifier> latestRevisionModule(final Unqualified 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 }