Skip to content

Commit d8ef79a

Browse files
committed
reformatting
1 parent 6b0c3c2 commit d8ef79a

File tree

57 files changed

+136
-142
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+136
-142
lines changed

either-workshop/src/main/java/Person.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Person {
1212
int id;
1313
int age;
1414
boolean active;
15-
15+
1616
Person activate() {
1717
return this.withActive(true);
1818
}
@@ -22,7 +22,7 @@ Person activate() {
2222
class PersonStats {
2323
Person person;
2424
int stats;
25-
25+
2626
static Predicate<PersonStats> matches(IntPredicate predicate) {
2727
return ps -> predicate.test(ps.stats);
2828
}

either-workshop/src/main/java/PersonRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static Either<String, Person> findById(int id) {
1111
return Either.left("cannot find person with id = " + id);
1212
}
1313
}
14-
14+
1515
static Either<String, Person> save(Person person) {
1616
return Either.right(person);
1717
}

either-workshop/src/main/java/PersonService.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public class PersonService {
2323
*/
2424
static Either<String, Person> process(Person person) {
2525
return null; // loadStats.apply(person)
26-
// filter, PersonStats.matches
27-
// getOrElse, "stats <= 15"
28-
// map, PersonStats::getPerson
29-
// map, Person.activate
30-
// flatMap, PersonRepository.save
26+
// filter, PersonStats.matches
27+
// getOrElse, "stats <= 15"
28+
// map, PersonStats::getPerson
29+
// map, Person.activate
30+
// flatMap, PersonRepository.save
3131
}
3232
}

either-workshop/src/test/groovy/Answers.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import java.time.Month
1111
import java.util.function.Consumer
1212
import java.util.function.Function
1313
import java.util.function.UnaryOperator
14-
import java.util.stream.Collectors
14+
import java.util.stream.Collectors
1515

1616
class Answers extends Specification {
1717

either-workshop/src/test/groovy/Workshop.groovy

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import java.time.Month
1111
import java.util.function.Consumer
1212
import java.util.function.Function
1313
import java.util.function.UnaryOperator
14-
import java.util.stream.Collectors
14+
import java.util.stream.Collectors
1515

1616
class Workshop extends Specification {
17-
17+
1818
def "create successful (Right) Either with value 1, then verify"() {
1919
given:
2020
Either<String, Integer> success = -1 // hint: Either.right
@@ -109,7 +109,7 @@ class Workshop extends Specification {
109109
when:
110110
Either<String, Number> sum = from1To4 // hint: sequenceRight, map, sum
111111
Either<String, Number> fail = all // hint: sequenceRight, map, sum
112-
112+
113113
then:
114114
sum.isRight()
115115
sum.get() == 10
@@ -301,14 +301,14 @@ class Workshop extends Specification {
301301
Consumer<Integer> log = {
302302
logfile << it
303303
}
304-
304+
305305
and:
306306
Function<Integer, Either<String, String>> findById = {
307307
id ->
308308
CacheRepository.findById(id)
309-
// hint: peekLeft, logfile, log
310-
// hint: orElse, DatabaseRepository.findById(id)
311-
// hint: peekLeft, logfile, log
309+
// hint: peekLeft, logfile, log
310+
// hint: orElse, DatabaseRepository.findById(id)
311+
// hint: peekLeft, logfile, log
312312
}
313313
314314
when:
@@ -337,7 +337,7 @@ class Workshop extends Specification {
337337
Consumer<Integer> log = {
338338
logfile << it
339339
}
340-
340+
341341
and:
342342
Function<Integer, Either<String, String>> findById = {
343343
id -> DatabaseRepository.findById(id)
@@ -362,7 +362,7 @@ class Workshop extends Specification {
362362
Consumer<Integer> pushToDisplay = {
363363
display << it
364364
}
365-
365+
366366
and:
367367
Consumer<Integer> process = {
368368
id -> DatabaseRepository.findById(id) // hint: orElseRun, pushToDisplay

option-workshop/src/main/java/Person.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@Value
44
class Person {
55
int age;
6-
6+
77
boolean isAdult() {
88
return age >= 18;
99
}

option-workshop/src/test/groovy/Answers.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class Answers extends Specification {
201201
Option<Integer> definedNull = Option.some()
202202
Option<Integer> empty = Option.none()
203203
and:
204-
Function<Integer, Integer> squareOrZero = { nonNull(it) ? it ** 2 : 0 }
204+
Function<Integer, Integer> squareOrZero = { nonNull(it) ? it**2 : 0 }
205205

206206
when:
207207
Option<String> definedMapped = defined.map { squareOrZero.apply(it) }

partial-function-lifting-workshop/src/main/java/ActiveUserRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void add(ActiveUser user) {
1515
boolean containsAll(Collection<Integer> ids) {
1616
return storage.keySet().containsAll(ids);
1717
}
18-
18+
1919
int count() {
2020
return storage.size();
2121
}

partial-function-lifting-workshop/src/main/java/BlockedUser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class BlockedUser {
1111
int id;
1212
int warn;
1313
LocalDate banDate;
14-
14+
1515
ActiveUser activate(Clock clock) {
1616
if (warn > 10) {
1717
throw new BusinessException("id = " + id + ": warns has to be <= 10");

partial-function-lifting-workshop/src/main/java/Increment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Increment implements PartialFunction<Integer, Integer> {
1010

1111
Range<Integer> range;
12-
12+
1313
@Override
1414
public Integer apply(Integer integer) {
1515
//if defined: x -> x + 1, otherwise -1

partial-function-lifting-workshop/src/main/java/LifterAnswer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ static <T, R> Function<T, Option<R>> lift(PartialFunction<T, R> function) {
1111

1212
static <T, R> Function<T, Option<R>> lift(Function<T, R> function) {
1313
return x -> Try.of(() -> function.apply(x)).toOption();
14-
}
15-
14+
}
15+
1616
static <T, R> Function<T, Try<R>> liftTry(Function<T, R> function) {
1717
return x -> Try.of(() -> function.apply(x));
1818
}

partial-function-lifting-workshop/src/main/java/RandomIdentityAnswer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
1010
@RequiredArgsConstructor
1111
class RandomIdentityAnswer implements PartialFunction<Integer, Integer> {
12-
12+
1313
Random random = new Random();
1414
Range<Integer> range;
1515

pattern-matching-workshop/src/main/java/credit/CreditAssessmentService.java

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.Objects;
77

88
import static io.vavr.API.*;
9-
import static io.vavr.API.$;
109
import static workshops.DecompositionAnswersPatterns.$CreditAssessSubjects;
1110

1211
public class CreditAssessmentService {

pattern-matching-workshop/src/main/java/out/Display.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class Display {
88
@Getter
99
private String message;
10-
10+
1111
public void push(String message) {
1212
this.message = message;
1313
}

pattern-matching-workshop/src/main/java/person/Person.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Person {
1515
boolean active;
1616
Account account;
1717
Address address;
18-
18+
1919
public static Predicate<Person> hasType(PersonType type) {
2020
return p -> Match(p.type).of(
2121
Case(API.$(type), true),

pattern-matching-workshop/src/main/java/person/Salary.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
1010
public class Salary {
1111
int value;
12-
12+
1313
public static Salary of(int salary) {
1414
Preconditions.checkArgument(salary >= 0);
15-
15+
1616
return new Salary(salary);
1717
}
1818
}

pattern-matching-workshop/src/main/java/person/StatsService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class StatsService {
44
public static String getFullStats(Person person) {
55
return "full stats";
66
}
7-
7+
88
public static String getStats(Person person) {
99
return "just stats";
1010
}

pattern-matching-workshop/src/main/java/workshops/DecompositionWorkshop.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@Patterns
1212
class DecompositionWorkshop {
13-
13+
1414
@Unapply
1515
// create Tuple2 of account, balance from an input person
1616
static Tuple2<Account, Address> PersonByCreditAssessSubjects(Person person) {

pattern-matching-workshop/src/main/java/workshops/Workshop.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -278,20 +278,20 @@ public static Integer personDecompose(@NonNull Person person) {
278278
/**
279279
* we often meet logic steered by set (or iterable) membership
280280
* it is usually pile of if-then-return statements
281-
* if set1.contains(x) return y1
282-
* if set2.contains(x) return y2
283-
* if set3.contains(x) return y3
284-
* otherwise throw exception
281+
* if set1.contains(x) return y1
282+
* if set2.contains(x) return y2
283+
* if set3.contains(x) return y3
284+
* otherwise throw exception
285285
* which is quite noisy and vague
286-
*
286+
* <p>
287287
* the goal of this example is to show how to rewrite it using pattern matching
288-
*
288+
* <p>
289289
* this method simply returns type (for example for front-end) based on chosen payment
290-
* CREDIT_CARD, GIFT_CARD -> "card"
291-
* CASH -> "cash"
292-
* PAYPAL -> "online"
293-
* BLIK, APPLE_PAY -> "mobile"
294-
* null -> "not paid yet"
290+
* CREDIT_CARD, GIFT_CARD -> "card"
291+
* CASH -> "cash"
292+
* PAYPAL -> "online"
293+
* BLIK, APPLE_PAY -> "mobile"
294+
* null -> "not paid yet"
295295
*/
296296
public static String isInTest(@NonNull Invoice invoice) {
297297
var paymentType = invoice.getPaymentType();
@@ -312,7 +312,7 @@ public static String isInTest(@NonNull Invoice invoice) {
312312
if (Objects.isNull(paymentType)) {
313313
return "not paid yet";
314314
}
315-
315+
316316
throw new IllegalArgumentException("value not supported: " + paymentType);
317317
}
318318

pattern-matching-workshop/src/test/groovy/AnswersTests.groovy

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import spock.lang.Specification
1010
import workshops.Answers
1111

1212
import java.time.LocalDate
13-
import java.time.format.DateTimeParseException
13+
import java.time.format.DateTimeParseException
1414

1515
class AnswersTests extends Specification {
1616
def "numberConverter"() {
@@ -221,18 +221,18 @@ class AnswersTests extends Specification {
221221
222222
when:
223223
Answers.tryDecompose('3', display)
224-
224+
225225
then:
226226
display.message == 'squared number is: 9'
227227
}
228-
228+
229229
def "tryDecompose, fail to parse an input string as a number"() {
230230
given:
231231
def display = new Display()
232232
233233
when:
234234
Answers.tryDecompose('wrong', display)
235-
235+
236236
then:
237237
display.message == 'cannot square number: For input string: "wrong"'
238238
}
@@ -289,7 +289,7 @@ class AnswersTests extends Specification {
289289
Answers.personDecompose(p2) == 328
290290
Answers.personDecompose(p3) == 508
291291
}
292-
292+
293293
def "isInTest"() {
294294
expect:
295295
Answers.isInTest(Invoice.of(PaymentType.APPLE_PAY)) == 'mobile'

pattern-matching-workshop/src/test/groovy/WorkshopTests.groovy

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import spock.lang.Specification
1010
import workshops.Workshop
1111

1212
import java.time.LocalDate
13-
import java.time.format.DateTimeParseException
13+
import java.time.format.DateTimeParseException
1414

1515
class WorkshopTests extends Specification {
1616
def "numberConverter"() {
@@ -221,18 +221,18 @@ class WorkshopTests extends Specification {
221221
222222
when:
223223
Workshop.tryDecompose('3', display)
224-
224+
225225
then:
226226
display.message == 'squared number is: 9'
227227
}
228-
228+
229229
def "tryDecompose, fail to parse an input string as a number"() {
230230
given:
231231
def display = new Display()
232232
233233
when:
234234
Workshop.tryDecompose('wrong', display)
235-
235+
236236
then:
237237
display.message == 'cannot square number: For input string: "wrong"'
238238
}

try-workshop/src/main/java/PersonRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static Try<Person> findById(int id) {
1313
return Try.failure(new EntityNotFoundException());
1414
}
1515
}
16-
16+
1717
static void save(Person person) {
1818
switch (person.getId()) {
1919
case 1:

try-workshop/src/main/java/RepositoryFacade.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static Try<String> findById(int id) {
1919
}
2020
if (id == 3) {
2121
return Try.failure(new CacheUserCannotBeFound(id));
22-
}
22+
}
2323
if (id == 4) {
2424
return Try.failure(new CacheUserCannotBeFound(id));
2525
}
@@ -46,7 +46,7 @@ static Try<String> findById(int id) {
4646

4747
class BackupRepository {
4848
static Try<String> findById(int id) {
49-
return Try.of(() -> "from backup");
49+
return Try.of(() -> "from backup");
5050
}
5151

5252
}
@@ -66,5 +66,5 @@ class DatabaseConnectionProblem extends RuntimeException {
6666
}
6767

6868
class CacheSynchronization extends RuntimeException {
69-
69+
7070
}

try-workshop/src/main/java/TWR.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
class TWR {
1010
static String usingJava(String path) throws IOException {
1111
String fileLines;
12-
12+
1313
try (var stream = Files.lines(Paths.get(path))) {
1414
fileLines = stream.collect(joining(","));
1515
}
1616

1717
return fileLines;
1818
}
19-
19+
2020
// rewrite usingJava with vavr try-with-resources
2121
static Try<String> usingVavr(String path) {
2222
return null;

0 commit comments

Comments
 (0)