GitNex-Android-App/app/src/main/java/org/mian/gitnex/helpers/Version.java

240 lines
4.7 KiB
Java
Raw Normal View History

package org.mian.gitnex.helpers;
import android.util.Log;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 6543
*/
public class Version {
// the raw String
private String raw;
// the version numbers in its order (dot separated)
private List<Integer> values;
private boolean dev;
public Version(String value) {
raw = value;
this.init();
}
/**
* valid return true if string is a valid version
*
2024-03-28 13:02:17 +01:00
* @param value String
* @return true/false
*/
public static boolean valid(String value) {
if (value == null) {
return false;
}
final Pattern patternValid =
2024-03-28 13:02:17 +01:00
Pattern.compile("^[vV]?(\\d+)+(\\.(\\d+))*([_\\-+][\\w\\-+.]*)?$");
return value.equals("main") || patternValid.matcher(value).find();
}
/**
* init parse and store values for other functions of an Version instance it use the raw
* variable as base
*/
private void init() {
final Pattern patternNumberDotNumber = Pattern.compile("^\\d+(\\.(\\d)+)*");
if (raw.isEmpty()) {
raw = "0";
}
if (!valid(raw)) {
// throw new IllegalArgumentException("Invalid version format: " + raw);
Log.e("Version", "Invalid version format: " + raw);
}
if (raw.equals("main")) {
dev = true;
values = new ArrayList<>();
return;
}
if (raw.charAt(0) == 'v' || raw.charAt(0) == 'V') {
raw = raw.substring(1);
}
values = new ArrayList<>();
Matcher match = patternNumberDotNumber.matcher(raw);
if (!match.find()) {
dev = true;
values = new ArrayList<>();
return;
}
for (String i : match.group().split("\\.")) {
values.add(Integer.parseInt(i));
}
}
/**
* equal return true if version is the same
*
2024-03-28 13:02:17 +01:00
* @param value String
* @return true/false
*/
public boolean equal(String value) {
return this.equal(new Version(value));
}
/**
* equal return true if version is the same
*
2024-03-28 13:02:17 +01:00
* @param v Version
* @return true/false
*/
public boolean equal(@NonNull Version v) {
if (dev || v.dev) { // equal if raw is equal
return Objects.equals(raw, v.raw);
}
int rounds = Math.min(this.values.size(), v.values.size());
for (int i = 0; i < rounds; i++) {
if (!Objects.equals(this.values.get(i), v.values.get(i))) {
return false;
}
}
return true;
}
/**
* less return true if version is less
*
2024-03-28 13:02:17 +01:00
* @param value String
* @return true/false
*/
public boolean less(String value) {
return this.less(new Version(value));
}
/**
* less return true if version is less
*
2024-03-28 13:02:17 +01:00
* @param v Version
* @return true/false
*/
public boolean less(@NonNull Version v) {
return v.higher(this);
}
/**
* higher return true if version is higher
*
2024-03-28 13:02:17 +01:00
* @param value String
* @return true/false
*/
public boolean higher(String value) {
return this.higher(new Version(value));
}
/**
* higher return true if version is higher
*
2024-03-28 13:02:17 +01:00
* @param v Version
* @return true/false
*/
public boolean higher(@NonNull Version v) {
if (dev) {
return !v.dev;
} else if (v.dev) {
return false;
}
int rounds = Math.min(this.values.size(), v.values.size());
for (int i = 0; i < rounds; i++) {
if (i + 1 == rounds) {
if (this.values.get(i) <= v.values.get(i)) {
return false;
}
} else {
if (this.values.get(i) < v.values.get(i)) {
return false;
} else if (this.values.get(i) > v.values.get(i)) {
return true;
}
}
}
return true;
}
/**
* lessOrEqual return true if version is less or equal
*
2024-03-28 13:02:17 +01:00
* @param value String
* @return true/false
*/
public boolean lessOrEqual(String value) {
return this.lessOrEqual(new Version(value));
}
/**
* lessOrEqual return true if version is less or equal
*
2024-03-28 13:02:17 +01:00
* @param v Version
* @return true/false
*/
public boolean lessOrEqual(@NonNull Version v) {
return v.higherOrEqual(this);
}
/**
* higherOrEqual return true if version is higher or equal
*
2024-03-28 13:02:17 +01:00
* @param value String
* @return true/false
*/
public boolean higherOrEqual(String value) {
return this.higherOrEqual(new Version(value));
}
/**
* higherOrEqual return true if version is higher or equal
*
2024-03-28 13:02:17 +01:00
* @param v Version
* @return true/false
*/
public boolean higherOrEqual(@NonNull Version v) {
if (dev || v.dev) { // if one is a dev version, only true if both are dev
return v.dev && dev;
}
int rounds = Math.min(this.values.size(), v.values.size());
for (int i = 0; i < rounds; i++) {
if (this.values.get(i) > v.values.get(i)) {
return true;
} else if (this.values.get(i) < v.values.get(i)) {
return false;
}
}
return true;
}
@NonNull @Override
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
public String toString() {
return raw;
}
}