Remove RevisionSourceIdentifier
[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.repo.api.SourceIdentifier;
19
20 /**
21  * Namespace key matching criterion.
22  *
23  * @param <K> Key type
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public abstract class NamespaceKeyCriterion<K> {
29     private static final class LatestRevisionModule extends NamespaceKeyCriterion<SourceIdentifier> {
30         private final Unqualified moduleName;
31
32         LatestRevisionModule(final Unqualified moduleName) {
33             this.moduleName = requireNonNull(moduleName);
34         }
35
36         @Override
37         public boolean match(final SourceIdentifier key) {
38             return moduleName.equals(key.name());
39         }
40
41         @Override
42         public SourceIdentifier select(final SourceIdentifier first, final SourceIdentifier second) {
43             return Revision.compare(first.revision(), second.revision()) >= 0 ? first : second;
44         }
45
46         @Override
47         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
48             return toStringHelper.add("moduleName", moduleName.getLocalName());
49         }
50     }
51
52     /**
53      * Return a criterion which selects the latest known revision of a particular module.
54      *
55      * @param moduleName Module name
56      * @return A criterion object.
57      */
58     public static NamespaceKeyCriterion<SourceIdentifier> latestRevisionModule(final Unqualified moduleName) {
59         return new LatestRevisionModule(moduleName);
60     }
61
62     /**
63      * Match a key against this criterion.
64      *
65      * @param key Key to be matched
66      * @return True if the key matches this criterion, false otherwise.
67      */
68     public abstract boolean match(@NonNull K key);
69
70     /**
71      * Select the better match from two candidate keys.
72      *
73      * @param first First key
74      * @param second Second key
75      * @return Selected key, must be either first or second key, by identity.
76      */
77     public abstract K select(@NonNull K first, @NonNull K second);
78
79     @Override
80     public final String toString() {
81         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
82     }
83
84     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
85         return toStringHelper;
86     }
87
88     @Override
89     public final int hashCode() {
90         return super.hashCode();
91     }
92
93     @Override
94     public final boolean equals(final Object obj) {
95         return super.equals(obj);
96     }
97 }