Update model.api.Status design
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / Status.java
1 /*
2  * Copyright (c) 2013 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.model.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNullByDefault;
13 import org.eclipse.jdt.annotation.Nullable;
14
15 /**
16  * Enumeration describing YANG 'status' statement. If no status is specified, the
17  * default is CURRENT.
18  */
19 @NonNullByDefault
20 public enum Status {
21     /**
22      * CURRENT means that the definition is current and valid.
23      */
24     CURRENT("current"),
25     /**
26      * DEPRECATED indicates an obsolete definition, but it permits new/
27      * continued implementation in order to foster interoperability with
28      * older/existing implementations.
29      */
30     DEPRECATED("deprecated"),
31     /**
32      * OBSOLETE means the definition is obsolete and SHOULD NOT be implemented
33      * and/or can be removed from implementations.
34      */
35     OBSOLETE("obsolete");
36
37     private final String argument;
38
39     Status(final String argument) {
40         this.argument = requireNonNull(argument);
41     }
42
43     /**
44      * Returns the YANG {@code status} statement argument value corresponding to this object.
45      *
46      * @return String that corresponds to the YANG {@code status} statement argument
47      */
48     public String argument() {
49         return argument;
50     }
51
52     /**
53      * Return a {@link Status} for specified {@code status} statement argument. This methods returns a  {@code null} for
54      * illegal values. See {@link #ofArgument(String)} for a version which returns non-null and throws an exception for
55      * illegal values.
56      *
57      * @param argument {@code status} statement argument
58      * @return An enumeration value, or {@code null} if specified argument is not valid
59      * @throws NullPointerException if {@code argument} is {@code null}
60      */
61     public static @Nullable Status forArgument(final String argument) {
62         return switch (argument) {
63             case "current" -> CURRENT;
64             case "deprecated" -> DEPRECATED;
65             case "obsolete" -> OBSOLETE;
66             default -> null;
67         };
68     }
69
70     /**
71      * Return a {@link Status} for specified {@code status} statement argument. This methods throws an exception for
72      * illegal values. See {@link #forArgument(String)} for a version which returns a {@code null} instead for illegal
73      * values.
74      *
75      * @param argument {@code status} statement argument
76      * @return An enumeration value, or {@code null} if specified argument is not valid
77      * @throws NullPointerException if {@code argument} is {@code null}
78      * @throws IllegalArgumentException if {@code argument} is not a valid {@code status} statement argument
79      */
80     public static Status ofArgument(final String argument) {
81         final var ret = forArgument(argument);
82         if (ret == null) {
83             throw new IllegalArgumentException("\"" + argument + "\" is not a valid status statement argument");
84         }
85         return ret;
86     }
87 }