Search in sources :

Example 1 with PagingProvider

use of org.mule.runtime.extension.api.runtime.streaming.PagingProvider in project mule by mulesoft.

the class DrStrangeOperations method sayMagicWords.

public PagingProvider<MysticConnection, String> sayMagicWords(@Content List<String> values, int fetchSize) {
    final AtomicInteger index = new AtomicInteger(0);
    return new PagingProvider<MysticConnection, String>() {

        private int timesClosed = 0;

        @Override
        public List<String> getPage(MysticConnection connection) {
            final int i = index.get();
            if (i >= values.size()) {
                return emptyList();
            }
            List<String> words = values.subList(i, i + fetchSize);
            index.addAndGet(fetchSize);
            return words;
        }

        @Override
        public java.util.Optional<Integer> getTotalResults(MysticConnection connection) {
            return of(values.size());
        }

        @Override
        public void close(MysticConnection connection) throws MuleException {
            timesClosed++;
            if (timesClosed > 1) {
                throw new RuntimeException("Expected to be closed only once but was called twice");
            }
        }
    };
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PagingProvider(org.mule.runtime.extension.api.runtime.streaming.PagingProvider)

Example 2 with PagingProvider

use of org.mule.runtime.extension.api.runtime.streaming.PagingProvider in project mule by mulesoft.

the class IntrospectionUtils method getPagingProviderTypes.

/**
 * Introspects a {@link PagingProvider} type and returns their generics.
 *
 * @param pagingProvider {@link PagingProvider} to introspect
 * @return The {@link PagingProvider} generics.
 */
public static Pair<ResolvableType, ResolvableType> getPagingProviderTypes(ResolvableType pagingProvider) {
    if (!PagingProvider.class.isAssignableFrom(pagingProvider.getRawClass())) {
        throw new IllegalArgumentException("The given OutputType is not a PagingProvider");
    }
    ResolvableType[] generics = pagingProvider.getGenerics();
    ResolvableType connectionType = null;
    ResolvableType returnType = null;
    if (generics.length == 0) {
        for (ResolvableType resolvableType : pagingProvider.getInterfaces()) {
            if (resolvableType.getRawClass().equals(PagingProvider.class)) {
                connectionType = resolvableType.getGeneric(0);
                returnType = resolvableType.getGeneric(1);
            }
        }
    } else {
        connectionType = generics[0];
        returnType = generics[1];
    }
    return new Pair<>(connectionType, returnType);
}
Also used : PagingProvider(org.mule.runtime.extension.api.runtime.streaming.PagingProvider) ResolvableType(org.springframework.core.ResolvableType) Pair(org.mule.runtime.api.util.Pair)

Aggregations

PagingProvider (org.mule.runtime.extension.api.runtime.streaming.PagingProvider)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Pair (org.mule.runtime.api.util.Pair)1 ResolvableType (org.springframework.core.ResolvableType)1