tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 1 | package org.onlab.onos; |
| 2 | |
| 3 | import java.util.Objects; |
| 4 | |
| 5 | import static java.lang.Integer.parseInt; |
| 6 | |
| 7 | /** |
| 8 | * Representation of the product version. |
| 9 | */ |
| 10 | public final class Version { |
| 11 | |
| 12 | public static final String FORMAT = "%d.%d.%d.%s"; |
| 13 | |
| 14 | private final int major; |
| 15 | private final int minor; |
| 16 | private final int patch; |
| 17 | private final String build; |
| 18 | |
| 19 | private final String format; |
| 20 | |
| 21 | // Creates a new version descriptor |
| 22 | private Version(int major, int minor, int patch, String build) { |
| 23 | this.major = major; |
| 24 | this.minor = minor; |
| 25 | this.patch = patch; |
| 26 | this.build = build; |
| 27 | this.format = String.format(FORMAT, major, minor, patch, build); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * Creates a new version from the specified constituent numbers. |
| 33 | * |
| 34 | * @param major major version number |
| 35 | * @param minor minod version number |
| 36 | * @param patch version patch number |
| 37 | * @param build build string |
| 38 | * @return version descriptor |
| 39 | */ |
| 40 | public static Version version(int major, int minor, int patch, String build) { |
| 41 | return new Version(major, minor, patch, build); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Creates a new version by parsing the specified string. |
| 46 | * |
| 47 | * @param string version string |
| 48 | * @return version descriptor |
| 49 | */ |
| 50 | public static Version version(String string) { |
| 51 | String[] fields = string.split("[.-]"); |
| 52 | return new Version(parseInt(fields[0]), parseInt(fields[1]), |
| 53 | parseInt(fields[2]), fields[3]); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the major version number. |
| 58 | * |
| 59 | * @return major version number |
| 60 | */ |
| 61 | public int major() { |
| 62 | return major; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the minor version number. |
| 67 | * |
| 68 | * @return minor version number |
| 69 | */ |
| 70 | public int minor() { |
| 71 | return minor; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the version patch number. |
| 76 | * |
| 77 | * @return patch number |
| 78 | */ |
| 79 | public int patch() { |
| 80 | return patch; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the version build string. |
| 85 | * |
| 86 | * @return build string |
| 87 | */ |
| 88 | public String build() { |
| 89 | return build; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public String toString() { |
| 94 | return format; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public int hashCode() { |
| 99 | return Objects.hash(format); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public boolean equals(Object obj) { |
| 104 | if (this == obj) { |
| 105 | return true; |
| 106 | } |
| 107 | if (obj instanceof Version) { |
| 108 | final Version other = (Version) obj; |
| 109 | return Objects.equals(this.format, other.format); |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | } |