1
2
3
4
5
6 package org.bud.kernel.model;
7 /***
8 *
9 */
10 public class Version implements Comparable {
11 private int major;
12 private int minor;
13 private int patch;
14
15
16 public Version() {
17 }
18
19
20 public Version(int major, int minor, int patch) {
21 this.major = major;
22 this.minor = minor;
23 this.patch = patch;
24 }
25
26
27 public void setMajor(int major) {
28 this.major = major;
29 }
30
31
32 public int getMajor() {
33 return major;
34 }
35
36
37 public void setMinor(int minor) {
38 this.minor = minor;
39 }
40
41
42 public int getMinor() {
43 return minor;
44 }
45
46
47 public void setPatch(int patch) {
48 this.patch = patch;
49 }
50
51
52 public int getPatch() {
53 return patch;
54 }
55
56
57 public String toString() {
58 String postVersion = (patch == 0) ? "" : "." + patch;
59 return major + "." + minor + postVersion;
60 }
61
62
63 public static Version createVersion(String text) {
64 String[] numbers = text.split("//.");
65
66 Version version = new Version();
67 version.setMajor(Integer.parseInt(numbers[0]));
68 version.setMinor(Integer.parseInt(numbers[1]));
69 if (numbers.length == 3) {
70 version.setPatch(Integer.parseInt(numbers[2]));
71 }
72 return version;
73 }
74
75
76 public boolean equals(Object object) {
77 if (this == object) {
78 return true;
79 }
80 if (object == null || !(object instanceof Version)) {
81 return false;
82 }
83
84 return compareTo(object) == 0;
85 }
86
87
88 public int hashCode() {
89 int result;
90 result = major;
91 result = 29 * result + minor;
92 result = 29 * result + patch;
93 return result;
94 }
95
96
97 public int compareTo(Object object) {
98 Version other = (Version)object;
99 if (getMajor() != other.getMajor()) {
100 return compareNumber(getMajor(), other.getMajor());
101 }
102 if (getMinor() != other.getMinor()) {
103 return compareNumber(getMinor(), other.getMinor());
104 }
105 return compareNumber(getPatch(), other.getPatch());
106 }
107
108
109 private int compareNumber(int numberA, int numberB) {
110 if (numberA > numberB) {
111 return 1;
112 }
113 else if (numberA < numberB) {
114 return -1;
115 }
116 else {
117 return 0;
118 }
119 }
120 }