Future-proof OvsdbTypesIdResolver
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / notation / Version.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, 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
9 package org.opendaylight.ovsdb.lib.notation;
10
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13
14 /**
15  * This class represents a version according to RFC 7047
16  * The default implementation assumes the left-most digit is most significant when performing comparisons
17  * @see <a href="http://tools.ietf.org/html/rfc7047#section-3.1">RFC7047 Section 3.1</a>
18  */
19 public class Version implements Comparable<Version> {
20
21     int major;
22     int minor;
23     int patch;
24
25     private static final String FORMAT = "(\\d+)\\.(\\d+)\\.(\\d+)";
26     private static final Pattern PATTERN = Pattern.compile(Version.FORMAT);
27
28     public Version(int major, int minor, int patch) {
29         this.major = major;
30         this.minor = minor;
31         this.patch = patch;
32     }
33
34     public static final Version NULL = new Version(0,0,0);
35     public static final String NULL_VERSION_STRING = "0.0.0";
36
37     public static Version fromString(String version) {
38         final Matcher matcher = Version.PATTERN.matcher(version);
39         if (!matcher.find()) {
40             throw new IllegalArgumentException("<" + version + "> does not match format " + Version.FORMAT);
41         }
42         int major = Integer.valueOf(matcher.group(1));
43         int minor = Integer.valueOf(matcher.group(2));
44         int patch = Integer.valueOf(matcher.group(3));
45         return new Version(major, minor, patch);
46     }
47
48     public String toString() {
49         return "" + major + "." + minor + "." + patch;
50     }
51
52     public int getMajor() {
53         return major;
54     }
55
56     public void setMajor(int major) {
57         this.major = major;
58     }
59
60     public int getMinor() {
61         return minor;
62     }
63
64     public void setMinor(int minor) {
65         this.minor = minor;
66     }
67
68     public int getPatch() {
69         return patch;
70     }
71
72     public void setPatch(int patch) {
73         this.patch = patch;
74     }
75
76
77     // ToDo: While format is X.X.X semantics are schema dependent.
78     // Therefore we should allow equals to be overridden by the schema
79     @Override
80     public boolean equals(Object object) {
81         if (this == object) {
82             return true;
83         }
84         if (object == null || getClass() != object.getClass()) {
85             return false;
86         }
87
88         Version version = (Version) object;
89
90         if (major != version.major) {
91             return false;
92         }
93         if (minor != version.minor) {
94             return false;
95         }
96         if (patch != version.patch) {
97             return false;
98         }
99
100         return true;
101     }
102
103     @Override
104     public int hashCode() {
105         int result = major;
106         result = 31 * result + minor;
107         result = 31 * result + patch;
108         return result;
109     }
110
111     // ToDo: While format is X.X.X semantics are schema dependent
112     // Therefore we should allow compareTo to be overridden by the schema
113     @Override
114     public int compareTo(Version version) {
115         if (this.equals(version)) {
116             return 0;
117         }
118         if (this.major > version.major) {
119             return 1;
120         }
121         if (this.major < version.major) {
122             return -1;
123         }
124         // major is equal
125         if (this.minor > version.minor) {
126             return 1;
127         }
128         if (this.minor < version.minor) {
129             return -1;
130         }
131         // minor is equal
132         if (this.patch > version.patch) {
133             return 1;
134         }
135         // must be less than
136         return -1;
137     }
138 }