blob: 06e9e206304f08b5cdc720fc0faaf6f3b57922a6 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.core;
tom53efab52014-10-07 17:43:48 -070017
18import java.util.Objects;
19
Thomas Vachuska045c01d2014-12-04 00:18:06 -080020import static com.google.common.base.Preconditions.checkArgument;
21import static com.google.common.base.Strings.isNullOrEmpty;
tom53efab52014-10-07 17:43:48 -070022import static java.lang.Integer.parseInt;
23
24/**
25 * Representation of the product version.
26 */
27public final class Version {
28
Thomas Vachuska93d944c2015-06-16 09:48:55 -070029 public static final String FORMAT_MINIMAL = "%d.%d";
Thomas Vachuska045c01d2014-12-04 00:18:06 -080030 public static final String FORMAT_SHORT = "%d.%d.%s";
Thomas Vachuska93d944c2015-06-16 09:48:55 -070031 public static final String FORMAT_LONG = "%d.%d.%s.%s";
Thomas Vachuska045c01d2014-12-04 00:18:06 -080032
33 private static final String NEGATIVE = "Version segment cannot be negative";
Thomas Vachuskaafb11da2015-06-16 10:01:13 -070034 public static final String TOO_SHORT = "Version must have at least major and minor numbers";
tom53efab52014-10-07 17:43:48 -070035
36 private final int major;
37 private final int minor;
Thomas Vachuska045c01d2014-12-04 00:18:06 -080038 private final String patch;
tom53efab52014-10-07 17:43:48 -070039 private final String build;
40
41 private final String format;
42
43 // Creates a new version descriptor
Thomas Vachuska045c01d2014-12-04 00:18:06 -080044 private Version(int major, int minor, String patch, String build) {
tom53efab52014-10-07 17:43:48 -070045 this.major = major;
46 this.minor = minor;
47 this.patch = patch;
48 this.build = build;
Thomas Vachuska93d944c2015-06-16 09:48:55 -070049 this.format =
50 isNullOrEmpty(patch) ?
51 String.format(FORMAT_MINIMAL, major, minor) :
52 (isNullOrEmpty(build) ?
53 String.format(FORMAT_SHORT, major, minor, patch) :
54 String.format(FORMAT_LONG, major, minor, patch, build));
tom53efab52014-10-07 17:43:48 -070055 }
56
57
58 /**
59 * Creates a new version from the specified constituent numbers.
60 *
61 * @param major major version number
Yuta HIGUCHIc7aa5072015-02-02 15:01:02 -080062 * @param minor minor version number
Thomas Vachuska045c01d2014-12-04 00:18:06 -080063 * @param patch version patch segment
64 * @param build optional build string
tom53efab52014-10-07 17:43:48 -070065 * @return version descriptor
66 */
Thomas Vachuska045c01d2014-12-04 00:18:06 -080067 public static Version version(int major, int minor, String patch, String build) {
Thomas Vachuskaafb11da2015-06-16 10:01:13 -070068 checkArgument(major >= 0, NEGATIVE);
69 checkArgument(minor >= 0, NEGATIVE);
tom53efab52014-10-07 17:43:48 -070070 return new Version(major, minor, patch, build);
71 }
72
73 /**
74 * Creates a new version by parsing the specified string.
75 *
76 * @param string version string
77 * @return version descriptor
78 */
79 public static Version version(String string) {
80 String[] fields = string.split("[.-]");
Thomas Vachuskaafb11da2015-06-16 10:01:13 -070081 checkArgument(fields.length >= 2, TOO_SHORT);
tom53efab52014-10-07 17:43:48 -070082 return new Version(parseInt(fields[0]), parseInt(fields[1]),
Thomas Vachuska93d944c2015-06-16 09:48:55 -070083 fields.length >= 3 ? fields[2] : null,
84 fields.length >= 4 ? fields[3] : null);
tom53efab52014-10-07 17:43:48 -070085 }
86
87 /**
88 * Returns the major version number.
89 *
90 * @return major version number
91 */
92 public int major() {
93 return major;
94 }
95
96 /**
97 * Returns the minor version number.
98 *
99 * @return minor version number
100 */
101 public int minor() {
102 return minor;
103 }
104
105 /**
Thomas Vachuska045c01d2014-12-04 00:18:06 -0800106 * Returns the version patch segment.
tom53efab52014-10-07 17:43:48 -0700107 *
108 * @return patch number
109 */
Thomas Vachuska045c01d2014-12-04 00:18:06 -0800110 public String patch() {
tom53efab52014-10-07 17:43:48 -0700111 return patch;
112 }
113
114 /**
115 * Returns the version build string.
116 *
117 * @return build string
118 */
119 public String build() {
120 return build;
121 }
122
123 @Override
124 public String toString() {
125 return format;
126 }
127
128 @Override
129 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700130 return format.hashCode();
tom53efab52014-10-07 17:43:48 -0700131 }
132
133 @Override
134 public boolean equals(Object obj) {
135 if (this == obj) {
136 return true;
137 }
138 if (obj instanceof Version) {
139 final Version other = (Version) obj;
140 return Objects.equals(this.format, other.format);
141 }
142 return false;
143 }
144}