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