blob: 7a26357dbb33696b86005a2adb802f9089f04772 [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";
tom53efab52014-10-07 17:43:48 -070034
35 private final int major;
36 private final int minor;
Thomas Vachuska045c01d2014-12-04 00:18:06 -080037 private final String patch;
tom53efab52014-10-07 17:43:48 -070038 private final String build;
39
40 private final String format;
41
42 // Creates a new version descriptor
Thomas Vachuska045c01d2014-12-04 00:18:06 -080043 private Version(int major, int minor, String patch, String build) {
tom53efab52014-10-07 17:43:48 -070044 this.major = major;
45 this.minor = minor;
46 this.patch = patch;
47 this.build = build;
Thomas Vachuska93d944c2015-06-16 09:48:55 -070048 this.format =
49 isNullOrEmpty(patch) ?
50 String.format(FORMAT_MINIMAL, major, minor) :
51 (isNullOrEmpty(build) ?
52 String.format(FORMAT_SHORT, major, minor, patch) :
53 String.format(FORMAT_LONG, major, minor, patch, build));
tom53efab52014-10-07 17:43:48 -070054 }
55
56
57 /**
58 * Creates a new version from the specified constituent numbers.
59 *
60 * @param major major version number
Yuta HIGUCHIc7aa5072015-02-02 15:01:02 -080061 * @param minor minor version number
Thomas Vachuska045c01d2014-12-04 00:18:06 -080062 * @param patch version patch segment
63 * @param build optional build string
tom53efab52014-10-07 17:43:48 -070064 * @return version descriptor
65 */
Thomas Vachuska045c01d2014-12-04 00:18:06 -080066 public static Version version(int major, int minor, String patch, String build) {
67 checkArgument(major > 0, NEGATIVE);
68 checkArgument(minor > 0, NEGATIVE);
tom53efab52014-10-07 17:43:48 -070069 return new Version(major, minor, patch, build);
70 }
71
72 /**
73 * Creates a new version by parsing the specified string.
74 *
75 * @param string version string
76 * @return version descriptor
77 */
78 public static Version version(String string) {
79 String[] fields = string.split("[.-]");
80 return new Version(parseInt(fields[0]), parseInt(fields[1]),
Thomas Vachuska93d944c2015-06-16 09:48:55 -070081 fields.length >= 3 ? fields[2] : null,
82 fields.length >= 4 ? fields[3] : null);
tom53efab52014-10-07 17:43:48 -070083 }
84
85 /**
86 * Returns the major version number.
87 *
88 * @return major version number
89 */
90 public int major() {
91 return major;
92 }
93
94 /**
95 * Returns the minor version number.
96 *
97 * @return minor version number
98 */
99 public int minor() {
100 return minor;
101 }
102
103 /**
Thomas Vachuska045c01d2014-12-04 00:18:06 -0800104 * Returns the version patch segment.
tom53efab52014-10-07 17:43:48 -0700105 *
106 * @return patch number
107 */
Thomas Vachuska045c01d2014-12-04 00:18:06 -0800108 public String patch() {
tom53efab52014-10-07 17:43:48 -0700109 return patch;
110 }
111
112 /**
113 * Returns the version build string.
114 *
115 * @return build string
116 */
117 public String build() {
118 return build;
119 }
120
121 @Override
122 public String toString() {
123 return format;
124 }
125
126 @Override
127 public int hashCode() {
128 return Objects.hash(format);
129 }
130
131 @Override
132 public boolean equals(Object obj) {
133 if (this == obj) {
134 return true;
135 }
136 if (obj instanceof Version) {
137 final Version other = (Version) obj;
138 return Objects.equals(this.format, other.format);
139 }
140 return false;
141 }
142}