Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
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"; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 34 | |
| 35 | private final int major; |
| 36 | private final int minor; |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 37 | private final String patch; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 38 | private final String build; |
| 39 | |
| 40 | private final String format; |
| 41 | |
| 42 | // Creates a new version descriptor |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 43 | private Version(int major, int minor, String patch, String build) { |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 44 | this.major = major; |
| 45 | this.minor = minor; |
| 46 | this.patch = patch; |
| 47 | this.build = build; |
Thomas Vachuska | 93d944c | 2015-06-16 09:48:55 -0700 | [diff] [blame] | 48 | 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)); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Creates a new version from the specified constituent numbers. |
| 59 | * |
| 60 | * @param major major version number |
Yuta HIGUCHI | c7aa507 | 2015-02-02 15:01:02 -0800 | [diff] [blame] | 61 | * @param minor minor version number |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 62 | * @param patch version patch segment |
| 63 | * @param build optional build string |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 64 | * @return version descriptor |
| 65 | */ |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 66 | public static Version version(int major, int minor, String patch, String build) { |
| 67 | checkArgument(major > 0, NEGATIVE); |
| 68 | checkArgument(minor > 0, NEGATIVE); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 69 | 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 Vachuska | 93d944c | 2015-06-16 09:48:55 -0700 | [diff] [blame] | 81 | fields.length >= 3 ? fields[2] : null, |
| 82 | fields.length >= 4 ? fields[3] : null); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 83 | } |
| 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 Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 104 | * Returns the version patch segment. |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 105 | * |
| 106 | * @return patch number |
| 107 | */ |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 108 | public String patch() { |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 109 | 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 | } |