View Javadoc
1   /*
2    * Copyright (c) Patrick Magauran 2018.
3    *   Licensed under the AGPLv3. All conditions of said license apply.
4    *       This file is part of ABOS.
5    *
6    *       ABOS is free software: you can redistribute it and/or modify
7    *       it under the terms of the GNU Affero General Public License as published by
8    *       the Free Software Foundation, either version 3 of the License, or
9    *       (at your option) any later version.
10   *
11   *       ABOS is distributed in the hope that it will be useful,
12   *       but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *       GNU Affero General Public License for more details.
15   *
16   *       You should have received a copy of the GNU Affero General Public License
17   *       along with ABOS.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package Utilities;
21  
22  public class Version {
23  
24      private final int major;
25      private final int minor;
26      private final int revision;
27  
28      public Version(int major, int minor, int revision) {
29          this.major = major;
30          this.minor = minor;
31          this.revision = revision;
32      }
33  
34      public Version(String version) {
35          String[] versionArray = version.split("\\.");
36          this.major = versionArray.length >= 1 ? Integer.valueOf(versionArray[0]) : 0;
37          this.minor = versionArray.length >= 2 ? Integer.valueOf(versionArray[1]) : 0;
38          this.revision = versionArray.length >= 3 ? Integer.valueOf(versionArray[2]) : 0;
39      }
40  
41      public int getMajor() {
42          return major;
43      }
44  
45      public int getMinor() {
46          return minor;
47      }
48  
49      public int getRevision() {
50          return revision;
51      }
52  
53      public boolean greaterThan(String version) {
54          String[] versionArray = version.split("\\.");
55  
56          int majorVal = versionArray.length >= 1 ? Integer.valueOf(versionArray[0]) : 0;
57          int minorVal = versionArray.length >= 2 ? Integer.valueOf(versionArray[1]) : 0;
58          int revisionVal = versionArray.length >= 3 ? Integer.valueOf(versionArray[2]) : 0;
59          return major > majorVal || (major >= majorVal && minor > minorVal) || (major >= majorVal && minor >= minorVal && revision > revisionVal);
60      }
61  
62      public static String format(String version) {
63          return new Version(version).toString();
64      }
65  
66      public boolean greaterThanOrEqual(String version) {
67          String[] versionArray = version.split("\\.");
68          int majorVal = versionArray.length >= 1 ? Integer.valueOf(versionArray[0]) : 0;
69          int minorVal = versionArray.length >= 2 ? Integer.valueOf(versionArray[1]) : 0;
70          int revisionVal = versionArray.length >= 3 ? Integer.valueOf(versionArray[2]) : 0;
71          return (major >= majorVal && minor >= minorVal && revision >= revisionVal) || (major > majorVal) || (major >= majorVal && minor > minorVal);
72      }
73  
74      public boolean greaterThan(Version version) {
75  
76          int majorVal = version.major;
77          int minorVal = version.minor;
78          int revisionVal = version.revision;
79          return major > majorVal || (major >= majorVal && minor > minorVal) || (major >= majorVal && minor >= minorVal && revision > revisionVal);
80      }
81  
82      @Override
83      public boolean equals(Object version) {
84          if (version == null) return false;
85          if (version == this) return true;
86          if (!(version instanceof String) && !(version instanceof Version)) return false;
87          if (version instanceof Version) {
88              return (major == ((Version) version).major && minor == ((Version) version).minor && revision == ((Version) version).revision);
89  
90          } else {
91              String[] versionArray = ((String) version).split("\\.");
92              int majorVal = versionArray.length >= 1 ? Integer.valueOf(versionArray[0]) : 0;
93              int minorVal = versionArray.length >= 2 ? Integer.valueOf(versionArray[1]) : 0;
94              int revisionVal = versionArray.length >= 3 ? Integer.valueOf(versionArray[2]) : 0;
95              return (major == majorVal && minor == minorVal && revision == revisionVal);
96  
97          }
98      }
99  
100     @Override
101     public int hashCode() {
102         return Integer.hashCode(major) + Integer.hashCode(minor) + Integer.hashCode(revision);
103     }
104 
105     public boolean greaterThanOrEqual(Version version) {
106         int majorVal = version.major;
107         int minorVal = version.minor;
108         int revisionVal = version.revision;
109         return (major >= majorVal && minor >= minorVal && revision >= revisionVal) || (major > majorVal) || (major >= majorVal && minor > minorVal);
110     }
111 
112     @Override
113     public String toString() {
114         return major + "." + minor + "." + revision;
115     }
116 }