All Downloads are FREE. Search and download functionalities are using the official Maven repository.

grails.async.Promises Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
/*
 * Copyright 2013 SpringSource
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package grails.async;


import groovy.lang.Closure;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.grails.async.decorator.PromiseDecorator;
import org.grails.async.factory.SynchronousPromiseFactory;
import org.grails.async.factory.gpars.GparsPromiseFactory;

/**
 * Factory class for working with {@link Promise} instances
 *
 * @author Graeme Rocher
 * @since 2.3
 */
public class Promises {

    static PromiseFactory promiseFactory;

    static {
        if (GparsPromiseFactory.isGparsAvailable()) {
            promiseFactory = new GparsPromiseFactory();
        }
        /*else if (ReactorPromiseFactory.isReactorAvailable()) {
            promiseFactory = new ReactorPromiseFactory()
        }*/
        else {
            promiseFactory = new SynchronousPromiseFactory();
        }
    }

    public static PromiseFactory getPromiseFactory() {
        if (promiseFactory == null) {
            if (GparsPromiseFactory.isGparsAvailable()) {
                promiseFactory = new GparsPromiseFactory();
            }
            else {
                promiseFactory = new SynchronousPromiseFactory();
            }
        }
        return promiseFactory;
    }

    public static void setPromiseFactory(PromiseFactory promiseFactory) {
        Promises.promiseFactory = promiseFactory;
    }

    /**
     * @see PromiseFactory#waitAll(grails.async.Promise[])
     */
    public static List waitAll(Promise...promises) {
        return getPromiseFactory().waitAll(promises);
    }

    /**
     * @see PromiseFactory#waitAll(java.util.List)
     */
    public static List waitAll(List> promises) {
        return getPromiseFactory().waitAll(promises);
    }

    /**
     * @see PromiseFactory#waitAll(java.util.List)
     */
    public static List waitAll(List> promises, final long timeout, final TimeUnit units) {
        return getPromiseFactory().waitAll(promises, timeout, units);
    }

    /**
     * @see PromiseFactory#onComplete(java.util.List, groovy.lang.Closure)
     */
    public static Promise> onComplete(List> promises, Closure callable ) {
        return getPromiseFactory().onComplete(promises, callable);
    }
    /**
     * @see PromiseFactory#onError(java.util.List, groovy.lang.Closure)
     */
    public static Promise> onError(List> promises, Closure callable ) {
        return getPromiseFactory().onError(promises, callable);
    }
    /**
     * @see PromiseFactory#createPromise(java.util.Map)
     */
    public static Promise> createPromise(Map map) {
        return getPromiseFactory().createPromise(map);
    }
    /**
     * @see PromiseFactory#createPromise(groovy.lang.Closure[])
     */
    public static Promise createPromise(Closure... c) {
        return getPromiseFactory().createPromise(c);
    }

    /**
     * @see PromiseFactory#createPromise(java.util.Map)
     */
    public static Promise> tasks(Map map) {
        return getPromiseFactory().createPromise(map);
    }
    /**
     * @see PromiseFactory#createPromise(groovy.lang.Closure[])
     */
    public static Promise task(Closure c) {
        return getPromiseFactory().createPromise(c);
    }
    /**
     * @see PromiseFactory#createPromise(groovy.lang.Closure[])
     */
    public static Promise tasks(Closure... c) {
        return getPromiseFactory().createPromise(c);
    }
    /**
     * @see PromiseFactory#createPromise(groovy.lang.Closure[])
     */
    public static Promise> tasks(List> closures) {
        return getPromiseFactory().createPromise(closures);
    }

    /**
     * @see PromiseFactory#createPromise(groovy.lang.Closure, java.util.List)
     */
    public static Promise createPromise(Closure c, List decorators) {
        return getPromiseFactory().createPromise(c, decorators);
    }
    /**
     * @see PromiseFactory#createPromise(java.util.List, java.util.List)
     */
    public static Promise> createPromise(List> closures, List decorators) {
        return getPromiseFactory().createPromise(closures, decorators);
    }
    /**
     * @see PromiseFactory#createPromise(grails.async.Promise[])
     */
    public static  Promise> createPromise(Promise...promises) {
        return getPromiseFactory().createPromise(promises);
    }

    /**
     * @see PromiseFactory#createBoundPromise(java.lang.Object)
     */
    public static Promise createBoundPromise(T value) {
        return getPromiseFactory().createBoundPromise(value);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy