Skip to content

Commit 67666e4

Browse files
committed
Use 'Try' instead of 'Option' for IO operations
1 parent 3c66c9d commit 67666e4

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

csv-validator-ui/src/main/scala/uk/gov/nationalarchives/csv/validator/ui/CsvValidatorUi.scala

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,9 @@ object CsvValidatorUi extends SimpleSwingApplication {
186186
* @param s String to save to the file
187187
* @param f File to which the associated string is saved
188188
*/
189-
private def saveToFile(s: String, f: Path) : Option[IOException] = {
190-
val data : Array[Byte] = s.getBytes(UTF_8)
191-
try {
192-
Files.write(f, data, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW)
193-
None
194-
} catch {
195-
case ioe: IOException => Some(ioe)
196-
}
189+
private def saveToFile(s: String, f: Path) : Try[String] = {
190+
val data : Array[Byte] = s.getBytes(UTF_8)
191+
Try(Files.write(f, data, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW)).map(_ => "s has been written to file")
197192
}
198193

199194
case class Settings(lastCsvPath: Path, lastCsvSchemaPath: Path, lastReportPath: Path)
@@ -521,9 +516,9 @@ object CsvValidatorUi extends SimpleSwingApplication {
521516
if (uri.endsWith("/")) uri else s"$uri/"
522517
}
523518

524-
def updateFileText(path: Path): Option[IOException] = {
519+
def updateFileText(path: Path): Try[String] = {
525520
fileTextField.text = pathToUri(path)
526-
None
521+
Success("Text updated")
527522
}
528523

529524
val okButton = new Button("OK")

csv-validator-ui/src/main/scala/uk/gov/nationalarchives/csv/validator/ui/ScalaSwingHelpers.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@ import java.nio.file.Path
1717
import scala.swing.Dialog.Message
1818
import java.io.IOException
1919
import javax.swing.JTextField
20+
import scala.util.{Failure, Success, Try}
2021

2122
/**
2223
* Some simple helpers to ease
2324
* the use of scala.swing
2425
*/
2526
object ScalaSwingHelpers {
26-
private def handleSelectedFile(dialogAction: Result.Value, fileChooser: FileChooser, result: Path => Option[IOException], dialogText: String): Unit =
27+
private def handleSelectedFile(dialogAction: Result.Value, fileChooser: FileChooser, result: Path => Try[String], dialogText: String): Unit =
2728
dialogAction match {
2829
case Result.Approve =>
2930
result(fileChooser.selectedFile.toPath) match {
30-
case Some(ioe) =>
31-
ioe.printStackTrace()
32-
Dialog.showMessage(fileChooser, s"${ioe.getClass.getName}: ${ioe.getMessage}", s"Unable to ${dialogText.toLowerCase()} file", Message.Error)
33-
case None =>
31+
case Failure(e) =>
32+
e.printStackTrace()
33+
Dialog.showMessage(fileChooser, s"${e.getClass.getName}: ${e.getMessage}", s"Unable to ${dialogText.toLowerCase()} file", Message.Error)
34+
case Success(_) =>
3435
}
3536
case Result.Cancel =>
3637
}
@@ -42,9 +43,8 @@ object ScalaSwingHelpers {
4243
* @param output A text component which displays the absolute path of the chosen file
4344
* @param locateOver A component over which the FileChooser dialog should be located
4445
*/
45-
def chooseFile(fileChooser: FileChooser, output: JTextField, locateOver: Component) : Unit = {
46-
chooseFile(fileChooser, {f => output.setText(f.toAbsolutePath.toString); None}, locateOver)
47-
}
46+
def chooseFile(fileChooser: FileChooser, output: JTextField, locateOver: Component) : Unit =
47+
chooseFile(fileChooser, f => Try(output.setText(f.toAbsolutePath.toString)).map(_ => "Path updated"), locateOver)
4848

4949
/**
5050
* Opens a FileChooser and sends the result to a function
@@ -53,7 +53,7 @@ object ScalaSwingHelpers {
5353
* @param result A function which takes the chosen file
5454
* @param locateOver A component over which the FileChooser dialog should be located
5555
*/
56-
def chooseFile(fileChooser: FileChooser, result: Path => Option[IOException], locateOver: Component, dialogText: String = "OK") : Unit = {
56+
def chooseFile(fileChooser: FileChooser, result: Path => Try[String], locateOver: Component, dialogText: String = "OK") : Unit = {
5757
val showDialogAction = fileChooser.showDialog(locateOver, dialogText)
5858
handleSelectedFile(showDialogAction, fileChooser, result, dialogText)
5959
}
@@ -65,7 +65,7 @@ object ScalaSwingHelpers {
6565
* @param result A function which writes the report out
6666
* @param locateOver A component over which the FileChooser dialog should be located
6767
*/
68-
def saveFile(fileChooser: FileChooser, result: Path => Option[IOException], locateOver: Component, dialogText: String) : Unit = {
68+
def saveFile(fileChooser: FileChooser, result: Path => Try[String], locateOver: Component, dialogText: String) : Unit = {
6969
val saveDialogAction = fileChooser.showSaveDialog(locateOver)
7070
handleSelectedFile(saveDialogAction, fileChooser, result, dialogText)
7171
}

0 commit comments

Comments
 (0)