Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'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 | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 29 | 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"; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 33 | |
| 34 | private final int major; |
| 35 | private final int minor; |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 36 | private final String patch; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 37 | private final String build; |
| 38 | |
| 39 | private final String format; |
| 40 | |
| 41 | // Creates a new version descriptor |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 42 | private Version(int major, int minor, String patch, String build) { |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 43 | this.major = major; |
| 44 | this.minor = minor; |
| 45 | this.patch = patch; |
| 46 | this.build = build; |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 47 | this.format = isNullOrEmpty(build) ? |
| 48 | String.format(FORMAT_SHORT, major, minor, patch) : |
| 49 | String.format(FORMAT, major, minor, patch, build); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 50 | } |
| 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 Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 58 | * @param patch version patch segment |
| 59 | * @param build optional build string |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 60 | * @return version descriptor |
| 61 | */ |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 62 | public static Version version(int major, int minor, String patch, String build) { |
| 63 | checkArgument(major > 0, NEGATIVE); |
| 64 | checkArgument(minor > 0, NEGATIVE); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 65 | 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 Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 77 | fields[2], fields.length == 4 ? fields[3] : null); |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 78 | } |
| 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 Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 99 | * Returns the version patch segment. |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 100 | * |
| 101 | * @return patch number |
| 102 | */ |
Thomas Vachuska | 045c01d | 2014-12-04 00:18:06 -0800 | [diff] [blame] | 103 | public String patch() { |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 104 | 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 | } |