Search in sources :

Example 21 with Supplier

use of java.util.function.Supplier in project presto by prestodb.

the class RedisConnectorFactory method create.

@Override
public Connector create(String connectorId, Map<String, String> config, ConnectorContext context) {
    requireNonNull(connectorId, "connectorId is null");
    requireNonNull(config, "config is null");
    try {
        Bootstrap app = new Bootstrap(new JsonModule(), new RedisConnectorModule(), binder -> {
            binder.bind(RedisConnectorId.class).toInstance(new RedisConnectorId(connectorId));
            binder.bind(TypeManager.class).toInstance(context.getTypeManager());
            binder.bind(NodeManager.class).toInstance(context.getNodeManager());
            if (tableDescriptionSupplier.isPresent()) {
                binder.bind(new TypeLiteral<Supplier<Map<SchemaTableName, RedisTableDescription>>>() {
                }).toInstance(tableDescriptionSupplier.get());
            } else {
                binder.bind(new TypeLiteral<Supplier<Map<SchemaTableName, RedisTableDescription>>>() {
                }).to(RedisTableDescriptionSupplier.class).in(Scopes.SINGLETON);
            }
        });
        Injector injector = app.strictConfig().doNotInitializeLogging().setRequiredConfigurationProperties(config).initialize();
        return injector.getInstance(RedisConnector.class);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : JsonModule(io.airlift.json.JsonModule) SchemaTableName(com.facebook.presto.spi.SchemaTableName) NodeManager(com.facebook.presto.spi.NodeManager) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Bootstrap(io.airlift.bootstrap.Bootstrap) TypeManager(com.facebook.presto.spi.type.TypeManager) Supplier(java.util.function.Supplier) Map(java.util.Map)

Example 22 with Supplier

use of java.util.function.Supplier in project presto by prestodb.

the class KafkaConnectorFactory method create.

@Override
public Connector create(String connectorId, Map<String, String> config, ConnectorContext context) {
    requireNonNull(connectorId, "connectorId is null");
    requireNonNull(config, "config is null");
    try {
        Bootstrap app = new Bootstrap(new JsonModule(), new KafkaConnectorModule(), binder -> {
            binder.bind(KafkaConnectorId.class).toInstance(new KafkaConnectorId(connectorId));
            binder.bind(TypeManager.class).toInstance(context.getTypeManager());
            binder.bind(NodeManager.class).toInstance(context.getNodeManager());
            if (tableDescriptionSupplier.isPresent()) {
                binder.bind(new TypeLiteral<Supplier<Map<SchemaTableName, KafkaTopicDescription>>>() {
                }).toInstance(tableDescriptionSupplier.get());
            } else {
                binder.bind(new TypeLiteral<Supplier<Map<SchemaTableName, KafkaTopicDescription>>>() {
                }).to(KafkaTableDescriptionSupplier.class).in(Scopes.SINGLETON);
            }
        });
        Injector injector = app.strictConfig().doNotInitializeLogging().setRequiredConfigurationProperties(config).initialize();
        return injector.getInstance(KafkaConnector.class);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : JsonModule(io.airlift.json.JsonModule) SchemaTableName(com.facebook.presto.spi.SchemaTableName) NodeManager(com.facebook.presto.spi.NodeManager) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Bootstrap(io.airlift.bootstrap.Bootstrap) TypeManager(com.facebook.presto.spi.type.TypeManager) Supplier(java.util.function.Supplier) Map(java.util.Map)

Example 23 with Supplier

use of java.util.function.Supplier in project ratpack by ratpack.

the class Blocking method get.

/**
   * Performs a blocking operation on a separate thread, returning a promise for its value.
   * <p>
   * This method should be used to perform blocking IO, or to perform any operation that synchronously waits for something to happen.
   * The given factory function will be executed on a thread from a special pool for such operations (i.e. not a thread from the main compute event loop).
   * <p>
   * The operation should do as little computation as possible.
   * It should just perform the blocking operation and immediately return the result.
   * Performing computation during the operation will degrade performance.
   *
   * @param factory the operation that blocks
   * @param <T> the type of value created by the operation
   * @return a promise for the return value of the given blocking operation
   */
public static <T> Promise<T> get(Factory<T> factory) {
    return new DefaultPromise<>(downstream -> {
        DefaultExecution execution = DefaultExecution.require();
        EventLoop eventLoop = execution.getEventLoop();
        execution.delimit(downstream::error, continuation -> eventLoop.execute(() -> CompletableFuture.supplyAsync(new Supplier<Result<T>>() {

            Result<T> result;

            @Override
            public Result<T> get() {
                try {
                    DefaultExecution.THREAD_BINDING.set(execution);
                    intercept(execution, execution.getAllInterceptors().iterator(), () -> {
                        try {
                            result = Result.success(factory.create());
                        } catch (Throwable e) {
                            result = Result.error(e);
                        }
                    });
                    return result;
                } catch (Throwable e) {
                    DefaultExecution.interceptorError(e);
                    return result;
                } finally {
                    DefaultExecution.THREAD_BINDING.remove();
                }
            }
        }, execution.getController().getBlockingExecutor()).thenAcceptAsync(v -> continuation.resume(() -> downstream.accept(v)), eventLoop)));
    });
}
Also used : EventLoop(io.netty.channel.EventLoop) DefaultExecution(ratpack.exec.internal.DefaultExecution) DefaultPromise(ratpack.exec.internal.DefaultPromise) Supplier(java.util.function.Supplier)

Example 24 with Supplier

use of java.util.function.Supplier in project java.webdriver by sayems.

the class MultiSelect method getSelect.

public Select getSelect(Supplier<By> by) {
    final Element element = untilFound(by);
    new WebDriverWait(this, 3, 100).until((Predicate<WebDriver>) driver -> {
        element.click();
        return !element.findElements(By.tagName("option")).isEmpty();
    });
    return new Select(element);
}
Also used : WebDriver(org.openqa.selenium.WebDriver) NoSuchElementException(org.openqa.selenium.NoSuchElementException) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) Arrays(java.util.Arrays) List(java.util.List) Predicate(com.google.common.base.Predicate) By(org.openqa.selenium.By) WebDriver(org.openqa.selenium.WebDriver) Select(org.openqa.selenium.support.ui.Select) WebElement(org.openqa.selenium.WebElement) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) WebElement(org.openqa.selenium.WebElement) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) Select(org.openqa.selenium.support.ui.Select)

Example 25 with Supplier

use of java.util.function.Supplier in project cassandra by apache.

the class PartitionImplementationTest method testIter.

private void testIter(Supplier<Collection<? extends Unfiltered>> contentSupplier, Row staticRow) {
    NavigableSet<Clusterable> sortedContent = new TreeSet<Clusterable>(metadata.comparator);
    sortedContent.addAll(contentSupplier.get());
    AbstractBTreePartition partition;
    try (UnfilteredRowIterator iter = new Util.UnfilteredSource(metadata, Util.dk("pk"), staticRow, sortedContent.stream().map(x -> (Unfiltered) x).iterator())) {
        partition = ImmutableBTreePartition.create(iter);
    }
    ColumnMetadata defCol = metadata.getColumn(new ColumnIdentifier("col", true));
    ColumnFilter cf = ColumnFilter.selectionBuilder().add(defCol).build();
    Function<? super Clusterable, ? extends Clusterable> colFilter = x -> x instanceof Row ? ((Row) x).filter(cf, metadata) : x;
    Slices slices = Slices.with(metadata.comparator, Slice.make(clustering(KEY_RANGE / 4), clustering(KEY_RANGE * 3 / 4)));
    Slices multiSlices = makeSlices();
    // lastRow
    assertRowsEqual((Row) get(sortedContent.descendingSet(), x -> x instanceof Row), partition.lastRow());
    // get(static)
    assertRowsEqual(staticRow, partition.getRow(Clustering.STATIC_CLUSTERING));
    // get
    for (int i = 0; i < KEY_RANGE; ++i) {
        Clustering cl = clustering(i);
        assertRowsEqual(getRow(sortedContent, cl), partition.getRow(cl));
    }
    // isEmpty
    assertEquals(sortedContent.isEmpty() && staticRow == null, partition.isEmpty());
    // hasRows
    assertEquals(sortedContent.stream().anyMatch(x -> x instanceof Row), partition.hasRows());
    // iterator
    assertIteratorsEqual(sortedContent.stream().filter(x -> x instanceof Row).iterator(), partition.iterator());
    // unfiltered iterator
    assertIteratorsEqual(sortedContent.iterator(), partition.unfilteredIterator());
    // unfiltered iterator
    assertIteratorsEqual(sortedContent.iterator(), partition.unfilteredIterator(ColumnFilter.all(metadata), Slices.ALL, false));
    // column-filtered
    assertIteratorsEqual(sortedContent.stream().map(colFilter).iterator(), partition.unfilteredIterator(cf, Slices.ALL, false));
    // sliced
    assertIteratorsEqual(slice(sortedContent, slices.get(0)), partition.unfilteredIterator(ColumnFilter.all(metadata), slices, false));
    assertIteratorsEqual(streamOf(slice(sortedContent, slices.get(0))).map(colFilter).iterator(), partition.unfilteredIterator(cf, slices, false));
    // randomly multi-sliced
    assertIteratorsEqual(slice(sortedContent, multiSlices), partition.unfilteredIterator(ColumnFilter.all(metadata), multiSlices, false));
    assertIteratorsEqual(streamOf(slice(sortedContent, multiSlices)).map(colFilter).iterator(), partition.unfilteredIterator(cf, multiSlices, false));
    // reversed
    assertIteratorsEqual(sortedContent.descendingIterator(), partition.unfilteredIterator(ColumnFilter.all(metadata), Slices.ALL, true));
    assertIteratorsEqual(sortedContent.descendingSet().stream().map(colFilter).iterator(), partition.unfilteredIterator(cf, Slices.ALL, true));
    assertIteratorsEqual(invert(slice(sortedContent, slices.get(0))), partition.unfilteredIterator(ColumnFilter.all(metadata), slices, true));
    assertIteratorsEqual(streamOf(invert(slice(sortedContent, slices.get(0)))).map(colFilter).iterator(), partition.unfilteredIterator(cf, slices, true));
    assertIteratorsEqual(invert(slice(sortedContent, multiSlices)), partition.unfilteredIterator(ColumnFilter.all(metadata), multiSlices, true));
    assertIteratorsEqual(streamOf(invert(slice(sortedContent, multiSlices))).map(colFilter).iterator(), partition.unfilteredIterator(cf, multiSlices, true));
    // search iterator
    testSearchIterator(sortedContent, partition, ColumnFilter.all(metadata), false);
    testSearchIterator(sortedContent, partition, cf, false);
    testSearchIterator(sortedContent, partition, ColumnFilter.all(metadata), true);
    testSearchIterator(sortedContent, partition, cf, true);
    // sliceable iter
    testSlicingOfIterators(sortedContent, partition, ColumnFilter.all(metadata), false);
    testSlicingOfIterators(sortedContent, partition, cf, false);
    testSlicingOfIterators(sortedContent, partition, ColumnFilter.all(metadata), true);
    testSlicingOfIterators(sortedContent, partition, cf, true);
}
Also used : AbstractBTreePartition(org.apache.cassandra.db.partitions.AbstractBTreePartition) java.util(java.util) Iterables(com.google.common.collect.Iterables) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) BeforeClass(org.junit.BeforeClass) SearchIterator(org.apache.cassandra.utils.SearchIterator) org.apache.cassandra.db(org.apache.cassandra.db) Deletion(org.apache.cassandra.db.rows.Row.Deletion) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) org.apache.cassandra.db.rows(org.apache.cassandra.db.rows) Partition(org.apache.cassandra.db.partitions.Partition) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) ImmutableBTreePartition(org.apache.cassandra.db.partitions.ImmutableBTreePartition) StreamSupport(java.util.stream.StreamSupport) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) Predicate(java.util.function.Predicate) Util(org.apache.cassandra.Util) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) KeyspaceParams(org.apache.cassandra.schema.KeyspaceParams) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SchemaLoader(org.apache.cassandra.SchemaLoader) Stream(java.util.stream.Stream) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) TableMetadata(org.apache.cassandra.schema.TableMetadata) Assert(org.junit.Assert) AsciiType(org.apache.cassandra.db.marshal.AsciiType) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) AbstractBTreePartition(org.apache.cassandra.db.partitions.AbstractBTreePartition) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Aggregations

Supplier (java.util.function.Supplier)81 List (java.util.List)29 Test (org.junit.Test)28 Map (java.util.Map)16 Collectors (java.util.stream.Collectors)15 Function (java.util.function.Function)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 Arrays (java.util.Arrays)11 Consumer (java.util.function.Consumer)11 Assert.assertEquals (org.junit.Assert.assertEquals)10 Optional (java.util.Optional)9 Mockito.mock (org.mockito.Mockito.mock)6 Collections (java.util.Collections)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Assert.assertFalse (org.junit.Assert.assertFalse)5 Matchers.any (org.mockito.Matchers.any)5 Mockito.when (org.mockito.Mockito.when)5 Maps (com.google.common.collect.Maps)4