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