Migrate yang-common to use JDT annotations
[yangtools.git] / yang / 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 com.google.common.annotations.Beta;
13 import com.google.common.collect.Maps;
14 import java.util.Arrays;
15 import java.util.Map;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * Enumeration of supported YANG versions.
21  *
22  * @author Robert Varga
23  */
24 @Beta
25 @NonNullByDefault
26 public enum YangVersion {
27     /**
28      * Version 1, as defined in RFC6020.
29      */
30     VERSION_1("1", "RFC6020"),
31     /**
32      * Version 1.1, as defined in RFC7950.
33      */
34     VERSION_1_1("1.1", "RFC7950");
35
36     private static final Map<String, YangVersion> YANG_VERSION_MAP = Maps.uniqueIndex(Arrays.asList(values()),
37         YangVersion::toString);
38
39     private final String str;
40     private String reference;
41
42     YangVersion(final String str, final String reference) {
43         this.str = requireNonNull(str);
44         this.reference = requireNonNull(reference);
45     }
46
47     /**
48      * Parse a YANG version from its textual representation.
49      *
50      * @param str String to parse
51      * @return YANG version
52      * @throws NullPointerException if the string is null
53      */
54     public static Optional<YangVersion> parse(final String str) {
55         return Optional.ofNullable(YANG_VERSION_MAP.get(requireNonNull(str)));
56     }
57
58     /**
59      * Return the normative reference defining this YANG version.
60      *
61      * @return Normative reference.
62      */
63     public String getReference() {
64         return reference;
65     }
66
67     @Override
68     public String toString() {
69         return str;
70     }
71 }