Bump Xtend to 2.36.0
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / YangVersion.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.yangtools.yang.common;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15
16 /**
17  * Enumeration of supported YANG versions.
18  */
19 public enum YangVersion {
20     /**
21      * Version 1, as defined in RFC6020.
22      */
23     VERSION_1("1", "RFC6020"),
24     /**
25      * Version 1.1, as defined in RFC7950.
26      */
27     VERSION_1_1("1.1", "RFC7950");
28
29     private final @NonNull String str;
30     private final @NonNull String reference;
31
32     YangVersion(final @NonNull String str, final @NonNull String reference) {
33         this.str = requireNonNull(str);
34         this.reference = requireNonNull(reference);
35     }
36
37     /**
38      * Parse a YANG version from its textual representation.
39      *
40      * @param str String to parse
41      * @return YANG version, or {@code null}
42      * @throws NullPointerException if the string is {@code null}
43      */
44     public static @Nullable YangVersion forString(final @NonNull String str) {
45         return switch (requireNonNull(str)) {
46             case "1" -> VERSION_1;
47             case "1.1" -> VERSION_1_1;
48             default -> null;
49         };
50     }
51
52     /**
53      * Parse a YANG version from its textual representation.
54      *
55      * @param str String to parse
56      * @return YANG version
57      * @throws NullPointerException if the string is {@code null}
58      * @throws IllegalArgumentException if the string is not recognized
59      */
60     public static @NonNull YangVersion ofString(final @NonNull String str) {
61         final var ret = forString(str);
62         if (ret != null) {
63             return ret;
64         }
65         throw new IllegalArgumentException("Invalid YANG version " + str);
66     }
67
68     /**
69      * Parse a YANG version from its textual representation.
70      *
71      * @param str String to parse
72      * @return An Optional YANG version
73      * @throws NullPointerException if the string is {@code null}
74      * @deprecated Use {@link #forString(String)} or {@link #ofString(String)}
75      */
76     @Deprecated(since = "11.0.0", forRemoval = true)
77     public static Optional<YangVersion> parse(final @NonNull String str) {
78         return Optional.ofNullable(forString(str));
79     }
80
81     /**
82      * Return the normative reference defining this YANG version.
83      *
84      * @return Normative reference.
85      */
86     public @NonNull String reference() {
87         return reference;
88     }
89
90     /**
91      * Return the normative reference defining this YANG version.
92      *
93      * @return Normative reference.
94      * @deprecated Use {@link #reference()} instead
95      */
96     @Deprecated(since = "11.0.0", forRemoval = true)
97     public @NonNull String getReference() {
98         return reference;
99     }
100
101     @Override
102     public @NonNull String toString() {
103         return str;
104     }
105 }