Giter Club home page Giter Club logo

fluent's Introduction

Fluent

Fluent is an ultra-lightweight Java library to aid fluent coding in Java 8 onwards

Build Status Maven Central

Map Building

Classes to aid Map building in a declarative way. A bit nicer than the anonymous sub-class with initializer block approach, don't you think?

// simple usage, assuming someMap is a Map<String, String> already declared
Map<String, String> example = new Fluent.LinkedHashMap<String, String>()
    .append("key1", "val1")
    .append("key2", "val2")
    .appendAll(someMap);

// nested structures declared all at once
Map users = new Fluent.HashMap<>()
    .append("David", new Fluent.HashMap<>()
        .append("customers", asList(
            new Fluent.HashMap<>()
                .append("name", "Darrel")
                .append("age", 33),
            new Fluent.HashMap<>()
                .append("name", "John")
                .append("age", 29)))
        .append("keywords", asList("word1", "word2")))
    .append("Karen", new Fluent.HashMap<>()
        .append("customers", emptyList())
        .append("keywords", asList("word33")));

// fluent immutable maps
Map immutable = new Fluent.HashMap<>()
    .append("one", 1)
    .append("two", 2)
    .append("three", 3)
    .unmodifiable();

Checked Exception Handling With Functional Wrapping

Utility methods to wrap the propagation of a possible checked exception in an unchecked one. Particularly useful in making fluent functional code involving checked exceptions not a horror to behold.

// in short Unchecker#unchecked lets you do this
unchecked(() -> methodThatThrowsACheckedException());

// which is equivalent to
try {
    methodThatThrowsACheckedException();
}
catch (RuntimeException | Error e) {
    throw e;
}
catch (Throwable t) {
    throw new RuntimeException(t);
}

Dealing with checked exception mad code in lambdas is messy, take for example these methods

// some Exception throwing instance
interface CheckMe {
    void youMustHandleMyErrorCases() throws Exception;
    List<Integer> atLeastWriteThrowsAllOverYourCode() throws Throwable;
}

// some exception throwing utility class
class ImportantUtility {
    public static String dontIgnoreMeeeee(Integer i)
        throws IOException, GeneralSecurityException, NoSuchMethodException {
        /* some important logic ... */
    }
}

This logic could cause the following mess in simple handler code

List<String> uncheckThrowerManual(CheckMe object) {
    try {
        object.youMustHandleMyErrorCases();
        return object.atLeastWriteThrowsAllOverYourCode().stream()
            .map(number -> {
                try {
                    return ImportantUtility.dontIgnoreMeeeee(number);
                }
                catch (IOException | GeneralSecurityException | NoSuchMethodException ex) {
                    throw new RuntimeException(ex);
                }
            })
            .collect(toList());
    }
    catch (RuntimeException | Error e) {
        throw e;
    }
    catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    }
}

We can clean this up with Java 8 & Fluent

/*
 * Unchecker#uncheckedGet returns a throwing supplier's result
 *          (wrapping checked in RuntimeExceptions by default)
 * Unchecker#uncheck transforms a throwing lambda/method into a unchecked one
 */
List<String> uncheckThrower(CheckMe object) {
    return uncheckedGet(() -> {
        object.youMustHandleMyErrorCases();
        return object.atLeastWriteThrowsAllOverYourCode().stream()
            .map(uncheck(ImportantUtility::dontIgnoreMeeeee))
            .collect(toList());
    });
}

Fluent is licensed under the Apache 2.0 licence.

Releases

2.0 is the current latest release, available at maven central. Requiring JDK 1.8 or later.

<dependency>
  <groupId>com.github.alexheretic</groupId>
  <artifactId>fluent</artifactId>
  <version>2.0</version>
</dependency>

Changelog

Release 2.x

  • Add Fluent.Map#unmodifiable()

Release 1.x

  • Fluent.Map classes
  • Unchecker checked exception utility

fluent's People

Contributors

alexheretic avatar

Watchers

James Cloos avatar Cornelius Buschka 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.