-
Notifications
You must be signed in to change notification settings - Fork 0
add custom types #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SirCotare
merged 2 commits into
main
from
ab-238-be-extract-value-objects-to-aboutbits-java-toolbox
Sep 4, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/it/aboutbits/springboot/toolbox/type/EmailAddress.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package it.aboutbits.springboot.toolbox.type; | ||
|
|
||
| import it.aboutbits.springboot.toolbox.validation.util.EmailAddressValidator; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** | ||
| * A record representing an email address, validated and stored in lowercase. | ||
| * <p> | ||
| * The EmailAddress record ensures that the provided email address string | ||
| * is in a valid format and automatically converts it to lowercase for | ||
| * consistent internal representation. It implements the Comparable | ||
| * interface to allow comparison between different email addresses. | ||
| * </p> | ||
| * | ||
| * @param value the email address string | ||
| * @throws IllegalArgumentException if the provided email address is not in a valid format | ||
| */ | ||
| public record EmailAddress(String value) implements Comparable<EmailAddress> { | ||
| public EmailAddress(String value) { | ||
| if (value == null || EmailAddressValidator.isNotValid(value)) { | ||
| throw new IllegalArgumentException("Value is not a valid email address: " + value); | ||
| } | ||
|
|
||
| this.value = value.toLowerCase(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(@NotNull EmailAddress o) { | ||
| return value().compareTo(o.value()); | ||
| } | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
src/main/java/it/aboutbits/springboot/toolbox/type/Iban.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package it.aboutbits.springboot.toolbox.type; | ||
|
|
||
| import it.aboutbits.springboot.toolbox.validation.util.IbanValidator; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * The {@code Iban} class represents an International Bank Account Number (IBAN) and provides functionality for validation | ||
| * and comparison of IBANs. It ensures the valid format of the IBAN according to predefined rules. | ||
| * | ||
| * @param value the IBAN value, which must be a valid and non-null string | ||
| */ | ||
| public record Iban(String value) implements Comparable<Iban> { | ||
| public Iban(String value) { | ||
| if (value == null || IbanValidator.isNotValid(value.toUpperCase())) { | ||
| throw new IllegalArgumentException("Value is not a valid IBAN: " + value); | ||
| } | ||
|
|
||
| this.value = value.toUpperCase(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return value; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the ABI (Bank Branch Code) if the IBAN belongs to an Italian bank. | ||
| * | ||
| * @return An {@code Optional} containing the ABI value if the IBAN is Italian, or an empty {@code Optional} otherwise. | ||
| */ | ||
| public Optional<String> getAbiIfItalian() { | ||
| var iban = value(); | ||
|
|
||
| if (!iban.startsWith("IT")) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(iban.substring(5, 10)); | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(@NotNull Iban o) { | ||
| return value().compareTo(o.value()); | ||
| } | ||
| } |
233 changes: 233 additions & 0 deletions
233
src/main/java/it/aboutbits/springboot/toolbox/type/ScaledBigDecimal.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| package it.aboutbits.springboot.toolbox.type; | ||
|
|
||
| import lombok.NonNull; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.BigInteger; | ||
| import java.math.MathContext; | ||
| import java.math.RoundingMode; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * A record that represents a scaled BigDecimal with a fixed MathContext. | ||
| * This class provides various constructors for creating instances with different numerical types. | ||
| * It also provides a set of arithmetic operations that return new instances with the appropriate scale and rounding. | ||
| */ | ||
| public record ScaledBigDecimal( | ||
| @NonNull BigDecimal value | ||
| ) implements Comparable<ScaledBigDecimal> { | ||
| private static final MathContext MATH_CONTEXT = new MathContext(15, RoundingMode.HALF_UP); | ||
|
|
||
| public static final ScaledBigDecimal ZERO = new ScaledBigDecimal(0); | ||
| public static final ScaledBigDecimal ONE = new ScaledBigDecimal(1); | ||
| public static final ScaledBigDecimal TWO = new ScaledBigDecimal(2); | ||
| public static final ScaledBigDecimal TEN = new ScaledBigDecimal(10); | ||
|
|
||
| public ScaledBigDecimal(@NonNull BigDecimal value) { | ||
| this.value = value.setScale(MATH_CONTEXT.getPrecision(), MATH_CONTEXT.getRoundingMode()); | ||
| } | ||
|
|
||
| public ScaledBigDecimal(int value) { | ||
| this(new BigDecimal(value, MATH_CONTEXT)); | ||
| } | ||
|
|
||
| public ScaledBigDecimal(@NonNull Integer value) { | ||
| this(new BigDecimal(value, MATH_CONTEXT)); | ||
| } | ||
|
|
||
| public ScaledBigDecimal(@NonNull BigInteger value) { | ||
| this(new BigDecimal(value, MATH_CONTEXT)); | ||
| } | ||
|
|
||
| public ScaledBigDecimal(long value) { | ||
| this(new BigDecimal(value, MATH_CONTEXT)); | ||
| } | ||
|
|
||
| public ScaledBigDecimal(@NonNull Long value) { | ||
| this(new BigDecimal(value, MATH_CONTEXT)); | ||
| } | ||
|
|
||
| public ScaledBigDecimal(float value) { | ||
| this(new BigDecimal(String.valueOf(value), MATH_CONTEXT)); | ||
| } | ||
|
|
||
| public ScaledBigDecimal(@NonNull Float value) { | ||
| this(new BigDecimal(String.valueOf(value), MATH_CONTEXT)); | ||
| } | ||
|
|
||
| public ScaledBigDecimal(double value) { | ||
| this(new BigDecimal(String.valueOf(value), MATH_CONTEXT)); | ||
| } | ||
|
|
||
| public ScaledBigDecimal(@NonNull Double value) { | ||
| this(new BigDecimal(String.valueOf(value), MATH_CONTEXT)); | ||
| } | ||
|
|
||
| public ScaledBigDecimal(@NonNull String value) { | ||
| this(new BigDecimal(value, MATH_CONTEXT)); | ||
| } | ||
|
|
||
| public static ScaledBigDecimal valueOf(int value) { | ||
| return new ScaledBigDecimal(value); | ||
| } | ||
|
|
||
| public static ScaledBigDecimal valueOf(@NonNull Integer value) { | ||
| return new ScaledBigDecimal(value); | ||
| } | ||
|
|
||
| public static ScaledBigDecimal valueOf(@NonNull BigInteger value) { | ||
| return new ScaledBigDecimal(value); | ||
| } | ||
|
|
||
| public static ScaledBigDecimal valueOf(long value) { | ||
| return new ScaledBigDecimal(value); | ||
| } | ||
|
|
||
| public static ScaledBigDecimal valueOf(@NonNull Long value) { | ||
| return new ScaledBigDecimal(value); | ||
| } | ||
|
|
||
| public static ScaledBigDecimal valueOf(float value) { | ||
| return new ScaledBigDecimal(value); | ||
| } | ||
|
|
||
| public static ScaledBigDecimal valueOf(@NonNull Float value) { | ||
| return new ScaledBigDecimal(value); | ||
| } | ||
|
|
||
| public static ScaledBigDecimal valueOf(double value) { | ||
| return new ScaledBigDecimal(value); | ||
| } | ||
|
|
||
| public static ScaledBigDecimal valueOf(@NonNull Double value) { | ||
| return new ScaledBigDecimal(value); | ||
| } | ||
|
|
||
| public static ScaledBigDecimal valueOf(@NonNull String value) { | ||
| return new ScaledBigDecimal(value); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal add(@NonNull ScaledBigDecimal other) { | ||
| return new ScaledBigDecimal(this.value().add(other.value())); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal subtract(@NonNull ScaledBigDecimal other) { | ||
| return new ScaledBigDecimal(this.value().subtract(other.value())); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal multiply(@NonNull ScaledBigDecimal other) { | ||
| return new ScaledBigDecimal(this.value().multiply(other.value())); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal divide(@NonNull ScaledBigDecimal other) { | ||
| return new ScaledBigDecimal(this.value().divide(other.value(), MATH_CONTEXT)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal add(@NonNull BigDecimal other) { | ||
| return new ScaledBigDecimal(this.value().add(other)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal subtract(@NonNull BigDecimal other) { | ||
| return new ScaledBigDecimal(this.value().subtract(other)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal multiply(@NonNull BigDecimal other) { | ||
| return new ScaledBigDecimal(this.value().multiply(other)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal divide(@NonNull BigDecimal other) { | ||
| return new ScaledBigDecimal(this.value().divide(other, MATH_CONTEXT)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal remainder(@NonNull ScaledBigDecimal divisor) { | ||
| return new ScaledBigDecimal(this.value().remainder(divisor.value(), MATH_CONTEXT)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal remainder(@NonNull BigDecimal divisor) { | ||
| return new ScaledBigDecimal(this.value().remainder(divisor, MATH_CONTEXT)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal sqrt() { | ||
| return new ScaledBigDecimal(this.value().sqrt(MATH_CONTEXT)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal pow(int n) { | ||
| return new ScaledBigDecimal(this.value().pow(n, MATH_CONTEXT)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal abs() { | ||
| return new ScaledBigDecimal(this.value().abs(MATH_CONTEXT)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal negate() { | ||
| return new ScaledBigDecimal(this.value().negate(MATH_CONTEXT)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal min(@NonNull ScaledBigDecimal val) { | ||
| return new ScaledBigDecimal(this.value().min(val.value())); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal min(@NonNull BigDecimal val) { | ||
| return new ScaledBigDecimal(this.value().min(val)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal max(@NonNull ScaledBigDecimal val) { | ||
| return new ScaledBigDecimal(this.value().max(val.value())); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal max(@NonNull BigDecimal val) { | ||
| return new ScaledBigDecimal(this.value().max(val)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public BigDecimal toBigDecimal(int scale) { | ||
| return this.value().setScale(scale, RoundingMode.HALF_UP); | ||
| } | ||
|
|
||
| @NotNull | ||
| public ScaledBigDecimal roundToScale(int scale) { | ||
| return new ScaledBigDecimal(this.value().setScale(scale, RoundingMode.HALF_UP)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
|
|
||
| if (o instanceof ScaledBigDecimal other) { | ||
| return this.value().compareTo(other.value()) == 0; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(MATH_CONTEXT, value()); | ||
| } | ||
|
SirCotare marked this conversation as resolved.
|
||
|
|
||
| @Override | ||
| public int compareTo(@NotNull ScaledBigDecimal o) { | ||
| return this.value().compareTo(o.value()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.