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