How to properly return Optional<> of a method? : <p>I have read a lot of Java 8 Optional and I do understand the concept, but still get difficulties when trying to implement it myself in my code.</p>

<p>Although I have serached the web for good examples, I didn't found one with good explanation.</p>

<p>I have the next method:</p>

<pre><code>public static String getFileMd5(String filePath) throws NoSuchAlgorithmException, IOException {
    AutomationLogger.getLog().info("Trying getting MD5 hash from file: " + filePath);
    MessageDigest md = MessageDigest.getInstance("MD5");
    InputStream inputStream;
    try {
        inputStream = Files.newInputStream(Paths.get(filePath));
    } catch (NoSuchFileException e) {
        AutomationLogger.getLog().error("No such file path: " + filePath, e);
        return null;
    }

    DigestInputStream dis = new DigestInputStream(inputStream, md);
    byte[] buffer = new byte[8 * 1024];

    while (dis.read(buffer) != -1);
    dis.close();
    inputStream.close();

    byte[] output = md.digest();
    BigInteger bi = new BigInteger(1, output);
    String hashText = bi.toString(16);
    return hashText;
}
</code></pre>

<p>This simple method returns the md5 of a file, by passing it the file path.
As you can notice, if the file path doesn't exists (or wrong typed) a <strong>NoSuchFileException</strong> get thrown and the method return <strong>Null</strong>.</p>

<p>Instead of returning null, I want to use Optional, so my method should return <code>Optional &lt;String&gt;</code>, right?</p>

<ol>
<li>What is the proper way of doing it right?</li>
<li>If the returned String is null - can I use here <code>orElse()</code>, or this
kind of method should be used by the client side?</li>
</ol>
