Skip to content

Commit 79271e2

Browse files
committed
KTLN-654 Code update based on received article comment.
1 parent 4ce37bf commit 79271e2

File tree

3 files changed

+63
-34
lines changed
  • kotlin-patterns/src

3 files changed

+63
-34
lines changed

kotlin-patterns/src/main/kotlin/com/baeldung/railwayorientedprogramming/ROP.kt

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,35 @@ data class Customer(val name: String, val emailAddress: String)
44

55
object ROP {
66
fun createAndSave(args: Array<String>): Result<String> {
7-
return validateName(args.toList()) then ::validateEmail then ::createCustomerDAO then ::save
7+
return Success(args.toList()) then ::parse then ::validateCustomerName then ::validateCustomerEmail then ::saveCustomer otherwise ::error
88
}
99

10-
fun validateName(inp: List<String>): Result<List<String>> {
11-
if(inp.get(0).length < 10) {
12-
return Failure("Name validation failed. Name must be greater than 10 characters.")
13-
}else{
14-
return Success(inp)
10+
private fun parse(inp: List<String>): Result<Customer> {
11+
return Success(Customer(inp.get(0), inp.get(1)))
12+
}
13+
14+
private fun validateCustomerName(customer: Customer): Result<Customer> {
15+
if (customer.name.length < 10) {
16+
return Failure("Name validation failed; name length must be greater than 10 characters")
17+
} else {
18+
return Success(customer)
1519
}
1620
}
1721

18-
fun validateEmail(inp: List<String>): Result<List<String>> {
19-
if(inp.get(1).contains("@")) {
20-
return Success(inp)
21-
}else{
22-
return Failure("Email validation failed. Email must contain '@' symbol.")
22+
private fun validateCustomerEmail(customer: Customer): Result<Customer> {
23+
if (customer.emailAddress.contains("@")) {
24+
return Success(customer)
25+
} else {
26+
return Failure("Email validation failed; email must contain the '@' symbol")
2327
}
2428
}
2529

26-
fun createCustomerDAO(inp: List<String>): Result<Customer> = Success(Customer(inp.get(0), inp.get(1)))
30+
private fun saveCustomer(customer: Customer): Result<String> {
31+
return Success("Customer successfully saved: " + customer)
32+
}
33+
34+
private fun error(message: String): Failure<String> {
35+
return Failure("Error: ${message}")
36+
}
2737

28-
fun save(customer: Customer): Result<String> { return Success("Customer successfully saved: " + customer) }
2938
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.baeldung.railwayorientedprogramming
22

33
sealed class Result<T>
4-
data class Success<T>(val value: T): Result<T>()
5-
data class Failure<T>(val errorMessage: String): Result<T>()
6-
infix fun <T,U> Result<T>.then(f: (T) -> Result<U>) =
4+
data class Success<T>(val value: T) : Result<T>()
5+
data class Failure<T>(val errorMessage: String) : Result<T>()
6+
7+
infix fun <T, U> Result<T>.then(f: (T) -> Result<U>) =
78
when (this) {
89
is Success -> f(this.value)
910
is Failure -> Failure(this.errorMessage)
1011
}
1112

12-
infix fun <T> Result<T>.otherwise(f: (String) -> Unit) =
13-
if (this is Failure) f(this.errorMessage) else Unit
13+
infix fun <T> Result<T>.otherwise(f: (String) -> Failure<T>) =
14+
when (this) {
15+
is Success -> Success(this.value)
16+
is Failure -> f(this.errorMessage)
17+
}

kotlin-patterns/src/test/java/com/baeldung/railwayorientedprogramming/ROPUnitTest.kt

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,48 @@ import org.junit.jupiter.api.Test
66
class ROPUnitTest {
77
@Test
88
fun `create and save customer successfully`() {;
9-
val customer = Customer("Zeeshan Arif Saeed"
10-
11-
val res = ROP.createAndSave(arrayOf("Zeeshan Arif Saeed"
12-
13-
assertInstanceOf(res::class.java,
14-
Success("Customer successfully saved: " + customer))
9+
val customer = Customer(
10+
"Zeeshan Arif Saeed", "[email protected]"
11+
)
12+
val res = ROP.createAndSave(
13+
arrayOf(
14+
"Zeeshan Arif Saeed", "[email protected]"
15+
)
16+
)
17+
assertInstanceOf(
18+
res::class.java,
19+
Success("Customer successfully saved: " + customer)
20+
)
1521
}
1622

1723
@Test
1824
fun `create and save customer name validatation failure`() {;
19-
val customer = Customer("Zeeshan"
20-
21-
val res = ROP.createAndSave(arrayOf("Zeeshan"
22-
23-
assertInstanceOf(res::class.java,
25+
val customer = Customer(
26+
"Zeeshan", "[email protected]"
27+
)
28+
val res = ROP.createAndSave(
29+
arrayOf(
30+
"Zeeshan", "[email protected]"
31+
)
32+
)
33+
assertInstanceOf(
34+
res::class.java,
2435
Failure<String>("Name validation failed. Name must be greater than 10 characters.")
2536
)
2637
}
2738

2839
@Test
2940
fun `create and save customer email validatation failure`() {;
30-
val customer = Customer("Zeeshan Arif Saeed"
31-
32-
val res = ROP.createAndSave(arrayOf("Zeeshan"
33-
34-
assertInstanceOf(res::class.java,
41+
val customer = Customer(
42+
"Zeeshan Arif Saeed", "[email protected]"
43+
)
44+
val res = ROP.createAndSave(
45+
arrayOf(
46+
"Zeeshan", "zeesh.arifgmail.com"
47+
)
48+
)
49+
assertInstanceOf(
50+
res::class.java,
3551
Failure<String>("Email validation failed. Email must contain '@' symbol.")
3652
)
3753
}

0 commit comments

Comments
 (0)