2022-07-31 20:41:56 +02:00
|
|
|
/*
|
2024-04-17 17:23:43 +02:00
|
|
|
* This file is part of FalsePatternLib.
|
|
|
|
|
*
|
2025-05-24 19:29:12 +02:00
|
|
|
* Copyright (C) 2022-2025 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
|
2025-05-24 19:29:12 +02:00
|
|
|
* the Free Software Foundation, only version 3 of the License.
|
2022-07-30 15:21:11 +02:00
|
|
|
*
|
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
|
|
|
*/
|
2022-05-06 13:39:00 +02:00
|
|
|
package com.falsepattern.lib.dependencies;
|
2022-05-06 13:03:23 +02:00
|
|
|
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
import lombok.NonNull;
|
|
|
|
|
import lombok.val;
|
2025-05-26 09:42:01 +02:00
|
|
|
import org.jetbrains.annotations.ApiStatus;
|
2022-05-06 13:03:23 +02:00
|
|
|
|
2022-07-31 20:41:56 +02:00
|
|
|
import java.util.Objects;
|
|
|
|
|
|
2022-05-06 13:03:23 +02:00
|
|
|
public class SemanticVersion extends Version {
|
2025-05-26 09:42:01 +02:00
|
|
|
@Getter
|
2022-07-31 20:41:56 +02:00
|
|
|
private final int majorVersion;
|
2025-05-26 09:42:01 +02:00
|
|
|
@Getter
|
2022-07-31 20:41:56 +02:00
|
|
|
private final int minorVersion;
|
2025-05-26 09:42:01 +02:00
|
|
|
@Getter
|
2022-07-31 20:41:56 +02:00
|
|
|
private final int patchVersion;
|
2025-05-26 09:42:01 +02:00
|
|
|
@Getter
|
2022-07-31 20:41:56 +02:00
|
|
|
private final String preRelease;
|
2025-05-26 09:42:01 +02:00
|
|
|
@Getter
|
2022-07-31 20:41:56 +02:00
|
|
|
private final String build;
|
2022-06-08 16:01:06 +02:00
|
|
|
|
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
|
|
|
public SemanticVersion(int majorVersion, int minorVersion, int patchVersion, String preRelease) {
|
|
|
|
|
this(majorVersion, minorVersion, patchVersion, preRelease, null);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 13:03:23 +02:00
|
|
|
public SemanticVersion(int majorVersion, int minorVersion, int patchVersion) {
|
|
|
|
|
this(majorVersion, minorVersion, patchVersion, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-26 09:42:01 +02:00
|
|
|
/**
|
|
|
|
|
* @since 0.10.0
|
|
|
|
|
*/
|
2022-08-17 20:08:46 +02:00
|
|
|
public SemanticVersion(int majorVersion, int minorVersion) {
|
|
|
|
|
this(majorVersion, minorVersion, -1, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-26 09:42:01 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 0.10.0
|
|
|
|
|
*/
|
2022-08-17 20:08:46 +02:00
|
|
|
public SemanticVersion(int majorVersion) {
|
|
|
|
|
this(majorVersion, -1, -1, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2025-05-26 09:42:01 +02:00
|
|
|
/**
|
|
|
|
|
* @since 0.10.0
|
|
|
|
|
*/
|
2022-08-17 20:08:46 +02:00
|
|
|
public static class SemanticVersionBuilder {
|
|
|
|
|
private int majorVersion;
|
|
|
|
|
private int minorVersion = -1;
|
|
|
|
|
private int patchVersion = -1;
|
|
|
|
|
private String preRelease;
|
|
|
|
|
private String build;
|
|
|
|
|
|
2025-05-26 09:42:01 +02:00
|
|
|
@ApiStatus.Internal
|
2022-08-17 20:08:46 +02:00
|
|
|
SemanticVersionBuilder() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SemanticVersionBuilder majorVersion(int majorVersion) {
|
|
|
|
|
this.majorVersion = majorVersion;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SemanticVersionBuilder minorVersion(int minorVersion) {
|
|
|
|
|
this.minorVersion = minorVersion;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SemanticVersionBuilder patchVersion(int patchVersion) {
|
|
|
|
|
this.patchVersion = patchVersion;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SemanticVersionBuilder preRelease(String preRelease) {
|
|
|
|
|
this.preRelease = preRelease;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SemanticVersionBuilder build(String build) {
|
|
|
|
|
this.build = build;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|