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

grails.async.factory.AbstractPromiseFactory.groovy Maven / Gradle / Ivy

/*
 * 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.factory

import grails.async.Promise
import grails.async.PromiseFactory
import grails.async.PromiseList
import grails.async.PromiseMap
import grails.async.decorator.PromiseDecorator
import grails.async.decorator.PromiseDecoratorLookupStrategy
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import org.grails.async.factory.BoundPromise

import java.util.concurrent.ConcurrentLinkedQueue

/**
 * Abstract implementation of the {@link grails.async.PromiseFactory} interface, subclasses should extend
 * this class to obtain common generic functionality
 *
 * @author Graeme Rocher
 * @since 2.3
 */
@CompileStatic
abstract class AbstractPromiseFactory implements PromiseFactory {

    protected Collection lookupStrategies = new ConcurrentLinkedQueue()

    void addPromiseDecoratorLookupStrategy(PromiseDecoratorLookupStrategy lookupStrategy) {
        lookupStrategies.add(lookupStrategy)
    }

    def  Promise createBoundPromise(T value) {
        return new BoundPromise(value)
    }

    /**
     * @see PromiseFactory#createPromise(groovy.lang.Closure, java.util.List)
     */
    def  Promise createPromise(Closure c, List decorators) {
        c = applyDecorators(c, decorators)

        return createPromiseInternal(c)
    }

    def  Closure applyDecorators(Closure c, List decorators) {
        List allDecorators = decorators != null ? new ArrayList(decorators): new ArrayList()
        for (PromiseDecoratorLookupStrategy lookupStrategy : lookupStrategies) {
            allDecorators.addAll(lookupStrategy.findDecorators())
        }
        if (!allDecorators.isEmpty()) {
            for(PromiseDecorator d : allDecorators) {
                c = d.decorate(c)
            }
        }
        return c
    }

    /**
     * @see PromiseFactory#createPromise(java.util.List)
     */
    def  Promise> createPromise(List> closures) {
        return createPromise(closures,null)
    }

    /**
     * @see PromiseFactory#createPromise(java.util.List, java.util.List)
     */
    def  Promise> createPromise(List> closures, List decorators) {

        List> newClosures = new ArrayList>(closures.size())
        for (Closure closure : closures) {
            newClosures.add(applyDecorators(closure, decorators))
        }
        closures = newClosures
        PromiseList promiseList = new PromiseList()

        for (Closure closure : closures) {
            promiseList.add(closure)
        }
        return promiseList
    }

    /**
     * @see PromiseFactory#createPromise(grails.async.Promise[])
     */
    def  Promise> createPromise(Promise... promises) {
        PromiseList promiseList = new PromiseList()
        for(Promise p : promises) {
            promiseList.add(p)
        }
        return promiseList
    }

    @Override
    def  Promise> createPromise(Map map, List decorators) {
        PromiseMap promiseMap = new PromiseMap()
        for (Map.Entry entry : map.entrySet()) {
            K key = entry.getKey()
            Object value = entry.getValue()
            if (value instanceof Promise) {
                promiseMap.put(key, (Promise)value)
            }
            else if (value instanceof Closure) {
                Closure c = (Closure) value
                applyDecorators(c, decorators)
                promiseMap.put(key, createPromiseInternal(c))
            }
            else {
                promiseMap.put(key, new BoundPromise((V)value))
            }
        }

        return promiseMap
    }
    /**
     * @see PromiseFactory#createPromise(java.util.Map)
     */
    def  Promise> createPromise(Map map) {
        createPromise(map, Collections.emptyList())
    }

    @CompileDynamic
    protected Promise createPromiseInternal(Closure c) {
        return createPromise(c)
    }

    /**
     * @see PromiseFactory#waitAll(grails.async.Promise[])
     */
    @CompileDynamic
     List waitAll(Promise... promises) {
        return waitAll(Arrays.asList(promises))
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy