Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
hprose.util.concurrent.Promise Maven / Gradle / Ivy
Go to download
Hprose is a High Performance Remote Object Service Engine.
It is a modern, lightweight, cross-language, cross-platform, object-oriented, high performance, remote dynamic communication middleware. It is not only easy to use, but powerful. You just need a little time to learn, then you can use it to easily construct cross language cross platform distributed application system.
Hprose supports many programming languages, for example:
* AAuto Quicker
* ActionScript
* ASP
* C++
* Dart
* Delphi/Free Pascal
* dotNET(C#, Visual Basic...)
* Golang
* Java
* JavaScript
* Node.js
* Objective-C
* Perl
* PHP
* Python
* Ruby
* ...
Through Hprose, You can conveniently and efficiently intercommunicate between those programming languages.
This project is the implementation of Hprose for Java.
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* Promise.java *
* *
* Promise class for Java. *
* *
* LastModified: Jun 19, 2016 *
* Author: Ma Bingyao *
* *
\**********************************************************/
package hprose.util.concurrent;
import hprose.util.JdkVersion;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
public final class Promise implements Resolver, Rejector, Thenable {
private final static ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
static {
Threads.registerShutdownHandler(new Runnable() {
public void run() {
timer.shutdown();
}
});
}
private final ConcurrentLinkedQueue> subscribers = new ConcurrentLinkedQueue>();
private volatile State state = State.PENDING;
private volatile Object value;
private volatile Throwable reason;
public Promise() {}
public Promise(final Callable computation) {
timer.execute(new Runnable() {
public void run() {
try {
Promise.this.resolve(computation.call());
}
catch (Throwable e) {
Promise.this.reject(e);
}
}
});
}
public Promise(Executor executor) {
executor.exec((Resolver)this, (Rejector)this);
}
public final static Promise value(T value) {
Promise promise = new Promise();
promise.resolve(value);
return promise;
}
public final static Promise value(Promise value) {
Promise promise = new Promise();
promise.resolve(value);
return promise;
}
public final static Promise error(Throwable reason) {
Promise promise = new Promise();
promise.reject(reason);
return promise;
}
public final static Promise delayed(long duration, TimeUnit timeunit, final Callable computation) {
final Promise promise = new Promise();
timer.schedule(new Runnable() {
public void run() {
try {
promise.resolve(computation.call());
}
catch (Throwable e) {
promise.reject(e);
}
}
}, duration, timeunit);
return promise;
}
public final static Promise delayed(long duration, TimeUnit timeunit, final T value) {
final Promise promise = new Promise();
timer.schedule(new Runnable() {
public void run() {
promise.resolve(value);
}
}, duration, timeunit);
return promise;
}
public final static Promise delayed(long duration, TimeUnit timeunit, final Promise value) {
final Promise promise = new Promise();
timer.schedule(new Runnable() {
public void run() {
promise.resolve(value);
}
}, duration, timeunit);
return promise;
}
public final static Promise delayed(long duration, Callable computation) {
return delayed(duration, TimeUnit.MILLISECONDS, computation);
}
public final static Promise delayed(long duration, T value) {
return delayed(duration, TimeUnit.MILLISECONDS, value);
}
public final static Promise delayed(long duration, Promise value) {
return delayed(duration, TimeUnit.MILLISECONDS, value);
}
public final static Promise sync(Callable computation) {
try {
return value(computation.call());
}
catch (Throwable e) {
return error(e);
}
}
public final static boolean isThenable(Object value) {
return value instanceof Thenable;
}
public final static boolean isPromise(Object value) {
return value instanceof Promise;
}
public final static Promise> toPromise(Object value) {
return isPromise(value) ? (Promise>)value : value(value);
}
@SuppressWarnings("unchecked")
private static void allHandler(final Promise promise, final AtomicInteger count, final T[] result, Object element, final int i) {
((Promise)toPromise(element)).then(
new Action() {
public void call(T value) throws Throwable {
result[i] = value;
if (count.decrementAndGet() == 0) {
promise.resolve(result);
}
}
},
new Action() {
public void call(Throwable e) throws Throwable {
promise.reject(e);
}
}
);
}
@SuppressWarnings("unchecked")
public final static Promise all(Object[] array, Class type) {
int n = array.length;
T[] result = (type == Object.class) ?
(T[])(new Object[n]) :
(T[])Array.newInstance(type, n);
if (n == 0) return (Promise)value(result);
AtomicInteger count = new AtomicInteger(n);
Promise promise = new Promise();
for (int i = 0; i < n; ++i) {
allHandler(promise, count, result, array[i], i);
}
return promise;
}
public final static Promise all(Object[] array) {
return all(array, Object.class);
}
@SuppressWarnings("unchecked")
public final static Promise all(Promise promise, final Class type) {
return (Promise)promise.then(new Func() {
public Object call(Object array) throws Throwable {
return all((Object[])array, type);
}
});
}
public final static Promise all(Promise promise) {
return all(promise, Object.class);
}
@SuppressWarnings("unchecked")
public final Promise all(Class type) {
return all((Promise)this, type);
}
@SuppressWarnings("unchecked")
public final Promise all() {
return all((Promise)this);
}
public final static Promise join(Object...args) {
return all(args);
}
@SuppressWarnings("unchecked")
public final static Promise race(Object[] array, Class type) {
Promise promise = new Promise();
for (int i = 0, n = array.length; i < n; ++i) {
((Promise)toPromise(array[i])).fill(promise);
}
return promise;
}
public final static Promise> race(Object[] array) {
return race(array, Object.class);
}
@SuppressWarnings("unchecked")
public final static Promise race(Promise promise, final Class type) {
return (Promise)promise.then(new Func, Object[]>() {
public Promise call(Object[] array) throws Throwable {
return race(array, type);
}
});
}
public final static Promise> race(Promise promise) {
return race(promise, Object.class);
}
@SuppressWarnings("unchecked")
public final Promise race(Class type) {
return race((Promise)this, type);
}
@SuppressWarnings("unchecked")
public final Promise> race() {
return race((Promise)this);
}
@SuppressWarnings("unchecked")
public final static Promise any(Object[] array, Class type) {
int n = array.length;
if (n == 0) {
return (Promise)Promise.error(new IllegalArgumentException("any(): array must not be empty"));
}
final RuntimeException reason = new RuntimeException("any(): all promises failed");
final Promise promise = new Promise();
final AtomicInteger count = new AtomicInteger(n);
for (int i = 0; i < n; ++i) {
((Promise)toPromise(array[i])).then(
new Action() {
public void call(T value) throws Throwable {
promise.resolve(value);
}
},
new Action() {
public void call(Throwable e) throws Throwable {
if (JdkVersion.majorJavaVersion >= JdkVersion.JAVA_17) {
reason.addSuppressed(e);
}
if (count.decrementAndGet() == 0) {
promise.reject(reason);
}
}
}
);
}
return promise;
}
public final static Promise> any(Object[] array) {
return any(array, Object.class);
}
@SuppressWarnings("unchecked")
public final static Promise any(Promise promise, final Class type) {
return (Promise)promise.then(new Func, Object[]>() {
public Promise call(Object[] array) throws Throwable {
return any(array, type);
}
});
}
public final static Promise> any(Promise promise) {
return any(promise, Object.class);
}
@SuppressWarnings("unchecked")
public final Promise any(Class type) {
return any((Promise)this, type);
}
@SuppressWarnings("unchecked")
public final Promise> any() {
return any((Promise)this);
}
public final static Promise> run(Action handler, Object...args) {
return all(args).then(handler);
}
public final static Promise> run(Func, Object[]> handler, Object...args) {
return all(args).then(handler);
}
@SuppressWarnings("unchecked")
public final static Promise> forEach(final Action callback, Object...args) {
return all(args).then(new Action() {
public void call(Object[] array) throws Throwable {
for (int i = 0, n = array.length; i < n; ++i) {
callback.call((V)array[i]);
}
}
});
}
@SuppressWarnings("unchecked")
private static Action getForEachHandler(final Handler, V> callback) {
return new Action() {
public void call(Object[] array) throws Throwable {
for (int i = 0, n = array.length; i < n; ++i) {
callback.call((V)array[i], i);
}
}
};
}
public final static Promise> forEach(Object[] array, Handler, V> callback) {
return all(array).then(getForEachHandler(callback));
}
public final static Promise> forEach(Promise array, Handler, V> callback) {
return all(array).then(getForEachHandler(callback));
}
public final Promise> forEach(Handler, V> callback) {
return this.all().then(getForEachHandler(callback));
}
@SuppressWarnings("unchecked")
public final static Promise every(final Func callback, Object...args) {
return (Promise) all(args).then(new Func() {
public Boolean call(Object[] array) throws Throwable {
for (int i = 0, n = array.length; i < n; ++i) {
if (!callback.call((V)array[i])) return false;
}
return true;
}
});
}
@SuppressWarnings("unchecked")
private static Func getEveryHandler(final Handler callback) {
return new Func() {
public Boolean call(Object[] array) throws Throwable {
for (int i = 0, n = array.length; i < n; ++i) {
if (!callback.call((V)array[i], i)) return false;
}
return true;
}
};
}
@SuppressWarnings("unchecked")
public final static Promise every(Object[] array, Handler callback) {
return (Promise) all(array).then(getEveryHandler(callback));
}
@SuppressWarnings("unchecked")
public final static Promise every(Promise array, Handler callback) {
return (Promise) all(array).then(getEveryHandler(callback));
}
@SuppressWarnings("unchecked")
public final Promise every(Handler callback) {
return (Promise) this.all().then(getEveryHandler(callback));
}
@SuppressWarnings("unchecked")
public final static Promise some(final Func callback, Object...args) {
return (Promise) all(args).then(new Func() {
public Boolean call(Object[] array) throws Throwable {
for (int i = 0, n = array.length; i < n; ++i) {
if (callback.call((V)array[i])) return true;
}
return false;
}
});
}
@SuppressWarnings("unchecked")
private static Func getSomeHandler(final Handler callback) {
return new Func() {
public Boolean call(Object[] array) throws Throwable {
for (int i = 0, n = array.length; i < n; ++i) {
if (callback.call((V)array[i], i)) return true;
}
return false;
}
};
}
@SuppressWarnings("unchecked")
public final static Promise some(Object[] array, Handler callback) {
return (Promise)all(array).then(getSomeHandler(callback));
}
@SuppressWarnings("unchecked")
public final static Promise some(Promise array, Handler callback) {
return (Promise)all(array).then(getSomeHandler(callback));
}
@SuppressWarnings("unchecked")
public final Promise some(Handler callback) {
return (Promise)this.all().then(getSomeHandler(callback));
}
@SuppressWarnings("unchecked")
public final static Promise filter(final Func callback, Object...args) {
return (Promise) all(args).then(new Func() {
public Object[] call(Object[] array) throws Throwable {
int n = array.length;
ArrayList result = new ArrayList(n);
for (int i = 0; i < n; ++i) {
if (callback.call((V)array[i])) result.add(array[i]);
}
return result.toArray();
}
});
}
@SuppressWarnings("unchecked")
private static Func getFilterHandler(final Handler callback, final Class type) {
return new Func() {
public T[] call(T[] array) throws Throwable {
int n = array.length;
ArrayList result = new ArrayList(n);
for (int i = 0; i < n; ++i) {
if (callback.call((V)array[i], i)) result.add(array[i]);
}
return result.toArray((type == Object.class) ?
(T[])(new Object[result.size()]) :
(T[])Array.newInstance(type, result.size()));
}
};
}
@SuppressWarnings("unchecked")
public final static Promise filter(Object[] array, Handler callback, Class type) {
return (Promise)all(array, type).then(getFilterHandler(callback, type));
}
public final static Promise filter(Object[] array, Handler callback) {
return filter(array, callback, Object.class);
}
@SuppressWarnings("unchecked")
public final static Promise filter(Promise array, Handler callback, Class type) {
return (Promise)all(array, type).then(getFilterHandler(callback, type));
}
public final static Promise filter(Promise array, Handler callback) {
return filter(array, callback, Object.class);
}
@SuppressWarnings("unchecked")
public final Promise filter(Handler callback, Class type) {
return (Promise)this.all(type).then(getFilterHandler(callback, type));
}
public final Promise filter(Handler callback) {
return filter(callback, Object.class);
}
@SuppressWarnings("unchecked")
public final static Promise map(final Func, V> callback, Object...args) {
return (Promise)all(args).then(new Func() {
public Object[] call(Object[] array) throws Throwable {
int n = array.length;
Object[] result = new Object[n];
for (int i = 0; i < n; ++i) {
result[i] = callback.call((V)array[i]);
}
return result;
}
});
}
@SuppressWarnings("unchecked")
private static Func getMapHandler(final Handler callback, final Class type) {
return new Func() {
public T[] call(Object[] array) throws Throwable {
int n = array.length;
T[] result = (type == Object.class) ?
(T[])(new Object[n]) :
(T[])Array.newInstance(type, n);
for (int i = 0; i < n; ++i) {
result[i] = callback.call((V)array[i], i);
}
return result;
}
};
}
@SuppressWarnings("unchecked")
private static Func getMapHandler(final Handler, V> callback) {
return new Func() {
public Object[] call(Object[] array) throws Throwable {
int n = array.length;
Object[] result = new Object[n];
for (int i = 0; i < n; ++i) {
result[i] = callback.call((V)array[i], i);
}
return result;
}
};
}
@SuppressWarnings("unchecked")
public final static Promise map(Object[] array, Handler callback, Class type) {
return (Promise)all(array).then(getMapHandler(callback, type));
}
@SuppressWarnings("unchecked")
public final static Promise map(Object[] array, Handler, V> callback) {
return (Promise)all(array).then(getMapHandler(callback));
}
@SuppressWarnings("unchecked")
public final static Promise map(Promise array, Handler callback, Class type) {
return (Promise)all(array).then(getMapHandler(callback, type));
}
@SuppressWarnings("unchecked")
public final static Promise map(Promise array, Handler, V> callback) {
return (Promise)all(array).then(getMapHandler(callback));
}
@SuppressWarnings("unchecked")
public final Promise map(Handler callback, Class type) {
return (Promise)this.all().then(getMapHandler(callback, type));
}
@SuppressWarnings("unchecked")
public final Promise map(Handler, V> callback) {
return (Promise)this.all().then(getMapHandler(callback));
}
@SuppressWarnings("unchecked")
private static Func getReduceHandler(final Reducer callback) {
return new Func() {
public V call(Object[] array) throws Throwable {
int n = array.length;
if (n == 0) return null;
V result = (V)array[0];
for (int i = 1; i < n; ++i) {
result = callback.call(result, (V)array[i], i);
}
return result;
}
};
}
@SuppressWarnings("unchecked")
public final static