Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions kata/beta/adapter-to-java-dot-util-dot-list-dot-of/README.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import static java.util.Arrays.stream;

import java.util.List;

interface InmutableList {
static <E> List<E> alternativeOf(E... varargs) {
return stream(varargs).toList();
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> singleElementList = InmutableList.alternativeOf(42);
assertEquals(1, singleElementList.size());
assertEquals(42, singleElementList.get(0));
}

@Test
void createListWithMultipleElements() {
List<String> multiElementList = InmutableList.alternativeOf("apple", "banana", "cherry");
assertEquals(3, multiElementList.size());
assertEquals("banana", multiElementList.get(1));
}

@Test
void createListWithVarargs() {
List<Integer> varargsList = InmutableList.alternativeOf(1, 2, 3, 4, 5);
assertEquals(5, varargsList.size());
assertEquals(4, varargsList.get(3));
}

@Test
void checkImmutability() {
List<String> immutableList = InmutableList.alternativeOf("one", "two", "three");
assertThrows(UnsupportedOperationException.class, () -> immutableList.add("four"));
}
}
1 change: 1 addition & 0 deletions kata/beta/index.md
Original file line number Diff line number Diff line change
@@ -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")
Expand Down