<dependency>
<groupId>nl.brusque.iou</groupId>
<artifactId>iou-java</artifactId>
<version>1.0.0-beta-01</version>
</dependency>
compile 'nl.brusque.iou:iou-java:1.0.0-beta-01' { transitive = true }
IOU<Integer> iou = new IOU<>();
iou.getPromise()
.then(new IThenCallable<Integer, Void>() {
@Override
public Void apply(Integer input) throws Exception {
System.out.println(input.toString());
return null;
}
});
iou.resolve(42); // prints "42"
IOU<Integer> iou = new IOU<>();
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"
IOU<Integer> iou = new IOU<>();
iou.getPromise()
.then(new IThenCallable<Integer, Integer>() {
@Override
public Integer apply(Integer integer) throws Exception {
return integer * 42;
}
})
.fail(new IThenCallable<Object, Void>() {
@Override
public Void apply(Object input) throws Exception {
System.out.println(String.format("%s I can't do that.", input));
return null;
}
});
iou.reject("I'm sorry, Dave."); // prints "I'm sorry, Dave. I can't do that."
Or if you like the A+ way better
IOU<Integer> iou = new IOU<>();
iou.getPromise()
.then(new IThenCallable<Integer, Integer>() {
@Override
public Integer apply(Integer input) throws Exception {
return input * 42;
}
}, new IThenCallable<Object, Integer>() {
@Override
public Integer apply(Object input) throws Exception {
System.out.println(String.format("%s I can't do that.", input));
return null;
}
});
iou.reject("I'm sorry, A+."); // prints "I'm sorry, A+. I can't do that."
IOU<Integer> iou = new IOU<>();
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 you do here?");
return null;
}
})
.fail(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."