Skip to content

Commit 37858a7

Browse files
committed
Simplify VersionUtils#compare, disable development.yml
1 parent ad70be1 commit 37858a7

File tree

7 files changed

+54
-108
lines changed

7 files changed

+54
-108
lines changed

.github/workflows/development.yml

Lines changed: 0 additions & 32 deletions
This file was deleted.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<groupId>dev.hypera</groupId>
2424
<artifactId>UpdateLib</artifactId>
25-
<version>3.1.1-SNAPSHOT</version>
25+
<version>3.1.2-SNAPSHOT</version>
2626
<packaging>jar</packaging>
2727

2828
<name>UpdateLib</name>

src/main/java/dev/hypera/updatelib/UpdateLib.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
import dev.hypera.updatelib.checkers.UpdateChecker;
2121
import dev.hypera.updatelib.data.CheckData;
2222
import dev.hypera.updatelib.objects.UpdateStatus;
23+
import dev.hypera.updatelib.objects.UpdateStatusBuilder;
2324

2425
import java.util.Timer;
2526
import java.util.TimerTask;
2627
import java.util.function.Consumer;
2728

2829
public class UpdateLib {
2930

30-
private final static String VERSION = "3.1.1-SNAPSHOT"; // Current UpdateLib version.
31+
private final static String VERSION = "3.1.2-SNAPSHOT"; // Current UpdateLib version.
3132

3233
private final long resourceId;
3334
private final String currentVersion;

src/main/java/dev/hypera/updatelib/objects/UpdateStatus.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,24 @@ public boolean isAvailable() {
7777
}
7878

7979
public enum Status {
80-
AVAILABLE, MAJOR_AVAILABLE, MINOR_AVAILABLE, UNAVAILABLE, FAILED
80+
MAJOR_AVAILABLE,
81+
MINOR_AVAILABLE,
82+
AVAILABLE,
83+
UNAVAILABLE,
84+
FAILED;
85+
86+
public static Status fromNumber(VersionScheme scheme, int i) {
87+
if(scheme.equals(VersionScheme.CALENDAR)) return AVAILABLE;
88+
89+
switch(i) {
90+
case 2:
91+
return MAJOR_AVAILABLE;
92+
case 3:
93+
return MINOR_AVAILABLE;
94+
default:
95+
return AVAILABLE;
96+
}
97+
}
8198
}
8299

83100
}

src/main/java/dev/hypera/updatelib/objects/UpdateStatusBuilder.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,7 @@ public UpdateStatus build() {
9595
scheme = versionScheme.get();
9696
}
9797

98-
VersionUtils.VersionChange change = VersionUtils.compare(scheme, distributedVersion, currentVersion);
99-
100-
return new UpdateStatus(distributedVersion, currentVersion, changeToStatus(change));
101-
}
102-
103-
/**
104-
* Converts {@link VersionUtils.VersionChange} to {@link UpdateStatus.Status}
105-
*
106-
* @param change VersionChange.
107-
*
108-
* @return Status.
109-
* @since 3.0.0-SNAPSHOT
110-
*/
111-
private UpdateStatus.Status changeToStatus(VersionUtils.VersionChange change) {
112-
if(change.equals(VersionUtils.VersionChange.NONE))
113-
return UpdateStatus.Status.UNAVAILABLE;
114-
115-
if(change.equals(VersionUtils.VersionChange.MAJOR))
116-
return UpdateStatus.Status.MAJOR_AVAILABLE;
117-
118-
if(change.equals(VersionUtils.VersionChange.MINOR))
119-
return UpdateStatus.Status.MINOR_AVAILABLE;
120-
121-
return UpdateStatus.Status.AVAILABLE;
98+
return new UpdateStatus(distributedVersion, currentVersion, VersionUtils.compare(scheme, distributedVersion, currentVersion));
12299
}
123100

124101
}

