2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.lib.notation;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
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>
19 public class Version implements Comparable<Version> {
25 private static final String FORMAT = "(\\d+)\\.(\\d+)\\.(\\d+)";
26 private static final Pattern PATTERN = Pattern.compile(Version.FORMAT);
28 public Version(int major, int minor, int patch) {
34 public static final Version NULL = new Version(0,0,0);
35 public static final String NULL_VERSION_STRING = "0.0.0";
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);
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);
48 public String toString() {
49 return "" + major + "." + minor + "." + patch;
52 public int getMajor() {
56 public void setMajor(int major) {
60 public int getMinor() {
64 public void setMinor(int minor) {
68 public int getPatch() {
72 public void setPatch(int patch) {
77 // ToDo: While format is X.X.X semantics are schema dependent.
78 // Therefore we should allow equals to be overridden by the schema
80 public boolean equals(Object object) {
84 if (object == null || getClass() != object.getClass()) {
88 Version version = (Version) object;
90 if (major != version.major) {
93 if (minor != version.minor) {
96 if (patch != version.patch) {
104 public int hashCode() {
106 result = 31 * result + minor;
107 result = 31 * result + patch;
111 // ToDo: While format is X.X.X semantics are schema dependent
112 // Therefore we should allow compareTo to be overridden by the schema
114 public int compareTo(Version version) {
115 if (this.equals(version)) {
118 if (this.major > version.major) {
121 if (this.major < version.major) {
125 if (this.minor > version.minor) {
128 if (this.minor < version.minor) {
132 if (this.patch > version.patch) {