From cf592edc57bc9cc8f37e54d60c26f2290e59ad29 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 1 Jun 2026 17:08:14 +0200 Subject: [PATCH] feat(model): Combination recipe record + order-independent key --- .../jeanluc/adventure/model/Combination.java | 35 +++++++++++++++++++ .../adventure/model/CombinationTest.java | 28 +++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Combination.java create mode 100644 Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/CombinationTest.java diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Combination.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Combination.java new file mode 100644 index 0000000..c9c0b1c --- /dev/null +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Combination.java @@ -0,0 +1,35 @@ +package thb.jeanluc.adventure.model; + +import java.util.List; + +/** + * A data-driven item-combination recipe. Identified by an unordered pair of + * item ids ({@code a}, {@code b}). When applied it may require conditions, + * consume items, produce an item, apply effects, and print a response. + */ +public record Combination( + String a, + String b, + List requires, + List consume, + String produce, + List effects, + String response, + String failText +) { + public Combination { + requires = requires == null ? List.of() : List.copyOf(requires); + consume = consume == null ? List.of() : List.copyOf(consume); + effects = effects == null ? List.of() : List.copyOf(effects); + } + + /** Order-independent key for an item pair (sorted, '|'-joined). */ + public static String key(String x, String y) { + return x.compareTo(y) <= 0 ? x + "|" + y : y + "|" + x; + } + + /** This recipe's canonical pair key. */ + public String key() { + return key(a, b); + } +} diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/CombinationTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/CombinationTest.java new file mode 100644 index 0000000..bb3bf22 --- /dev/null +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/CombinationTest.java @@ -0,0 +1,28 @@ +package thb.jeanluc.adventure.model; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class CombinationTest { + + @Test + void keyIsOrderIndependentAndSorted() { + assertThat(Combination.key("matches", "torch")) + .isEqualTo(Combination.key("torch", "matches")) + .isEqualTo("matches|torch"); + assertThat(Combination.key("b", "a")).isEqualTo("a|b"); + } + + @Test + void nullCollectionComponentsBecomeEmpty() { + Combination c = new Combination("a", "b", null, null, null, null, "ok", null); + assertThat(c.requires()).isEmpty(); + assertThat(c.consume()).isEmpty(); + assertThat(c.effects()).isEmpty(); + assertThat(c.produce()).isNull(); + assertThat(c.key()).isEqualTo("a|b"); + } +}