Move valuetypes API here temporarily
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package com.r35157.libs.valuetypes.basic;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record CurrencyType(
|
||||
UUID id,
|
||||
String name,
|
||||
String symbol
|
||||
) {
|
||||
@Override
|
||||
public @NotNull String toString() {
|
||||
return symbol;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.r35157.libs.valuetypes.basic;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Semantic Versioning (SemVer):
|
||||
* A version number has the form MAJOR.MINOR.PATCH.
|
||||
* - Increment MAJOR for incompatible API changes,
|
||||
* - Increment MINOR for added functionality in a backward-compatible way,
|
||||
* - Increment PATCH for backward-compatible bug fixes or improvements.
|
||||
* TODO: Not the whole specification is implemented yet!
|
||||
*/
|
||||
public record SemanticVersion(int major, int minor, int patch) {
|
||||
/**
|
||||
* Creates a SemanticVersion and validates that all components are valid.
|
||||
*
|
||||
* @throws IllegalArgumentException if any of {@code major}, {@code minor}, or {@code patch} is negative
|
||||
*/
|
||||
public SemanticVersion {
|
||||
initializationGuardClause(major, minor, patch);
|
||||
}
|
||||
|
||||
public static SemanticVersion of(int major, int minor, int patch) {
|
||||
return new SemanticVersion(major, minor, patch);
|
||||
}
|
||||
|
||||
public static SemanticVersion of(int major, int minor) {
|
||||
return of(major, minor, 0);
|
||||
}
|
||||
|
||||
public static SemanticVersion of(int major) {
|
||||
return of(major, 0);
|
||||
}
|
||||
|
||||
public static SemanticVersion of(@NotNull String versionStr) {
|
||||
final String s = versionStr.trim();
|
||||
|
||||
final Matcher m = SEMVER_REGEX.matcher(s);
|
||||
if (!m.matches()) {
|
||||
throw new IllegalArgumentException("Invalid semantic version: '" + versionStr + "'!");
|
||||
}
|
||||
|
||||
try {
|
||||
final int major = Integer.parseInt(m.group(1));
|
||||
final int minor = m.group(2) != null ? Integer.parseInt(m.group(2)) : 0;
|
||||
final int patch = m.group(3) != null ? Integer.parseInt(m.group(3)) : 0;
|
||||
return of(major, minor, patch);
|
||||
} catch (NumberFormatException e) {
|
||||
// Happens only with overruns
|
||||
throw new IllegalArgumentException("Invalid semantic version: '" + versionStr + "'!", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String toString() {
|
||||
return "%d.%d.%d".formatted(major, minor, patch);
|
||||
}
|
||||
|
||||
private void initializationGuardClause(int major, int minor, int patch) throws IllegalArgumentException {
|
||||
if (major < 0) throw new IllegalArgumentException("Version element 'major' cannot be negative - was '" + major + "'!");
|
||||
if (minor < 0) throw new IllegalArgumentException("Version element 'minor' cannot be negative - was '" + minor + "'!");
|
||||
if (patch < 0) throw new IllegalArgumentException("Version element 'patch' cannot be negative - was '" + patch + "'!");
|
||||
}
|
||||
|
||||
private static final Pattern SEMVER_REGEX =
|
||||
Pattern.compile("^(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?$");
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.r35157.libs.valuetypes.basic;
|
||||
|
||||
public record SmtpConfiguration(
|
||||
NetworkEndPoint networkEndPoint,
|
||||
Credentials credentials
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.r35157.libs.valuetypes.basic;
|
||||
|
||||
public record TradingPair(
|
||||
CurrencyType base, // The thing you are buying or selling.
|
||||
CurrencyType quote // The currency/unit used to price the base asset.
|
||||
) { }
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.r35157.libs.valuetypes.basic;
|
||||
|
||||
public record Credentials(
|
||||
ΩUserNameΩ userName,
|
||||
ΩPasswordΩ password
|
||||
) {}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.r35157.libs.valuetypes.basic;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public record MoneyAmount(
|
||||
ΩAmountΩ amount,
|
||||
CurrencyType currencyType
|
||||
) {
|
||||
@Override
|
||||
public @NotNull String toString() {
|
||||
return amount + " " + currencyType();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.r35157.libs.valuetypes.basic;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public record MoneyPrice(
|
||||
ΩPriceΩ price,
|
||||
CurrencyType currencyType
|
||||
) { }
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.r35157.libs.valuetypes.basic;
|
||||
|
||||
public record NetworkEndPoint(
|
||||
ΩHostnameΩ hostName,
|
||||
ΩportNumberΩ portNumber
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.r35157.libs.valuetypes.basic;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record Range<T extends Comparable<T>>(
|
||||
T from,
|
||||
boolean fromIncluding,
|
||||
T to,
|
||||
boolean toIncluding
|
||||
) {
|
||||
public Range {
|
||||
Objects.requireNonNull(from, "from");
|
||||
Objects.requireNonNull(to, "to");
|
||||
|
||||
if (from.compareTo(to) > 0) {
|
||||
throw new IllegalArgumentException("Range from-value must not be greater than to-value.");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean contains(T value) {
|
||||
Objects.requireNonNull(value, "value");
|
||||
|
||||
int fromComparison = value.compareTo(from);
|
||||
int toComparison = value.compareTo(to);
|
||||
|
||||
boolean afterFrom = fromIncluding
|
||||
? fromComparison >= 0
|
||||
: fromComparison > 0;
|
||||
|
||||
boolean beforeTo = toIncluding
|
||||
? toComparison <= 0
|
||||
: toComparison < 0;
|
||||
|
||||
return afterFrom && beforeTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String toString() {
|
||||
return (fromIncluding() ? "[" : "(") + from + "," + to + (toIncluding() ? "]" : ")");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user