dtonhofer

dtonhofer

Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 187 ff “Refactoring For Each”

Nothing special here, same changes as formerly, but we are using people.isEmpty() instead of people.size() == 0 for even better legibility.

package chapter11;

import org.junit.jupiter.api.Test;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

public class ForEachTest {

    record Person(String name, int age) {
    }

    interface Agency {

        boolean isChaperoneRequired(Set<Person> people);

    }

    static class AgencyBefore implements Agency {

        public boolean isChaperoneRequired(Set<Person> people) {
            boolean required = true;
            if (people.isEmpty()) {
                required = false;
            } else {
                for (var person : people) {
                    if (person.age() >= 18) {
                        required = false;
                        break;
                    }
                }
            }
            return required;
        }
    }

    static class AgencyAfter implements Agency {

        public boolean isChaperoneRequired(Set<Person> people) {
            return !people.isEmpty() &&
                    people.stream()
                            .noneMatch(person -> person.age() >= 18);
        }
    }

    private static void commonAgencyTests(Agency agency) {
        assertAll(
                () -> assertTrue(agency.isChaperoneRequired(
                        Set.of(new Person("Jake", 12)))),
                () -> assertTrue(agency.isChaperoneRequired(
                        Set.of(new Person("Jake", 12), new Person("Pam", 14)))),
                () -> assertTrue(agency.isChaperoneRequired(
                        Set.of(new Person("Shiv", 8),
                                new Person("Sam", 9), new Person("Jill", 11)))),
                () -> assertFalse(agency.isChaperoneRequired(
                        Set.of(new Person("Jake", 12), new Person("Pam", 18)))),
                () -> assertFalse(agency.isChaperoneRequired(Set.of()))
        );
    }

    @Test
    void agencyBefore() {
        commonAgencyTests(new AgencyBefore());
    }

    @Test
    void agencyAfter() {
        commonAgencyTests(new AgencyAfter());
    }
}

Popular Pragmatic topics Top

Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
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
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New

Other popular topics Top

dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
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
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
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New

Latest in PragProg

View all threads ❯