2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
10 import static java.util.Objects.requireNonNull;
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;
21 * Namespace key matching criterion.
25 * @author Robert Varga
28 public abstract class NamespaceKeyCriterion<K> {
29 private static final class LatestRevisionModule extends NamespaceKeyCriterion<SourceIdentifier> {
30 private final Unqualified moduleName;
32 LatestRevisionModule(final Unqualified moduleName) {
33 this.moduleName = requireNonNull(moduleName);
37 public boolean match(final SourceIdentifier key) {
38 return moduleName.getLocalName().equals(key.getName());
42 public SourceIdentifier select(final SourceIdentifier first, final SourceIdentifier second) {
43 return Revision.compare(first.getRevision(), second.getRevision()) >= 0 ? first : second;
47 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
48 return toStringHelper.add("moduleName", moduleName.getLocalName());
53 * Return a criterion which selects the latest known revision of a particular module.
55 * @param moduleName Module name
56 * @return A criterion object.
58 public static NamespaceKeyCriterion<SourceIdentifier> latestRevisionModule(final Unqualified moduleName) {
59 return new LatestRevisionModule(moduleName);
63 * Match a key against this criterion.
65 * @param key Key to be matched
66 * @return True if the key matches this criterion, false otherwise.
68 public abstract boolean match(@NonNull K key);
71 * Select the better match from two candidate keys.
73 * @param first First key
74 * @param second Second key
75 * @return Selected key, must be either first or second key, by identity.
77 public abstract K select(@NonNull K first, @NonNull K second);
80 public final String toString() {
81 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
84 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
85 return toStringHelper;
89 public final int hashCode() {
90 return super.hashCode();
94 public final boolean equals(final Object obj) {
95 return super.equals(obj);