Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.core; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.Objects; |
| 19 | |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkArgument; |
| 21 | import static com.google.common.base.Strings.isNullOrEmpty; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 22 | import static java.lang.Integer.parseInt; |
| 23 | |
| 24 | /** |
| 25 | * Representation of the product version. |
| 26 | */ |
| 27 | public final class Version { |
| 28 | |
Thomas Vachuska | 93d944c | 2015-06-16 09:48:55 -0700 | [diff] [blame] | 29 | public static final String FORMAT_MINIMAL = "%d.%d"; |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 30 | public static final String FORMAT_SHORT = "%d.%d.%s"; |
Thomas Vachuska | 93d944c | 2015-06-16 09:48:55 -0700 | [diff] [blame] | 31 | public static final String FORMAT_LONG = "%d.%d.%s.%s"; |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 32 | |
| 33 | private static final String NEGATIVE = "Version segment cannot be negative"; |
Thomas Vachuska | afb11da | 2015-06-16 10:01:13 -0700 | [diff] [blame] | 34 | public static final String TOO_SHORT = "Version must have at least major and minor numbers"; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 35 | |
| 36 | private final int major; |
| 37 | private final int minor; |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 38 | private final String patch; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 39 | private final String build; |
| 40 | |
| 41 | private final String format; |
| 42 | |
| 43 | // Creates a new version descriptor |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 44 | private Version(int major, int minor, String patch, String build) { |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 45 | this.major = major; |
| 46 | this.minor = minor; |
| 47 | this.patch = patch; |
| 48 | this.build = build; |
Thomas Vachuska | 93d944c | 2015-06-16 09:48:55 -0700 | [diff] [blame] | 49 | 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)); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Creates a new version from the specified constituent numbers. |
| 60 | * |
| 61 | * @param major major version number |
Yuta HIGUCHI | c7aa507 | 2015-02-02 15:01:02 -0800 | [diff] [blame] | 62 | * @param minor minor version number |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 63 | * @param patch version patch segment |
| 64 | * @param build optional build string |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 65 | * @return version descriptor |
| 66 | */ |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 67 | public static Version version(int major, int minor, String patch, String build) { |
Thomas Vachuska | afb11da | 2015-06-16 10:01:13 -0700 | [diff] [blame] | 68 | checkArgument(major >= 0, NEGATIVE); |
| 69 | checkArgument(minor >= 0, NEGATIVE); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 70 | 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) { |
David K. Bainbridge | ae06fc4 | 2016-06-07 19:13:07 -0700 | [diff] [blame] | 80 | String[] fields = string.split("[.-]", 4); |
Thomas Vachuska | afb11da | 2015-06-16 10:01:13 -0700 | [diff] [blame] | 81 | checkArgument(fields.length >= 2, TOO_SHORT); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 82 | return new Version(parseInt(fields[0]), parseInt(fields[1]), |
Thomas Vachuska | 93d944c | 2015-06-16 09:48:55 -0700 | [diff] [blame] | 83 | fields.length >= 3 ? fields[2] : null, |
| 84 | fields.length >= 4 ? fields[3] : null); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 85 | } |
| 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 Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 106 | * Returns the version patch segment. |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 107 | * |
| 108 | * @return patch number |
| 109 | */ |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 110 | public String patch() { |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 111 | 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 Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 130 | return format.hashCode(); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 131 | } |
| 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 | } |