When handling exceptions, try-catch syntax can be used, but scala.util.Try can be used for functional implementation.
In Java, the Try class cannot be used, but if you are not interested in the exception contents, you can use the Optional class. So, in this article, I'll show you how to convert an exception-based API to an Optional-based API in Java.
The target audience for this article is:
--People who have mastered Scala --People who are not familiar with Java
It hides try-catch in the method and provides an Optional-based interface for the client.
//Method to get time from Web API
public static Optional<String> getTime() {
try {
//Set the connection destination URL and connection method.
URL url = new URL("http://api.aoikujira.com/time/get.php");
......
......
return Optional.of(time.toString());
} catch (IOException e) {
e.printStackTrace();
}
return Optional.empty();
}
The program that acquires the current time from the Web API and displays it has been implemented in the following two ways.
--Exception base --Optional base
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
System.out.println(getTime());
}
//Method to get time from Web API
private static String getTime() {
try {
//Set the connection destination URL and connection method.
URL url = new URL("http://api.aoikujira.com/time/get.php");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("GET");
http.connect();
//Read the character string from the connection destination.
BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));
StringBuilder time = new StringBuilder();
String line = "";
while((line = reader.readLine()) != null)
time.append(line);
return time.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
reader.close();
}
return "";
}
}
Rewritten so that the getTime () method returns an Optional type. From the main routine (main method), the getTime () method looks like an Optional-based interface.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Optional;
public class Main {
public static void main(String[] args) {
System.out.println(getTime().orElse(""));
}
//Method to get time from Web API
private static Optional<String> getTime() {
try {
//Set the connection destination URL and connection method.
URL url = new URL("http://api.aoikujira.com/time/get.php");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("GET");
http.connect();
//Read the character string from the connection destination.
BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));
StringBuilder time = new StringBuilder();
String line = "";
while((line = reader.readLine()) != null)
time.append(line);
reader.close();
return Optional.of(time.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
reader.close();
}
return Optional.empty();
}
}
If there is a smarter way, please comment.
Recommended Posts