8000 exceptions-to-values.8 : Revert last, introduce using, exit properly · java-to-kotlin/code@e2beb0b · GitHub
[go: up one dir, main page]

Skip to content

Commit e2beb0b

Browse files
Duncan McGregordmcg
authored andcommitted
exceptions-to-values.8 : Revert last, introduce using, exit properly
1 parent 442f413 commit e2beb0b

File tree

2 files changed

+59
-42
lines changed

2 files changed

+59
-42
lines changed

src/main/java/travelator/marketing/HighValueCustomersMain.kt

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
11
package travelator.marketing
22

3+
import java.io.Closeable
4+
import java.io.OutputStreamWriter
35
import java.io.Reader
46
import java.io.Writer
57
import kotlin.system.exitProcess
68

79
fun main() {
8-
System.`in`.reader().use { reader ->
9-
System.out.writer().use { writer ->
10-
val errorLines = mutableListOf<ParseFailure>()
11-
val reportLines = reader
12-
.asLineSequence()
13-
.toHighValueCustomerReport {
14-
errorLines += it
15-
}
16-
if (errorLines.isNotEmpty()) {
17-
System.err.writer().use { error ->
18-
error.appendLine("Lines with errors")
19-
errorLines.asSequence().map { parseFailure ->
20-
"${parseFailure::class.simpleName} in ${parseFailure.line}"
21-
}.writeTo(error)
22-
}
23-
exitProcess(-1)
24-
} else {
25-
reportLines.writeTo(writer)
10+
val statusCode = using(
11+
System.`in`.reader(),
12+
System.out.writer(),
13+
System.err.writer()
14+
) { reader, writer, error ->
15+
val errorLines = mutableListOf<ParseFailure>()
16+
val reportLines = reader
17+
.asLineSequence()
18+
.toHighValueCustomerReport {
19+
errorLines += it
2620
}
21+
if (errorLines.isEmpty()) {
22+
reportLines.writeTo(writer)
23+
0
24+
} else {
25+
errorLines.writeTo(error)
26+
-1
2727
}
2828
}
29+
exitProcess(statusCode)
30+
}
31+
32+
inline fun <A : Closeable, B : Closeable, C : Closeable, R> using(
33+
a: A,
34+
b: B,
35+
c: C,
36+
block: (A, B, C) -> R
37+
): R =
38+
a.use {
39+
b.use {
40+
c.use {
41+
block(a, b, c)
42+
}
43+
}
44+
}
45+
46+
private fun List<ParseFailure>.writeTo(error: OutputStreamWriter) {
47+
error.appendLine("Lines with errors")
48+
asSequence().map { parseFailure ->
49+
"${parseFailure::class.simpleName} in ${parseFailure.line}"
50+
}.writeTo(error)
2951
}
3052

3153
fun Reader.asLineSequence() = buffered().lineSequence()

src/main/java/travelator/marketing/HighValueCustomersReport.kt

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package travelator.marketing
22

3-
import com.natpryce.*
3+
import com.natpryce.Failure
4+
import com.natpryce.Result
5+
import com.natpryce.Success
6+
import com.natpryce.recover
47

58
fun Sequence<String>.toHighValueCustomerReport(
69
onErrorLine: (ParseFailure) -> Unit = {}
@@ -31,29 +34,21 @@ private fun Sequence<String>.withoutHeader() = drop(1)
3134

3235
internal fun String.toCustomerData(): Result<CustomerData, ParseFailure> =
3336
split("\t").let { parts ->
34-
parts
35-
.takeUnless { it.size < 4 }
36-
.asResultOr { NotEnoughFieldsFailure(this) }
37-
.flatMap { parts ->
38-
parts[3].toIntOrNull()
39-
.asResultOr { ScoreIsNotAnIntFailure(this) }
40-
.flatMap { score: Int ->
41-
(if (parts.size == 4) 0.0
42-
else parts[4].toDoubleOrNull())
43-
.asResultOr { SpendIsNotADoubleFailure(this) }
44-
.flatMap { spend ->
45-
Success(
46-
CustomerData(
47-
id = parts[0],
48-
givenName = parts[1],
49-
familyName = parts[2],
50-
score = score,
51-
spend = spend
52-
)
53-
)
54-
}
55-
}
56-
}
37+
if (parts.size < 4)
38+
return Failure(NotEnoughFieldsFailure(this))
39+
val score = parts[3].toIntOrNull() ?:
40+
return Failure(ScoreIsNotAnIntFailure(this))
41+
val spend = if (parts.size == 4) 0.0 else parts[4].toDoubleOrNull() ?:
42+
return Failure(SpendIsNotADoubleFailure(this))
43+
Success(
44+
CustomerData(
45+
id = parts[0],
46+
givenName = parts[1],
47+
familyName = parts[2],
48+
score = score,
49+
spend = spend
50+
)
51+
)
5752
}
5853

5954
sealed class ParseFailure(open val line: String)

0 commit comments

Comments
 (0)
0