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());
    }
}

Where Next?

Popular Pragmatic Bookshelf topics Top

jdufour
Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
New
jgchristopher
“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ” Excerpt From: Br...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
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
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
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
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
sir.laksmana_wenk
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc. However, I don’t...
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: