Files
FalsePatternLib/src/main/java/com/falsepattern/lib/dependencies/SemanticVersion.java
T

185 lines
6.2 KiB
Java
Raw Normal View History

2022-07-31 20:41:56 +02:00
/*
2024-04-17 17:23:43 +02:00
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2024 FalsePattern
2022-07-30 15:21:11 +02:00
* All Rights Reserved
*
2024-04-17 17:23:43 +02:00
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
2022-07-30 15:21:11 +02:00
*
2024-04-17 17:23:43 +02:00
* FalsePatternLib is free software: you can redistribute it and/or modify
2022-07-30 15:21:11 +02:00
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
2024-04-17 17:23:43 +02:00
* FalsePatternLib is distributed in the hope that it will be useful,
2022-07-30 15:21:11 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2024-04-17 17:23:43 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
2022-07-30 15:21:11 +02:00
*
* You should have received a copy of the GNU Lesser General Public License
2024-04-17 17:23:43 +02:00
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
2022-07-30 15:21:11 +02:00
*/
package com.falsepattern.lib.dependencies;
2022-05-06 13:03:23 +02:00
2022-05-06 23:53:11 +02:00
import com.falsepattern.lib.StableAPI;
2022-05-06 13:03:23 +02:00
import lombok.Getter;
import lombok.NonNull;
import lombok.val;
2022-07-31 20:41:56 +02:00
import java.util.Objects;
2022-05-06 23:53:11 +02:00
@StableAPI(since = "0.6.0")
2022-05-06 13:03:23 +02:00
public class SemanticVersion extends Version {
2022-08-17 20:08:46 +02:00
@Getter(onMethod_ = @StableAPI.Expose)
2022-07-31 20:41:56 +02:00
private final int majorVersion;
2022-08-17 20:08:46 +02:00
@Getter(onMethod_ = @StableAPI.Expose)
2022-07-31 20:41:56 +02:00
private final int minorVersion;
2022-08-17 20:08:46 +02:00
@Getter(onMethod_ = @StableAPI.Expose)
2022-07-31 20:41:56 +02:00
private final int patchVersion;
2022-08-17 20:08:46 +02:00
@Getter(onMethod_ = @StableAPI.Expose)
2022-07-31 20:41:56 +02:00
private final String preRelease;
2022-08-17 20:08:46 +02:00
@Getter(onMethod_ = @StableAPI.Expose)
2022-07-31 20:41:56 +02:00
private final String build;
2022-06-08 16:01:06 +02:00
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-05-06 13:03:23 +02:00
public SemanticVersion(int majorVersion, int minorVersion, int patchVersion, String preRelease, String build) {
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
this.patchVersion = patchVersion;
preRelease = preRelease == null ? null : preRelease.trim();
build = build == null ? null : build.trim();
this.preRelease = "".equals(preRelease) ? null : preRelease;
this.build = "".equals(build) ? null : build;
}
2022-08-17 20:08:46 +02:00
@StableAPI.Expose
public SemanticVersion(int majorVersion, int minorVersion, int patchVersion, String preRelease) {
this(majorVersion, minorVersion, patchVersion, preRelease, null);
}
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-05-06 13:03:23 +02:00
public SemanticVersion(int majorVersion, int minorVersion, int patchVersion) {
this(majorVersion, minorVersion, patchVersion, null, null);
}
2022-08-17 20:08:46 +02:00
@StableAPI.Expose(since = "0.10.0")
public SemanticVersion(int majorVersion, int minorVersion) {
this(majorVersion, minorVersion, -1, null, null);
}
@StableAPI.Expose(since = "0.10.0")
public SemanticVersion(int majorVersion) {
this(majorVersion, -1, -1, null, null);
}
@StableAPI.Expose
public static SemanticVersionBuilder builder() {
return new SemanticVersionBuilder();
}
2022-05-06 13:03:23 +02:00
@Override
public int compareTo(@NonNull Version o) {
if (o instanceof ComplexVersion) {
2022-06-08 16:01:06 +02:00
val result = this.compareTo(((ComplexVersion) o).versions[0]);
2022-05-06 13:03:23 +02:00
if (result != 0) {
return result;
} else if (((ComplexVersion) o).versions.length > 1) {
return 1;
} else {
return 0;
}
} else if (o instanceof SemanticVersion) {
2022-06-08 16:01:06 +02:00
val other = (SemanticVersion) o;
2022-05-06 13:03:23 +02:00
if (majorVersion != other.majorVersion) {
return majorVersion - other.majorVersion;
} else if (minorVersion != other.minorVersion) {
return minorVersion - other.minorVersion;
} else if (patchVersion != other.patchVersion) {
return patchVersion - other.patchVersion;
} else if (!Objects.equals(preRelease, other.preRelease)) {
if (preRelease == null) {
return 1;
} else if (other.preRelease == null) {
return -1;
}
return preRelease.compareTo(other.preRelease);
} else {
return 0;
}
}
return 0;
}
@Override
public String toString() {
2023-07-16 13:09:05 +02:00
// @formatter:off
2022-08-17 20:10:51 +02:00
return majorVersion + (minorVersion < 0 ? "" : "." + minorVersion) +
(patchVersion < 0 ? "" : "." + patchVersion) + (preRelease == null ? "" : "-" + preRelease) +
2022-06-08 16:01:06 +02:00
(build == null ? "" : "+" + build);
2023-07-16 13:09:05 +02:00
// @formatter:on
2022-05-06 13:03:23 +02:00
}
2022-08-17 20:08:46 +02:00
@StableAPI(since = "0.10.0")
public static class SemanticVersionBuilder {
private int majorVersion;
private int minorVersion = -1;
private int patchVersion = -1;
private String preRelease;
private String build;
@StableAPI.Internal
SemanticVersionBuilder() {
}
@StableAPI.Expose
public SemanticVersionBuilder majorVersion(int majorVersion) {
this.majorVersion = majorVersion;
return this;
}
@StableAPI.Expose
public SemanticVersionBuilder minorVersion(int minorVersion) {
this.minorVersion = minorVersion;
return this;
}
@StableAPI.Expose
public SemanticVersionBuilder patchVersion(int patchVersion) {
this.patchVersion = patchVersion;
return this;
}
@StableAPI.Expose
public SemanticVersionBuilder preRelease(String preRelease) {
this.preRelease = preRelease;
return this;
}
@StableAPI.Expose
public SemanticVersionBuilder build(String build) {
this.build = build;
return this;
}
@StableAPI.Expose
public SemanticVersion build() {
return new SemanticVersion(majorVersion, minorVersion, patchVersion, preRelease, build);
}
@Override
public String toString() {
2023-07-16 13:09:05 +02:00
return "SemanticVersion.SemanticVersionBuilder(majorVersion="
+ this.majorVersion
+ ", minorVersion="
+ this.minorVersion
+ ", patchVersion="
+ this.patchVersion
+ ", preRelease="
+ this.preRelease
+ ", build="
+ this.build
+ ")";
2022-08-17 20:08:46 +02:00
}
}
2022-05-06 13:03:23 +02:00
}