Skip to content

Commit 56df564

Browse files
committed
BAEL-8198: code examples
1 parent 55875d4 commit 56df564

File tree

25 files changed

+195
-448
lines changed

25 files changed

+195
-448
lines changed

patterns-modules/data-oriented-programming/pom.xml

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,39 @@
1111

1212
<parent>
1313
<groupId>com.baeldung</groupId>
14-
<artifactId>parent-boot-2</artifactId>
15-
<version>0.0.1-SNAPSHOT</version>
16-
<relativePath>../../parent-boot-2</relativePath>
14+
<artifactId>patterns-modules</artifactId>
15+
<version>1.0.0-SNAPSHOT</version>
1716
</parent>
1817

19-
<dependencies>
20-
</dependencies>
18+
<build>
19+
<plugins>
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-compiler-plugin</artifactId>
23+
<configuration>
24+
<source>21</source>
25+
<target>21</target>
26+
<!-- Needed due to a bug with JDK 21, described here: https://issues.apache.org/jira/browse/MCOMPILER-546?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel&focusedCommentId=17767513 -->
27+
<debug>false</debug>
28+
<compilerArgs>
29+
<arg>--enable-preview</arg>
30+
</compilerArgs>
31+
</configuration>
32+
</plugin>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-surefire-plugin</artifactId>
36+
<configuration>
37+
<argLine>--enable-preview</argLine>
38+
</configuration>
39+
</plugin>
40+
41+
</plugins>
42+
</build>
2143

2244
<properties>
23-
<java.version>21</java.version>
45+
<maven.compiler.source.version>21</maven.compiler.source.version>
46+
<maven.compiler.target.version>21</maven.compiler.target.version>
2447
</properties>
2548

2649
</project>
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.patterns.dataoriented;
1+
package com.baeldung.patterns;
22

33
import java.util.List;
44
import java.util.Map;
@@ -10,6 +10,10 @@
1010
import static java.util.stream.Collectors.groupingBy;
1111

