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.common.Revision;
17 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
20 * Namespace key matching criterion.
24 * @author Robert Varga
27 public abstract class NamespaceKeyCriterion<K> {
28 private static final class LatestRevisionModule extends NamespaceKeyCriterion<ModuleIdentifier> {
29 private final String moduleName;
31 LatestRevisionModule(final String moduleName) {
32 this.moduleName = requireNonNull(moduleName);
36 public boolean match(final ModuleIdentifier key) {
37 return moduleName.equals(key.getName());
41 public ModuleIdentifier select(final ModuleIdentifier first, final ModuleIdentifier second) {
42 return Revision.compare(first.getRevision(), second.getRevision()) >= 0 ? first : second;
46 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
47 return toStringHelper.add("moduleName", moduleName);
52 * Return a criterion which selects the latest known revision of a particular module.
54 * @param moduleName Module name
55 * @return A criterion object.
57 public static NamespaceKeyCriterion<ModuleIdentifier> latestRevisionModule(final String moduleName) {
58 return new LatestRevisionModule(moduleName);
62 * Match a key against this criterion.
64 * @param key Key to be matched
65 * @return True if the key matches this criterion, false otherwise.
67 public abstract boolean match(@Nonnull K key);
70 * Select the better match from two candidate keys.
72 * @param first First key
73 * @param second Second key
74 * @return Selected key, must be either first or second key, by identity.
76 public abstract K select(@Nonnull K first, @Nonnull K second);
79 public final String toString() {
80 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
83 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
84 return toStringHelper;
88 public final int hashCode() {
89 return super.hashCode();
93 public final boolean equals(final Object obj) {
94 return super.equals(obj);