From 2306fbe20d9687f419135f9befb3780fbcd47dd0 Mon Sep 17 00:00:00 2001 From: Jitse Goutbeek <147500739+JitseGoutbeek@users.noreply.github.com> Date: Fri, 28 Jun 2024 11:56:25 +0200 Subject: [PATCH] Update FlagFactory.java Het korter maken --- .../ch03-complexity/flags/FlagFactory.java | 67 ++++++------------- 1 file changed, 19 insertions(+), 48 deletions(-) diff --git a/training-assignments/src/java/eu/sig/training/ch03-complexity/flags/FlagFactory.java b/training-assignments/src/java/eu/sig/training/ch03-complexity/flags/FlagFactory.java index 9b3e398..e056b01 100755 --- a/training-assignments/src/java/eu/sig/training/ch03-complexity/flags/FlagFactory.java +++ b/training-assignments/src/java/eu/sig/training/ch03-complexity/flags/FlagFactory.java @@ -1,55 +1,26 @@ -package eu.sig.training.ch03; - -import java.awt.Color; -import java.util.Arrays; import java.util.List; +import java.util.Map; +import java.util.Collections; +import static java.util.Map.entry; public class FlagFactory { + private static final Map> FLAG_COLORS = Map.ofEntries( + entry(Nationality.DUTCH, List.of(Color.RED, Color.WHITE, Color.BLUE)), + entry(Nationality.GERMAN, List.of(Color.BLACK, Color.RED, Color.YELLOW)), + entry(Nationality.BELGIAN, List.of(Color.BLACK, Color.YELLOW, Color.RED)), + entry(Nationality.FRENCH, List.of(Color.BLUE, Color.WHITE, Color.RED)), + entry(Nationality.ITALIAN, List.of(Color.GREEN, Color.WHITE, Color.RED)), + entry(Nationality.ROMANIA, List.of(Color.BLUE, Color.YELLOW, Color.RED)), + entry(Nationality.IRELAND, List.of(Color.GREEN, Color.WHITE, Color.ORANGE)), + entry(Nationality.HUNGARIAN, List.of(Color.RED, Color.WHITE, Color.GREEN)), + entry(Nationality.BULGARIAN, List.of(Color.WHITE, Color.GREEN, Color.RED)), + entry(Nationality.RUSSIA, List.of(Color.WHITE, Color.BLUE, Color.RED)), + entry(Nationality.UKRAINE, List.of(Color.BLUE, Color.YELLOW)) + ); + + private static final List DEFAULT_FLAG = List.of(Color.GRAY); - // tag::getFlag[] public List getFlagColors(Nationality nationality) { - List result; - switch (nationality) { - case DUTCH: - result = Arrays.asList(Color.RED, Color.WHITE, Color.BLUE); - break; - case GERMAN: - result = Arrays.asList(Color.BLACK, Color.RED, Color.YELLOW); - break; - case BELGIAN: - result = Arrays.asList(Color.BLACK, Color.YELLOW, Color.RED); - break; - case FRENCH: - result = Arrays.asList(Color.BLUE, Color.WHITE, Color.RED); - break; - case ITALIAN: - result = Arrays.asList(Color.GREEN, Color.WHITE, Color.RED); - break; - case ROMANIA: - result = Arrays.asList(Color.BLUE, Color.YELLOW, Color.RED); - break; - case IRELAND: - result = Arrays.asList(Color.GREEN, Color.WHITE, Color.ORANGE); - break; - case HUNGARIAN: - result = Arrays.asList(Color.RED, Color.WHITE, Color.GREEN); - break; - case BULGARIAN: - result = Arrays.asList(Color.WHITE, Color.GREEN, Color.RED); - break; - case RUSSIA: - result = Arrays.asList(Color.WHITE, Color.BLUE, Color.RED); - break; - case UKRAINE: - result = Arrays.asList(Color.BLUE, Color.YELLOW); - break; - case UNCLASSIFIED: - default: - result = Arrays.asList(Color.GRAY); - break; - } - return result; + return FLAG_COLORS.getOrDefault(nationality, DEFAULT_FLAG); } - // end::getFlag[] - }