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

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

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

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

Title: Web Development with Clojure, Third Edition, vB17.0 (p9)
The create table guestbook syntax suggested doesn’t seem to be accepted ...
New

A Common-Sense Guide to Data Structures and Algorithms, Second Edition by Jay Wengrow @jaywengrow
Hi,
I have the paperback version of t...
New

“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ”
Excerpt From: Br...
New

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

The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error.
lib/pento_web/live/product_l...
New

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

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

Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New

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

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

Author Spotlight
Mike Riley
@mriley
This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New

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

Author Spotlight
Erin Dees
@undees
Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New

Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New

Large Language Models like ChatGPT say The Darnedest Things.
The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New

I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc.
However, I don’t...
New

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
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /onivim
- /svelte
- /typescript
- /crystal
- /kotlin
- /c-plus-plus
- /tailwind
- /gleam
- /ocaml
- /react
- /flutter
- /elm
- /vscode
- /ash
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /zig
- /scala
- /sublime-text
- /textmate
- /nixos
- /debian
- /lisp
- /agda
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /django
- /ubuntu
- /manjaro
- /spring
- /diversity
- /nodejs
- /lua
- /slackware
- /julia
- /c
- /neovim