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