dtonhofer

dtonhofer

Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 193 ff “Refactoring Data Grouping Operations”

As usual, but the original code to be refactored was just too evil and had to be fixed.

package chapter11;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toSet;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DataGroupingOperationsTest {

    interface Scores {

        Map<Integer, Set<String>> namesForScores(final Map<String, Integer> scores);

    }

    public static class ScoresBefore implements Scores {

        public Map<Integer, Set<String>> namesForScores(final Map<String, Integer> scores) {
            final Map<Integer, Set<String>> result = new HashMap<>();
            for (Map.Entry<String, Integer> entry : scores.entrySet()) {
                final String name = entry.getKey();
                final Integer score = entry.getValue();
                if (!result.containsKey(score)) {
                    result.put(score, new HashSet<>());
                }
                result.get(score).add(name);
            }
            return result;
        }
    }

    public static class ScoresAfter implements Scores {

        public Map<Integer, Set<String>> namesForScores(final Map<String, Integer> scores) {
            return scores.keySet().stream()
                    .collect(groupingBy(scores::get, toSet()));
        }
    }

    private static void commonNamesForScoresTests(final Scores scores) {
        assertAll(
                () -> assertEquals(Map.of(), scores.namesForScores(Map.of())),
                () -> assertEquals(
                        Map.of(1, Set.of("Jill")), scores.namesForScores(Map.of("Jill", 1))),
                () -> assertEquals(
                        Map.of(1, Set.of("Jill"), 2, Set.of("Paul")),
                        scores.namesForScores(Map.of("Jill", 1, "Paul", 2))),
                () -> assertEquals(
                        Map.of(1, Set.of("Jill", "Kate"), 2, Set.of("Paul")),
                        scores.namesForScores(Map.of("Jill", 1, "Paul", 2, "Kate", 1)))
        );
    }

    @Test
    void scoresBefore() {
        commonNamesForScoresTests(new ScoresBefore());
    }

    @Test
    void scoresAfter() {
        commonNamesForScoresTests(new ScoresAfter());
    }
}

Where Next?

Popular Pragmatic Bookshelf topics Top

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
New
belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New

Other popular topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
PragmaticBookshelf
Create efficient, elegant software tests in pytest, Python's most powerful testing framework. Brian Okken @brianokken Edited by Kat...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: