blob: 9196b5be25c94d7b1fd52b45660c1900a995c386 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 Vachuska045c01d2014-12-04 00:18:06 -080029 public static final String FORMAT = "%d.%d.%s.%s";
30 public static final String FORMAT_SHORT = "%d.%d.%s";
31
32 private static final String NEGATIVE = "Version segment cannot be negative";
tom53efab52014-10-07 17:43:48 -070033
34 private final int major;
35 private final int minor;
Thomas Vachuska045c01d2014-12-04 00:18:06 -080036 private final String patch;
tom53efab52014-10-07 17:43:48 -070037 private final String build;
38
39 private final String format;
40
41 // Creates a new version descriptor
Thomas Vachuska045c01d2014-12-04 00:18:06 -080042 private Version(int major, int minor, String patch, String build) {
tom53efab52014-10-07 17:43:48 -070043 this.major = major;
44 this.minor = minor;
45 this.patch = patch;
46 this.build = build;
Thomas Vachuska045c01d2014-12-04 00:18:06 -080047 this.format = isNullOrEmpty(build) ?
48 String.format(FORMAT_SHORT, major, minor, patch) :
49 String.format(FORMAT, major, minor, patch, build);
tom53efab52014-10-07 17:43:48 -070050 }
51
52
53 /**
54 * Creates a new version from the specified constituent numbers.
55 *
56 * @param major major version number
57 * @param minor minod version number
Thomas Vachuska045c01d2014-12-04 00:18:06 -080058 * @param patch version patch segment
59 * @param build optional build string
tom53efab52014-10-07 17:43:48 -070060 * @return version descriptor
61 */
Thomas Vachuska045c01d2014-12-04 00:18:06 -080062 public static Version version(int major, int minor, String patch, String build) {
63 checkArgument(major > 0, NEGATIVE);
64 checkArgument(minor > 0, NEGATIVE);
tom53efab52014-10-07 17:43:48 -070065 return new Version(major, minor, patch, build);
66 }
67
68 /**
69 * Creates a new version by parsing the specified string.
70 *
71 * @param string version string
72 * @return version descriptor
73 */
74 public static Version version(String string) {
75 String[] fields = string.split("[.-]");
76 return new Version(parseInt(fields[0]), parseInt(fields[1]),
Thomas Vachuska045c01d2014-12-04 00:18:06 -080077 fields[2], fields.length == 4 ? fields[3] : null);
tom53efab52014-10-07 17:43:48 -070078 }
79
80 /**
81 * Returns the major version number.
82 *
83 * @return major version number
84 */
85 public int major() {
86 return major;
87 }
88
89 /**
90 * Returns the minor version number.
91 *
92 * @return minor version number
93 */
94 public int minor() {
95 return minor;
96 }
97
98 /**
Thomas Vachuska045c01d2014-12-04 00:18:06 -080099 * Returns the version patch segment.
tom53efab52014-10-07 17:43:48 -0700100 *
101 * @return patch number
102 */
Thomas Vachuska045c01d2014-12-04 00:18:06 -0800103 public String patch() {
tom53efab52014-10-07 17:43:48 -0700104 return patch;
105 }
106
107 /**
108 * Returns the version build string.
109 *
110 * @return build string
111 */
112 public String build() {
113 return build;
114 }
115
116 @Override
117 public String toString() {
118 return format;
119 }
120
121 @Override
122 public int hashCode() {
123 return Objects.hash(format);
124 }
125
126 @Override
127 public boolean equals(Object obj) {
128 if (this == obj) {
129 return true;
130 }
131 if (obj instanceof Version) {
132 final Version other = (Version) obj;
133 return Objects.equals(this.format, other.format);
134 }
135 return false;
136 }
137}