2 * Copyright (c) 2019 PANTHEON.tech, 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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ethertypes.rev190304;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
14 import com.google.common.base.CharMatcher;
15 import java.util.Comparator;
16 import java.util.EnumMap;
17 import java.util.Objects;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.Uint16;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ethertypes.rev190304.Ethertype.Enumeration;
24 * Utility methods for dealing with {@link Ethertype}.
26 public final class EthertypeUtils {
27 private static final CharMatcher DIGITS = CharMatcher.inRange('0', '9');
28 private static final EnumMap<Enumeration, Ethertype> ENUM_ETHERTYPES;
31 final EnumMap<Enumeration, Ethertype> map = new EnumMap<>(Enumeration.class);
32 for (Enumeration value : Enumeration.values()) {
33 verify(map.put(value, new Ethertype(value)) == null);
35 ENUM_ETHERTYPES = map;
38 private EthertypeUtils() {
39 //Exists only to defeat instantiation.
42 // TODO: the canonical representation is undefined. We may consider turning integers into enum values, but at this
43 // point it would confuse people and would not work well with JSON/XML parser output.
44 public static @NonNull Ethertype getDefaultInstance(final String defaultValue) {
45 final int length = defaultValue.length();
46 if (length > 0 && length < 6 && DIGITS.matchesAllOf(defaultValue)) {
47 final int value = Integer.parseInt(defaultValue);
49 return new Ethertype(Uint16.valueOf(value));
52 // Fall through and interpret as a string
55 final Optional<Enumeration> known = Enumeration.forName(defaultValue);
56 checkArgument(known.isPresent(), "Unknown ethertype %s", defaultValue);
57 return verifyNotNull(ENUM_ETHERTYPES.get(known.get()));
61 * Semantically compare two {@link Ethertype}s based on their numeric value, according to
62 * {@link Comparator#compare(Object, Object)} contract.
64 * @param first First Ethertype
65 * @param second Second Ethertype
66 * @return a negative integer, zero, or a positive integer as the
67 * first argument is less than, equal to, or greater than the
69 * @throws NullPointerException if any argument is null
71 public static int compareValue(final Ethertype first, final Ethertype second) {
72 return Integer.compare(extractValue(first), extractValue(second));
76 * Determine semantic equality of two {@link Ethertype}s based on their numeric value, according to
77 * {@link Objects#equals(Object, Object)} contract. This is distinct from {@link Ethertype#equals(Object)}, which
78 * does not perform semantic comparison.
80 * @param first First Ethertype
81 * @param second Second Ethertype
82 * @return True if the two Ethertypes are equal, false otherwise.
84 public static boolean equalValue(final Ethertype first, final Ethertype second) {
85 return first == second || first != null && extractValue(first) == extractValue(second);
89 * Determine semantic hash value of a {@link Ethertype}s based on its numeric value, according to
90 * {@link Object#hashCode()} contract. This is distinct from {@link Ethertype#hashCode()}, which does not perform
91 * semantic hashing. Unlike {@link Objects#hashCode(Object)}, this method does not return 0 for null objects.
93 * @param value Ethertype object
94 * @return Specified object's semantic hashCode
96 public static int hashValue(final Ethertype value) {
97 return value == null ? 65536 : extractValue(value);
100 private static int extractValue(final Ethertype type) {
101 final Enumeration known = type.getEnumeration();
102 return known != null ? known.getIntValue() : verifyNotNull(type.getUint16()).intValue();