-
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
Changes from 3 commits
b6256e8
df79663
1573d7e
5d68f41
0f6aa3f
ae780a2
f76a366
e4325ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
|
|
||
| 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 { | ||
|
|
||
| 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 correctly") | ||||||||||||||||||
|
JavaSaBr marked this conversation as resolved.
Outdated
|
||||||||||||||||||
| 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); | |
| } | |
| })); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package javasabr.rlib.common; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| public interface AliasedEnum<T extends Enum<T>> { | ||
|
|
||
|
JavaSaBr marked this conversation as resolved.
|
||
| Collection<String> aliases(); | ||
| } | ||
| 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("Detect duplicated alias:[%s] for [%s] and [%s]".formatted( | ||
|
JavaSaBr marked this conversation as resolved.
Outdated
|
||
| 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; | ||
| } | ||
|
Comment on lines
+13
to
+51
|
||
| } | ||
| 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("unkonwn")).isNull(); | ||
|
JavaSaBr marked this conversation as resolved.
Outdated
|
||
| 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); | ||
| } | ||
| } | ||
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.