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 Pragprog topics

As per the title, thanks.
New

Title: Web Development with Clojure, Third Edition - migrations/create not working: p159
When I execute the command:
user=> (create-...
New

I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New

Dear Sophie.
I tried to do the “Authorization” exercise and have two questions:
When trying to plug in an email-service, I found the ...
New

The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New

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

Hi,
I am getting an error I cannot figure out on my test.
I have what I think is the exact code from the book, other than I changed “us...
New

Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New

Title: Agile Web Development with Rails 7: (page 70)
I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New

Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
Other popular topics

New

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

SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New

Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New

I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl:
The Ser...
New

This looks like a stunning keycap set :orange_heart:
A LEGENDARY KEYBOARD LIVES ON
When you bought an Apple Macintosh computer in the e...
New

“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin
Even when you’re ultimately right about what the future ho...
New

Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New

A Hero’s Journey with Chris Pine
Chris Pine, author of Learn to Program, Third Edition, discusses his journey to becoming a Pragm...
New

Biggest jackpot ever apparently! :upside_down_face:
I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
Latest in Pragprog
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /preact
- /flutter
- /c-plus-plus
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /nim
- /vuejs
- /emacs
- /elm
- /nerves