Giter VIP home page Giter VIP logo

jexpected's Introduction

JExpected - Expected Error Handling in Java

Usage

package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import util.function.Expected;

import util.function.Expected;

public class Demo {
    enum URLError {
        MALFORMED_URL, IO_ERROR
    }

    /**
     * Opens and read the provided URL
     *
     * @param urlString
     *            the URL as String
     * @return an Expected with the content as String, in case of an error the
     *         corresponding URLError will be returned
     */
    public static Expected<String, URLError> readURL(String urlString) {
        URL url;
        try {
            url = new URL(urlString);
            URLConnection urlConnection = url.openConnection();
            StringBuilder sb = new StringBuilder();
            try (BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()))) {
                String line;
                while ((line = br.readLine()) != null)
                    sb.append(line);
                return Expected.expected(new String(sb.toString()));
            }
        } catch (MalformedURLException e) {
            return Expected.unexpected(URLError.MALFORMED_URL);
        } catch (IOException e) {
            return Expected.unexpected(URLError.IO_ERROR);
        }
    }

    public void someFunc() {
        // Functional programming using 'ifValue', 'map' and 'bind' to process
        // expected values
        final String URLs[] = { "http://google.com", "http://bing.com", "http://yahoo.com" };
        Arrays.asList(URLs).parallelStream().map(Demo::readURL).forEach(e -> e.ifValue(System.out::println));

        // Error handling using the expected
        Expected<String, URLError> expected = readURL("<?!>");
        if (expected.hasErrorValue()) {
            URLError error = expected.getErrorValue();
            // TODO cope with error
        }
    }
}

jexpected's People

Contributors

roehrdor avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.