src/main/java/dev/hypera/updatelib/utils/Check.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,34 @@ public class Check {
2626
*
2727
* @param name Name of the object.
2828
* @param object Object.
29+
*
2930
* @since 3.0.0-SNAPSHOT
3031
*/
3132
public static void notNull(String name, Object object) {
3233
if(null == object)
3334
fail(name + " cannot be null");
3435
}
3536

37+
/**
38+
* Check if objects are null, if so, throw an IllegalArgumentException.
39+
*
40+
* @param names Object names.
41+
* @param objects Objects to check.
42+
*
43+
* @since 3.1.2-SNAPSHOT
44+
*/
45+
public static void notNull(String[] names, Object... objects) {
46+
for(int i = 0; i < objects.length; i++) {
47+
if(null == objects[i])
48+
fail(names[i] + " cannot be null.");
49+
}
50+
}
51+
3652
/**
3753
* Throw an IllegalArgumentException.
3854
*
3955
* @param message Message.
56+
*
4057
* @since 3.0.0-SNAPSHOT
4158
*/
4259
private static void fail(String message) {

src/main/java/dev/hypera/updatelib/utils/VersionUtils.java

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import dev.hypera.updatelib.annotations.Internal;
2020
import dev.hypera.updatelib.exceptions.VersionSchemeException;
21+
import dev.hypera.updatelib.objects.UpdateStatus;
2122
import dev.hypera.updatelib.objects.VersionScheme;
2223

2324
import java.util.Arrays;
@@ -46,59 +47,29 @@ public static Optional<VersionScheme> detectScheme(String version) {
4647
* @param newVersion New/Distributed version.
4748
* @param currentVersion Current resource version.
4849
*
49-
* @return {@link VersionChange}
50+
* @return {@link UpdateStatus.Status}
5051
* @since 3.0.0-SNAPSHOT
5152
*/
52-
public static VersionChange compare(VersionScheme versionScheme, String newVersion, String currentVersion) {
53-
Check.notNull("version scheme", versionScheme);
54-
Check.notNull("new version", newVersion);
55-
Check.notNull("current version", currentVersion);
53+
public static UpdateStatus.Status compare(VersionScheme versionScheme, String newVersion, String currentVersion) {
54+
Check.notNull(new String[] { "version scheme", "new version", "current version" }, versionScheme, newVersion, currentVersion);
5655

57-
// if(newVersion.equals(currentVersion))
58-
// return VersionChange.NONE;
56+
if(newVersion.equals(currentVersion))
57+
return UpdateStatus.Status.UNAVAILABLE;
58+
else if(versionScheme.equals(VersionScheme.CALENDAR))
59+
return UpdateStatus.Status.AVAILABLE;
5960

6061
Matcher matcher = versionScheme.getPattern().matcher(newVersion);
6162
Matcher currentMatcher = versionScheme.getPattern().matcher(currentVersion);
6263

6364
if(!matcher.find() || !currentMatcher.find())
6465
throw new VersionSchemeException("Version does not matcher version scheme.");
6566

66-
switch(versionScheme) {
67-
case BASIC:
68-
if(safeCheck(currentMatcher.group("major"), matcher.group("major")))
69-
return VersionChange.MAJOR;
70-
if(safeCheck(currentMatcher.group("minor"), matcher.group("minor")))
71-
return VersionChange.MINOR;
72-
if(safeCheck(currentMatcher.group("prerelease"), matcher.group("prerelease")))
73-
return VersionChange.PRE_RELEASE;
74-
return VersionChange.NONE;
75-
76-
case SEMANTIC:
77-
if(safeCheck(currentMatcher.group("major"), matcher.group("major")))
78-
return VersionChange.MAJOR;
79-
if(safeCheck(currentMatcher.group("minor"), matcher.group("minor")))
80-
return VersionChange.MINOR;
81-
if(safeCheck(currentMatcher.group("patch"), matcher.group("patch")))
82-
return VersionChange.PATCH;
83-
if(safeCheck(currentMatcher.group("prerelease"), matcher.group("prerelease")))
84-
return VersionChange.PRE_RELEASE;
85-
if(safeCheck(currentMatcher.group("buildmetadata"), matcher.group("buildmetadata")))
86-
return VersionChange.METADATA;
87-
return VersionChange.NONE;
88-
89-
case CALENDAR:
90-
if(safeCheck(currentMatcher.group("year"), matcher.group("year")))
91-
return VersionChange.YEAR;
92-
if(safeCheck(currentMatcher.group("month"), matcher.group("month")))
93-
return VersionChange.MONTH;
94-
if(safeCheck(currentMatcher.group("day"), matcher.group("day")))
95-
return VersionChange.DAY;
96-
return VersionChange.NONE;
97-
98-
default:
99-
throw new VersionSchemeException("Unknown version scheme");
67+
for(int i = 1; i < Math.max(currentMatcher.groupCount(), matcher.groupCount()); i++) {
68+
if(safeCheck(currentMatcher.group(i), matcher.group(i)))
69+
return UpdateStatus.Status.fromNumber(versionScheme, i);
10070
}
10171

72+
return UpdateStatus.Status.AVAILABLE;
10273
}
10374

10475
/**
@@ -131,14 +102,9 @@ private static boolean safeCheck(String currentGroup, String newGroup) {
131102
}
132103

133104
private static boolean isEmpty(String one) {
134-
if(null == one) return true;
135-
if(one.equals("")) return true;
136-
return false;
137-
}
138-
139-
@Internal
140-
public enum VersionChange {
141-
NONE, MAJOR, MINOR, PATCH, PRE_RELEASE, METADATA, YEAR, MONTH, DAY
105+
if(null == one)
106+
return true;
107+
return one.equals("");
142108
}
143109

144110
}

0 commit comments

Comments
 (0)