diff --git a/kata/beta/adapter-to-java-dot-util-dot-list-dot-of/README.md b/kata/beta/adapter-to-java-dot-util-dot-list-dot-of/README.md new file mode 100644 index 00000000..66948ef1 --- /dev/null +++ b/kata/beta/adapter-to-java-dot-util-dot-list-dot-of/README.md @@ -0,0 +1,17 @@ +# [Adapter to java.util.List.of](https://www.codewars.com/kata/adapter-to-java-dot-util-dot-list-dot-of "https://www.codewars.com/kata/65e5ef770a978e003e36f16a") + +If you're an experienced Java programmer, you've likely come across `List.of`, but it often frustrates newcomers since the returned list is +immutable and doesn’t allow adding, removing, or modifying elements. This happens because the object implements the `java.util.List` +interface but delegates its method calls to another implementation behind the scenes, demonstrating the Adapter pattern that allows +different interfaces +to work together by translating method calls. + +Your task is to create a method that takes an array (or varargs) and returns a `List` structure that keeps the data immutable, similar to +how `List.of` works. + +You cannot use: + - List.of + - any method named .of + - the new operator, except when creating Exceptions + +You can find more info on [List.of()](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/List.html#of()) in the Java docs. \ No newline at end of file diff --git a/kata/beta/adapter-to-java-dot-util-dot-list-dot-of/main/InmutableList.java b/kata/beta/adapter-to-java-dot-util-dot-list-dot-of/main/InmutableList.java new file mode 100644 index 00000000..a64f1938 --- /dev/null +++ b/kata/beta/adapter-to-java-dot-util-dot-list-dot-of/main/InmutableList.java @@ -0,0 +1,9 @@ +import static java.util.Arrays.stream; + +import java.util.List; + +interface InmutableList { + static List alternativeOf(E... varargs) { + return stream(varargs).toList(); + } +} \ No newline at end of file diff --git a/kata/beta/adapter-to-java-dot-util-dot-list-dot-of/test/ExampleTest.java b/kata/beta/adapter-to-java-dot-util-dot-list-dot-of/test/ExampleTest.java new file mode 100644 index 00000000..bdf68540 --- /dev/null +++ b/kata/beta/adapter-to-java-dot-util-dot-list-dot-of/test/ExampleTest.java @@ -0,0 +1,34 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class ExampleTest { + @Test + void createListWithSingleElement() { + List singleElementList = InmutableList.alternativeOf(42); + assertEquals(1, singleElementList.size()); + assertEquals(42, singleElementList.get(0)); + } + + @Test + void createListWithMultipleElements() { + List multiElementList = InmutableList.alternativeOf("apple", "banana", "cherry"); + assertEquals(3, multiElementList.size()); + assertEquals("banana", multiElementList.get(1)); + } + + @Test + void createListWithVarargs() { + List varargsList = InmutableList.alternativeOf(1, 2, 3, 4, 5); + assertEquals(5, varargsList.size()); + assertEquals(4, varargsList.get(3)); + } + + @Test + void checkImmutability() { + List immutableList = InmutableList.alternativeOf("one", "two", "three"); + assertThrows(UnsupportedOperationException.class, () -> immutableList.add("four")); + } +} \ No newline at end of file diff --git a/kata/beta/index.md b/kata/beta/index.md index b1016f74..ef0f2865 100644 --- a/kata/beta/index.md +++ b/kata/beta/index.md @@ -1,4 +1,5 @@ - [02. Sum of array's elements](02-sum-of-arrays-elements "58f475735e78fde4a2000011") +- [Adapter to java.util.List.of](adapter-to-java-dot-util-dot-list-dot-of "65e5ef770a978e003e36f16a") - [¡Arrest the cardiac!](arrest-the-cardiac "660af4c7fe0da42cceb4af56") - [Average of Even Numbers in a List](average-of-even-numbers-in-a-list "685a10c62388b0a0220ac88d") - [Bad Collection?](bad-collection "5e36fb0ffc5a260001e65a5b")