Skip to content

Commit 2413f6d

Browse files
* docs: kata description * feat: kata/adapter-to-java-dot-util-dot-list-dot-of --------- Co-authored-by: ParanoidUser <[email protected]>
1 parent 5ec8196 commit 2413f6d

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# [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")
2+
3+
If you're an experienced Java programmer, you've likely come across `List.of`, but it often frustrates newcomers since the returned list is
4+
immutable and doesn’t allow adding, removing, or modifying elements. This happens because the object implements the `java.util.List`
5+
interface but delegates its method calls to another implementation behind the scenes, demonstrating the Adapter pattern that allows
6+
different interfaces
7+
to work together by translating method calls.
8+
9+
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
10+
how `List.of` works.
11+
12+
You cannot use:
13+
- List.of
14+
- any method named .of
15+
- the new operator, except when creating Exceptions
16+
17+
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.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import static java.util.Arrays.stream;
2+
3+
import java.util.List;
4+
5+
interface InmutableList {
6+
static <E> List<E> alternativeOf(E... varargs) {
7+
return stream(varargs).toList();
8+
}
9+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
import static org.junit.jupiter.api.Assertions.assertThrows;
3+
4+
import java.util.List;
5+
import org.junit.jupiter.api.Test;
6+
7+
class ExampleTest {
8+
@Test
9+
void createListWithSingleElement() {
10+
List<Integer> singleElementList = InmutableList.alternativeOf(42);
11+
assertEquals(1, singleElementList.size());
12+
assertEquals(42, singleElementList.get(0));
13+
}
14+
15+
@Test
16+
void createListWithMultipleElements() {
17+
List<String> multiElementList = InmutableList.alternativeOf("apple", "banana", "cherry");
18+
assertEquals(3, multiElementList.size());
19+
assertEquals("banana", multiElementList.get(1));
20+
}
21+
22+
@Test
23+
void createListWithVarargs() {
24+
List<Integer> varargsList = InmutableList.alternativeOf(1, 2, 3, 4, 5);
25+
assertEquals(5, varargsList.size());
26+
assertEquals(4, varargsList.get(3));
27+
}
28+
29+
@Test
30+
void checkImmutability() {
31+
List<String> immutableList = InmutableList.alternativeOf("one", "two", "three");
32+
assertThrows(UnsupportedOperationException.class, () -> immutableList.add("four"));
33+
}
34+
}

kata/beta/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
- [02. Sum of array's elements](02-sum-of-arrays-elements "58f475735e78fde4a2000011")
2+
- [Adapter to java.util.List.of](adapter-to-java-dot-util-dot-list-dot-of "65e5ef770a978e003e36f16a")
23
- [¡Arrest the cardiac!](arrest-the-cardiac "660af4c7fe0da42cceb4af56")
34
- [Average of Even Numbers in a List](average-of-even-numbers-in-a-list "685a10c62388b0a0220ac88d")
45
- [Bad Collection?](bad-collection "5e36fb0ffc5a260001e65a5b")

0 commit comments

Comments
 (0)