Skip to content

Commit 3b68b9c

Browse files
authored
Add ability tree composite implementation (#44)
1 parent 193a996 commit 3b68b9c

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pl.mperor.lab.java.design.pattern.structural.composite;
2+
3+
public interface Ability {
4+
5+
String name();
6+
7+
int value();
8+
9+
static Ability of(String name, int value) {
10+
return new SimpleAbility(name, value);
11+
}
12+
13+
static Ability of(String name, Ability... abilities) {
14+
return new ComplexAbility(name, abilities);
15+
}
16+
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package pl.mperor.lab.java.design.pattern.structural.composite;
2+
3+
import java.util.stream.Collectors;
4+
5+
public class AbilityUtils {
6+
7+
private AbilityUtils() {
8+
}
9+
10+
public static void printAbilityTree(Ability root) {
11+
System.out.println(printAbilityTree(root, 0));
12+
}
13+
14+
private static String printAbilityTree(Ability ability, int level) {
15+
var nested = "";
16+
if (ability instanceof ComplexAbility ca) {
17+
nested = ca.abilities().stream()
18+
.map(a -> printAbilityTree(a, level + 1))
19+
.collect(Collectors.joining());
20+
}
21+
22+
return "%s%s (%d)%n%s".formatted(" ".repeat(level), ability.name(), ability.value(), nested);
23+
}
24+
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package pl.mperor.lab.java.design.pattern.structural.composite;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public record ComplexAbility(String name, List<Ability> abilities) implements Ability {
7+
8+
public ComplexAbility(String name, Ability... abilities) {
9+
this(name, Arrays.asList(abilities));
10+
}
11+
12+
@Override
13+
public int value() {
14+
return (int) abilities.stream()
15+
.mapToInt(Ability::value)
16+
.average()
17+
.orElse(0);
18+
}
19+
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package pl.mperor.lab.java.design.pattern.structural.composite;
2+
3+
public record SimpleAbility(String name, int value) implements Ability {
4+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package pl.mperor.lab.java.design.pattern.structural.composite;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class AbilityTreeCompositeTest {
7+
8+
@Test
9+
public void testCreateAbilityTree() {
10+
var physicalHealth = Ability.of("Physical Health",
11+
Ability.of("Fitness",
12+
Ability.of("Jogging", 20),
13+
Ability.of("Stretching", 40),
14+
Ability.of("Swimming", 60)
15+
),
16+
Ability.of("Sleep", 50),
17+
Ability.of("Nutrition", 30)
18+
);
19+
Assertions.assertEquals(40, physicalHealth.value());
20+
21+
var mentalHealth = Ability.of("Mental Health",
22+
Ability.of("Stress Management", 50)
23+
);
24+
Assertions.assertEquals(50, mentalHealth.value());
25+
26+
var lifeAndHealth = Ability.of("Life and Health",
27+
physicalHealth,
28+
mentalHealth,
29+
Ability.of("Social Skills", 30)
30+
);
31+
Assertions.assertEquals(40, lifeAndHealth.value());
32+
33+
AbilityUtils.printAbilityTree(lifeAndHealth);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)