Skip to content
This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
/ iou-core Public archive

IOU is a Promises/A+ compliant promise library for Java

License

Notifications You must be signed in to change notification settings

ioweyou/iou-core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IOU Core

Travis CI Coverage Status License MIT

Promises/A+ logo A [Promises/A+](https://github.com/promises-aplus/promises-spec) compliant promise library for Java, designed to be extended by other libraries like [IOU Java](https://github.com/ioweyou/iou-java) and [IOU Android](https://github.com/ioweyou/iou-android).

Table Of Contents

Implementations

Dependency management

Examples

Implementations

Dependency management

Maven


<dependency>
    <groupId>nl.brusque.iou</groupId>
    <artifactId>iou-core</artifactId>
    <version>1.0.0-beta-01</version>
</dependency>

Gradle


compile 'nl.brusque.iou:iou-core:1.0.0-beta-01'

Examples


Given the example implementation

class TestTypedIOU<TInput> extends AbstractIOU<TInput> {
  ...
}

class TestTypedPromise<TInput> extends AbstractPromise<TInput> {
  ...
}

TestTypedIOU<Integer> iou = new TestTypedIOU<>();

Call with single then

iou.getPromise()
    .then(new IThenCallable<Integer, Void>() {
        @Override
        public Void apply(Integer integer) throws Exception {
            System.out.println(integer);

            return null;
        }
    });

iou.resolve(42); // prints 42

Call with single then and Java 8 lambda

iou.getPromise()
    .then((Integer integer) -> {
        System.out.println(integer);

        return null;
    });

iou.resolve(42); // prints 42

Chained or piped promise

iou.getPromise()
    .then(new IThenCallable<Integer, Integer>() {
        @Override
        public Integer apply(Integer input) throws Exception {
            return input * 10;
        }
    })
    .then(new IThenCallable<Integer, String>() {
        @Override
        public String apply(Integer input) throws Exception {
            return String.format("The result: %d", input);
        }
    })
    .then(new IThenCallable<String, Void>() {
        @Override
        public Void apply(String input) throws Exception {
            System.out.println(input);

            return null;
        }
    });

iou.resolve(42); // prints "The result: 420"

Sequential promise

TestTypedPromise<Integer> promise = iou.getPromise();

promise
    .then(new IThenCallable<Integer, Void>() {
        @Override
        public Void apply(Integer input) throws Exception {
            System.out.println(input);

            return null;
        }
    });

promise
    .then(new IThenCallable<Integer, String>() {
        @Override
        public Void apply(Integer input) throws Exception {
            String result = String.format("%d * 10 = %d", input, input * 10);
            System.out.println(result);

            return result;
        }
    });

iou.resolve(42); // prints "42" and "42 * 10 = 420" in exactly this order

Rejecting a promise

iou.getPromise()
    .then(new IThenCallable<Integer, Integer>() {
        @Override
        public Integer apply(Integer integer) throws Exception {
            return integer * 42;
        }
    }, new IThenCallable<Object, Void>() {
        @Override
        public Void apply(Object reason) throws Exception {
            System.out.println(String.format("%s I can't do that.", reason));

            return null;
        }
    });

iou.reject("I'm sorry, Dave."); // prints "I'm sorry, Dave. I can't do that."

Failing a promise

iou.getPromise()
    .then(new IThenCallable<Integer, Integer>() {
        @Override
        public Integer apply(Integer input) throws Exception {
            throw new Exception("I just don't care.");
        }
    })
    .then(new IThenCallable<Integer, Void>() {
        @Override
        public Void apply(Integer input) throws Exception {
            System.out.println("What would you say it is you do here?");

            return null;
        }
    }, new IThenCallable<Object, Void>() {
        @Override
        public Void apply(Object reason) throws Exception {
            System.out.println(
              String.format("It's not that I'm lazy, it's that %s",
                ((Exception)reason).getMessage()));

            return null;
        }
    });

iou.resolve(42); // prints "It's not that I'm lazy, it's that I just don't care."