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.api.source.SourceIdentifier;
21 * Namespace key matching criterion.
26 public abstract class NamespaceKeyCriterion<K> {
27 private static final class LatestRevisionModule extends NamespaceKeyCriterion<SourceIdentifier> {
28 private final Unqualified moduleName;
30 LatestRevisionModule(final Unqualified moduleName) {
31 this.moduleName = requireNonNull(moduleName);
35 public boolean match(final SourceIdentifier key) {
36 return moduleName.equals(key.name());
40 public SourceIdentifier select(final SourceIdentifier first, final SourceIdentifier second) {
41 return Revision.compare(first.revision(), second.revision()) >= 0 ? first : second;
45 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
46 return toStringHelper.add("moduleName", moduleName.getLocalName());
51 * Return a criterion which selects the latest known revision of a particular module.
53 * @param moduleName Module name
54 * @return A criterion object.
56 public static NamespaceKeyCriterion<SourceIdentifier> latestRevisionModule(final Unqualified moduleName) {
57 return new LatestRevisionModule(moduleName);
61 * Match a key against this criterion.
63 * @param key Key to be matched
64 * @return True if the key matches this criterion, false otherwise.
66 public abstract boolean match(@NonNull K key);
69 * Select the better match from two candidate keys.
71 * @param first First key
72 * @param second Second key
73 * @return Selected key, must be either first or second key, by identity.
75 public abstract K select(@NonNull K first, @NonNull K second);
78 public final String toString() {
79 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
82 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
83 return toStringHelper;
87 public final int hashCode() {
88 return super.hashCode();
92 public final boolean equals(final Object obj) {
93 return super.equals(obj);