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