-
Notifications
You must be signed in to change notification settings - Fork 6
Extend library API part 9 #55
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b6256e8
extends API for StringUtils and Enums
JavaSaBr df79663
add sort method to arrays
JavaSaBr 1573d7e
Merge remote-tracking branch 'origin/develop' into extend-api-v9
JavaSaBr 5d68f41
Update rlib-common/src/main/java/javasabr/rlib/common/AliasedEnum.java
JavaSaBr 0f6aa3f
Update rlib-collections/src/main/java/javasabr/rlib/collections/array…
JavaSaBr ae780a2
Update rlib-common/src/main/java/javasabr/rlib/common/util/AliasedEnu…
JavaSaBr f76a366
Update rlib-common/src/test/java/javasabr/rlib/common/util/AliasedEnu…
JavaSaBr e4325ef
Update rlib-collections/src/test/java/javasabr/rlib/collections/array…
JavaSaBr 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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 |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ repositories { | |
| } | ||
|
|
||
| ext { | ||
| rlibVersion = "10.0.alpha9" | ||
| rlibVersion = "10.0.alpha10" | ||
| } | ||
|
|
||
| dependencies { | ||
|
|
||
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| rootProject.version = "10.0.alpha9" | ||
| rootProject.version = "10.0.alpha10" | ||
| group = 'javasabr.rlib' | ||
|
|
||
| allprojects { | ||
|
|
||
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
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
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
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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||||||||||||||
| package javasabr.rlib.collections.array; | ||||||||||||||||||
|
|
||||||||||||||||||
| import java.util.Comparator; | ||||||||||||||||||
| import java.util.List; | ||||||||||||||||||
| import java.util.stream.Stream; | ||||||||||||||||||
| import org.junit.jupiter.api.Assertions; | ||||||||||||||||||
|
|
@@ -253,6 +254,36 @@ void shouldRenderToStringCorrectly(MutableArray<String> mutableArray) { | |||||||||||||||||
| mutableArray.toString()); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @ParameterizedTest | ||||||||||||||||||
| @MethodSource("generateMutableArrays") | ||||||||||||||||||
| @DisplayName("should sort array correctly") | ||||||||||||||||||
| void shouldSortArrayCorrectly(MutableArray<String> mutableArray) { | ||||||||||||||||||
| // given: | ||||||||||||||||||
| mutableArray.addAll(Array.of("10", "99", "5", "3", "77", "45", "25", "56")); | ||||||||||||||||||
|
|
||||||||||||||||||
| // when: | ||||||||||||||||||
| mutableArray.sort(); | ||||||||||||||||||
|
|
||||||||||||||||||
| // then: | ||||||||||||||||||
| var expected = Array.of("10", "25", "3", "45", "5", "56", "77", "99"); | ||||||||||||||||||
| Assertions.assertEquals(expected, mutableArray); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @ParameterizedTest | ||||||||||||||||||
| @MethodSource("generateMutableArrays") | ||||||||||||||||||
| @DisplayName("should sort array using comparator correctly") | ||||||||||||||||||
| void shouldSortArrayUsingComparatorCorrectly(MutableArray<String> mutableArray) { | ||||||||||||||||||
| // given: | ||||||||||||||||||
| mutableArray.addAll(Array.of("10", "99", "5", "3", "77", "45", "25", "56")); | ||||||||||||||||||
|
|
||||||||||||||||||
| // when: | ||||||||||||||||||
| mutableArray.sort(Comparator.comparingInt(Integer::parseInt)); | ||||||||||||||||||
|
||||||||||||||||||
| mutableArray.sort(Comparator.comparingInt(Integer::parseInt)); | |
| mutableArray.sort(Comparator.comparingInt(value -> { | |
| try { | |
| return Integer.parseInt(value); | |
| } catch (NumberFormatException e) { | |
| throw new IllegalArgumentException("Non-numeric value in array: " + value, e); | |
| } | |
| })); |
31 changes: 31 additions & 0 deletions
31
rlib-common/src/main/java/javasabr/rlib/common/AliasedEnum.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,31 @@ | ||
| package javasabr.rlib.common; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| /** | ||
| * Marks an {@link Enum} whose constants expose one or more string aliases. | ||
| * <p> | ||
| * Implementing enums can provide alternative string representations for each constant, | ||
| * which can be used, for example, for parsing user input, configuration values, | ||
| * or external identifiers that do not necessarily match the constant {@code name()}. | ||
| * | ||
| * @param <T> the concrete enum type implementing this interface | ||
| */ | ||
| public interface AliasedEnum<T extends Enum<T>> { | ||
|
|
||
| /** | ||
| * Returns the string aliases associated with this enum constant. | ||
| * <p> | ||
| * An alias is an alternative textual identifier for the constant, such as a | ||
| * short name, legacy name, or external code, that can be used for lookup | ||
| * or serialization instead of the enum's {@link Enum#name()}. | ||
| * <p> | ||
| * Implementations should never return {@code null}; an empty collection | ||
| * indicates that the constant has no aliases. Callers should not modify | ||
| * the returned collection unless the implementation explicitly documents | ||
| * that it is mutable. | ||
| * | ||
| * @return a non-{@code null} collection of aliases for this constant | ||
| */ | ||
| Collection<String> aliases(); | ||
| } |
52 changes: 52 additions & 0 deletions
52
rlib-common/src/main/java/javasabr/rlib/common/util/AliasedEnumMap.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,52 @@ | ||
| package javasabr.rlib.common.util; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import javasabr.rlib.common.AliasedEnum; | ||
| import lombok.AccessLevel; | ||
| import lombok.CustomLog; | ||
| import lombok.experimental.FieldDefaults; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| @CustomLog | ||
| @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) | ||
| public class AliasedEnumMap<T extends Enum<T> & AliasedEnum<T>> { | ||
|
|
||
| Map<String, T> aliasToValue; | ||
|
|
||
| public AliasedEnumMap(Class<T> enumClass) { | ||
| var enumConstants = enumClass.getEnumConstants(); | ||
| var aliasToValue = new HashMap<String, T>(); | ||
|
|
||
| for (T enumConstant : enumConstants) { | ||
| for (String alias : enumConstant.aliases()) { | ||
| T previous = aliasToValue.put(alias, enumConstant); | ||
| if (previous != null) { | ||
| throw new IllegalArgumentException("Detected duplicated alias:[%s] for [%s] and [%s]".formatted( | ||
| alias, | ||
| previous.name(), | ||
| enumConstant.name())); | ||
| } | ||
| } | ||
| } | ||
| this.aliasToValue = Map.copyOf(aliasToValue); | ||
| } | ||
|
|
||
| @Nullable | ||
| public T resolve(String alias) { | ||
| return aliasToValue.get(alias); | ||
| } | ||
|
|
||
| public T resolve(String alias, T def) { | ||
| T resolved = resolve(alias); | ||
| return resolved == null ? def : resolved; | ||
| } | ||
|
|
||
| public T require(String alias) { | ||
| T constant = resolve(alias); | ||
| if (constant == null) { | ||
| throw new IllegalArgumentException("Unknown enum constant for alias:[%s]".formatted(alias)); | ||
| } | ||
| return constant; | ||
| } | ||
| } |
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
63 changes: 63 additions & 0 deletions
63
rlib-common/src/test/java/javasabr/rlib/common/util/AliasedEnumMapTest.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,63 @@ | ||
| package javasabr.rlib.common.util; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import javasabr.rlib.common.AliasedEnum; | ||
| import org.jspecify.annotations.NonNull; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class AliasedEnumMapTest { | ||
|
|
||
| public enum TestEnum implements AliasedEnum<@NonNull TestEnum> { | ||
| CONSTANT1(Set.of("cnst1", "constant1", "CONSTANT1")), | ||
| CONSTANT2(Set.of("cnst2", "constant2", "CONSTANT2")), | ||
| CONSTANT3(Set.of("cnst3", "constant3", "CONSTANT3")), | ||
| CONSTANT4(Set.of("cnst4", "constant4", "CONSTANT4")), | ||
| CONSTANT5(Set.of("cnst5", "constant5", "CONSTANT5")); | ||
|
|
||
| private static final AliasedEnumMap<TestEnum> MAP = new AliasedEnumMap<>(TestEnum.class); | ||
|
|
||
| private final Collection<String> aliases; | ||
|
|
||
| TestEnum(Collection<String> aliases) { | ||
| this.aliases = aliases; | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<String> aliases() { | ||
| return aliases; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldResolveEnumByAlias() { | ||
| // when\then: | ||
| assertThat(TestEnum.MAP.resolve("cnst1")).isEqualTo(TestEnum.CONSTANT1); | ||
| assertThat(TestEnum.MAP.resolve("constant1")).isEqualTo(TestEnum.CONSTANT1); | ||
| assertThat(TestEnum.MAP.resolve("CONSTANT1")).isEqualTo(TestEnum.CONSTANT1); | ||
| assertThat(TestEnum.MAP.resolve("cnst2")).isEqualTo(TestEnum.CONSTANT2); | ||
| assertThat(TestEnum.MAP.resolve("CONSTANT2")).isEqualTo(TestEnum.CONSTANT2); | ||
| assertThat(TestEnum.MAP.resolve("constant3")).isEqualTo(TestEnum.CONSTANT3); | ||
| assertThat(TestEnum.MAP.resolve("CONSTANT4")).isEqualTo(TestEnum.CONSTANT4); | ||
| assertThat(TestEnum.MAP.resolve("unknown")).isNull(); | ||
| assertThat(TestEnum.MAP.resolve("")).isNull(); | ||
| assertThat(TestEnum.MAP.resolve("unknown", TestEnum.CONSTANT4)).isEqualTo(TestEnum.CONSTANT4); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldRequireEnumByAlias() { | ||
| // when\then: | ||
| assertThat(TestEnum.MAP.require("cnst1")).isEqualTo(TestEnum.CONSTANT1); | ||
| assertThat(TestEnum.MAP.require("CONSTANT2")).isEqualTo(TestEnum.CONSTANT2); | ||
| assertThat(TestEnum.MAP.require("cnst3")).isEqualTo(TestEnum.CONSTANT3); | ||
| assertThat(TestEnum.MAP.require("constant4")).isEqualTo(TestEnum.CONSTANT4); | ||
| assertThatThrownBy(() -> TestEnum.MAP.require("unknown")) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| assertThatThrownBy(() -> TestEnum.MAP.require("")) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing documentation for public API methods. The newly added sort methods should have Javadoc comments describing their behavior, parameters, and any exceptions they might throw. For example, the
sort()method should document that it sorts elements in their natural order (if Comparable) or by hashcode, and that ConcurrentModificationException might be thrown in concurrent contexts.