blob: 5641a5495326313b827ff7ad1f8b801da8f0ac69 [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
20import static java.lang.Integer.parseInt;
21
22/**
23 * Representation of the product version.
24 */
25public final class Version {
26
27 public static final String FORMAT = "%d.%d.%d.%s";
28
29 private final int major;
30 private final int minor;
31 private final int patch;
32 private final String build;
33
34 private final String format;
35
36 // Creates a new version descriptor
37 private Version(int major, int minor, int patch, String build) {
38 this.major = major;
39 this.minor = minor;
40 this.patch = patch;
41 this.build = build;
42 this.format = String.format(FORMAT, major, minor, patch, build);
43 }
44
45
46 /**
47 * Creates a new version from the specified constituent numbers.
48 *
49 * @param major major version number
50 * @param minor minod version number
51 * @param patch version patch number
52 * @param build build string
53 * @return version descriptor
54 */
55 public static Version version(int major, int minor, int patch, String build) {
56 return new Version(major, minor, patch, build);
57 }
58
59 /**
60 * Creates a new version by parsing the specified string.
61 *
62 * @param string version string
63 * @return version descriptor
64 */
65 public static Version version(String string) {
66 String[] fields = string.split("[.-]");
67 return new Version(parseInt(fields[0]), parseInt(fields[1]),
68 parseInt(fields[2]), fields[3]);
69 }
70
71 /**
72 * Returns the major version number.
73 *
74 * @return major version number
75 */
76 public int major() {
77 return major;
78 }
79
80 /**
81 * Returns the minor version number.
82 *
83 * @return minor version number
84 */
85 public int minor() {
86 return minor;
87 }
88
89 /**
90 * Returns the version patch number.
91 *
92 * @return patch number
93 */
94 public int patch() {
95 return patch;
96 }
97
98 /**
99 * Returns the version build string.
100 *
101 * @return build string
102 */
103 public String build() {
104 return build;
105 }
106
107 @Override
108 public String toString() {
109 return format;
110 }
111
112 @Override
113 public int hashCode() {
114 return Objects.hash(format);
115 }
116
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj) {
120 return true;
121 }
122 if (obj instanceof Version) {
123 final Version other = (Version) obj;
124 return Objects.equals(this.format, other.format);
125 }
126 return false;
127 }
128}