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 javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
19 * Namespace key matching criterion.
23 * @author Robert Varga
26 public abstract class NamespaceKeyCriterion<K> {
27 private static final class LatestRevisionModule extends NamespaceKeyCriterion<ModuleIdentifier> {
28 private final String moduleName;
30 LatestRevisionModule(final String moduleName) {
31 this.moduleName = requireNonNull(moduleName);
35 public boolean match(final ModuleIdentifier key) {
36 return moduleName.equals(key.getName());
40 public ModuleIdentifier select(final ModuleIdentifier first, final ModuleIdentifier second) {
41 return ModuleIdentifier.compareRevisions(first.getRevision(), second.getRevision()) >= 0 ? first : second;
45 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
46 return toStringHelper.add("moduleName", moduleName);
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<ModuleIdentifier> latestRevisionModule(final String 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);