1212
public class ScoringRules {
13+
14+
private ScoringRules() {
15+
}
16+
1317
static int pairs(List<Integer> dices, int nrOfPairs) {
1418
Map<Integer, Long> frequency = dices.stream()
1519
.collect(groupingBy(identity(), counting()));
@@ -31,7 +35,7 @@ static int pairs(List<Integer> dices, int nrOfPairs) {
3135
.sum();
3236
}
3337

34-
public static Integer moreOfSameKind(List<Integer> roll, int nrOfDicesOfSameKind) {
38+
static Integer moreOfSameKind(List<Integer> roll, int nrOfDicesOfSameKind) {
3539
Map<Integer, Long> frequency = roll.stream()
3640
.collect(groupingBy(identity(), counting()));
3741

@@ -44,7 +48,7 @@ public static Integer moreOfSameKind(List<Integer> roll, int nrOfDicesOfSameKind
4448
.orElse(0);
4549
}
4650

47-
public static Integer specificValue(List<Integer> dices, Integer value) {
51+
static Integer specificValue(List<Integer> dices, Integer value) {
4852
return dices.stream()
4953
.filter(value::equals)
5054
.mapToInt(it -> it)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.patterns;
2+
3+
4+
import com.baeldung.patterns.data.Roll;
5+
import com.baeldung.patterns.data.Strategies;
6+
import com.baeldung.patterns.data.Strategies.*;
7+
import com.baeldung.patterns.data.Strategy;
8+
import com.baeldung.patterns.data.Turn;
9+
10+
import java.util.ArrayList;
11+
import java.util.HashSet;
12+
import java.util.List;
13+
import java.util.concurrent.ThreadLocalRandom;
14+
import java.util.function.Supplier;
15+
import java.util.stream.IntStream;
16+
17+
import static com.baeldung.patterns.ScoringRules.*;
18+
19+
public class Yahtzee {
20+
21+
private Yahtzee() {
22+
}
23+
24+
static Supplier<Integer> diceValueGenerator = () -> ThreadLocalRandom.current().nextInt(1, 7);
25+
26+
static Roll roll() {
27+
List<Integer> dice = IntStream.rangeClosed(1, 5)
28+
.mapToObj(__ -> randomDieValue())
29+
.toList();
30+
return new Roll(dice, 1);
31+
}
32+
33+
static Roll rerollValues(Roll roll, Integer... values) {
34+
List<Integer> valuesToReroll = new ArrayList<>(List.of(values));
35+
if (roll.rollCount() >= 3) {
36+
throw new IllegalStateException("You can re-roll 3 times at most.");
37+
}
38+
if (!new HashSet<>(roll.dice()).containsAll(valuesToReroll)) {
39+
throw new IllegalStateException("You can re-roll dice values which are not from the original roll.");
40+
}
41+
List<Integer> newDice = roll.dice()
42+
.stream()
43+
.map(it -> {
44+
if (!valuesToReroll.contains(it)) {
45+
return it;
46+
}
47+
valuesToReroll.remove(it);
48+
return randomDieValue();
49+
}).toList();
50+
51+
return new Roll(newDice, roll.rollCount() + 1);
52+
}
53+
54+
static Turn chooseStrategy(Roll roll, String strategyStr) {
55+
Strategy strategy = Strategies.fromString(strategyStr);
56+
return new Turn(roll, strategy);
57+
}
58+
59+
static int score(Turn turn) {
60+
var dices = turn.roll().dice();
61+
return switch (turn.strategy()) {
62+
case Ones __ -> specificValue(dices, 1);
63+
case Twos __ -> specificValue(dices, 2);
64+
case OnePair __ -> pairs(dices, 1);
65+
case TwoPairs __ -> pairs(dices, 2);
66+
case ThreeOfaKind __ -> moreOfSameKind(dices, 3);
67+
};
68+
}
69+
70+
private static Integer randomDieValue() {
71+
return diceValueGenerator.get();
72+
}
73+
74+
}

patterns-modules/data-oriented-programming/src/main/java/com/baeldung/patterns/chess/ChessGamePlayback.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

patterns-modules/data-oriented-programming/src/main/java/com/baeldung/patterns/chess/tokens/Token.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

patterns-modules/data-oriented-programming/src/main/java/com/baeldung/patterns/chess/tokens/TokenFactory.java

Lines changed: 0 additions & 72 deletions
This file was deleted.

patterns-modules/data-oriented-programming/src/main/java/com/baeldung/patterns/chess/tokens/Tokens.java

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.patterns.data;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
public record Roll(List<Integer> dice, int rollCount) {
7+
public Roll {
8+
if (dice.size() != 5)
9+
throw new IllegalArgumentException("A Roll needs to have exactly 5 dice.");
10+
if (dice.stream().anyMatch(die -> die < 1 || die > 6))
11+
throw new IllegalArgumentException("Dice values should be between 1 and 6.");
12+
13+
dice = Collections.unmodifiableList(dice);
14+
}
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.patterns.data;
2+
3+
public class Strategies {
4+
public record Ones() implements Strategy {
5+
}
6+
7+
public record Twos() implements Strategy {
8+
}
9+
10+
public record OnePair() implements Strategy {
11+
}
12+
13+
public record TwoPairs() implements Strategy {
14+
}
15+
16+
public record ThreeOfaKind() implements Strategy {
17+
}
18+
19+
public static Strategy fromString(String strategyString) {
20+
return switch (strategyString) {
21+
case "ONES" -> new Ones();
22+
case "TWOS" -> new Twos();
23+
case "ONE_PAIR" -> new OnePair();
24+
case "TWO_PAIRS" -> new TwoPairs();
25+
case "THREE_OF_A_KIND" -> new ThreeOfaKind();
26+
default -> throw new IllegalStateException("Unknown strategy: " + strategyString);
27+
};
28+
}
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.patterns.data;
2+
3+
import static com.baeldung.patterns.data.Strategies.*;
4+
5+
public sealed interface Strategy permits Ones, Twos, OnePair, TwoPairs, ThreeOfaKind {
6+
}

0 commit comments

Comments
 (0)