Example usage for com.google.common.collect Iterables getOnlyElement

List of usage examples for com.google.common.collect Iterables getOnlyElement

Introduction

In this page you can find the example usage for com.google.common.collect Iterables getOnlyElement.

Prototype

public static <T> T getOnlyElement(Iterable<T> iterable) 

Source Link

Document

Returns the single element contained in iterable .

Usage

From source file:org.apache.calcite.rex.LogicVisitor.java

/** Finds a suitable logic for evaluating {@code seek} within a list of
 * expressions./*from w ww  .j a v a 2s.  co  m*/
 *
 * <p>Chooses a logic that is safe (that is, gives the right
 * answer) with the fewest possibilities (that is, we prefer one that
 * returns [true as true, false as false, unknown as false] over one that
 * distinguishes false from unknown).
 */
public static Logic find(Logic logic, List<RexNode> nodes, RexNode seek) {
    final Set<Logic> set = EnumSet.noneOf(Logic.class);
    final LogicVisitor visitor = new LogicVisitor(seek, set);
    for (RexNode node : nodes) {
        node.accept(visitor, logic);
    }
    // Convert FALSE (which can only exist within LogicVisitor) to
    // UNKNOWN_AS_TRUE.
    if (set.remove(Logic.FALSE)) {
        set.add(Logic.UNKNOWN_AS_TRUE);
    }
    switch (set.size()) {
    case 0:
        throw new IllegalArgumentException("not found: " + seek);
    case 1:
        return Iterables.getOnlyElement(set);
    default:
        return Logic.TRUE_FALSE_UNKNOWN;
    }
}

From source file:io.druid.query.UnionDataSource.java

@Override
public List<String> getNames() {
    return Lists.transform(dataSources, new Function<TableDataSource, String>() {
        @Override//  w  w w.  j a  va2 s. c o m
        public String apply(TableDataSource input) {
            return Iterables.getOnlyElement(input.getNames());
        }
    });
}

From source file:org.apache.beam.sdk.fn.data.RemoteGrpcPortWrite.java

public static RemoteGrpcPortWrite fromPTransform(PTransform pTransform) throws InvalidProtocolBufferException {
    checkArgument(URN.equals(pTransform.getSpec().getUrn()), "Expected URN for %s, got %s",
            RemoteGrpcPortWrite.class.getSimpleName(), pTransform.getSpec().getUrn());
    checkArgument(pTransform.getInputsCount() == 1, "Expected exactly one output, got %s",
            pTransform.getOutputsCount());
    RemoteGrpcPort port = RemoteGrpcPort.parseFrom(pTransform.getSpec().getPayload());
    String inputPCollectionId = Iterables.getOnlyElement(pTransform.getInputsMap().values());
    return writeToPort(inputPCollectionId, port);
}

From source file:org.jclouds.gogrid.functions.ParseLoadBalancerFromJsonResponse.java

@Override
public LoadBalancer apply(HttpResponse arg0) {
    return Iterables.getOnlyElement(parser.apply(arg0));
}

From source file:org.jclouds.aws.ec2.compute.predicates.AWSEC2InstancePresent.java

@Override
protected void refresh(RegionAndName instance) {
    if (instance.getName().indexOf("sir-") != 0)
        super.refresh(instance);
    else// www  .  j  av  a2s  . c o m
        Iterables.getOnlyElement(client.getSpotInstanceServices()
                .describeSpotInstanceRequestsInRegion(instance.getRegion(), instance.getName()));
}

From source file:com.google.devtools.build.lib.query2.engine.ParallelQueryUtils.java

/**
 * Executes the given {@link QueryTask}s using the given {@link ForkJoinPool} and interruptibly
 * waits for their completion. Throws the first {@link QueryException} encountered during parallel
 * execution or an {@link InterruptedException} if the calling thread is interrupted.
 *
 * <p>These "fail-fast" semantics are desirable to avoid doing unneeded work when evaluating
 * multiple {@link QueryTask}s in parallel: if serial execution of the tasks would result in a
 * {@link QueryException} then we want parallel execution to do so as well, but there's no need to
 * continue waiting for completion of the tasks after at least one of them results in a
 * {@link QueryException}./* w  w w  .ja  v  a2  s .  c  o  m*/
 */
public static void executeQueryTasksAndWaitInterruptiblyFailFast(List<QueryTask> queryTasks,
        ForkJoinPool forkJoinPool) throws QueryException, InterruptedException {
    int numTasks = queryTasks.size();
    if (numTasks == 1) {
        Iterables.getOnlyElement(queryTasks).execute();
        return;
    }
    FailFastCountDownLatch failFastLatch = new FailFastCountDownLatch(numTasks);
    ArrayList<QueryTaskForkJoinTask> forkJoinTasks = new ArrayList<>(numTasks);
    for (QueryTask queryTask : queryTasks) {
        QueryTaskForkJoinTask forkJoinTask = adaptAsForkJoinTask(queryTask, failFastLatch);
        forkJoinTasks.add(forkJoinTask);
        forkJoinPool.submit(forkJoinTask);
    }
    failFastLatch.await();
    try {
        MoreFutures.waitForAllInterruptiblyFailFast(forkJoinTasks);
    } catch (ExecutionException e) {
        throw rethrowCause(e);
    }
}

From source file:org.jclouds.aws.ec2.predicates.VolumeAvailable.java

public boolean apply(Volume volume) {
    logger.trace("looking for status on volume %s", volume.getId());
    volume = Iterables.getOnlyElement(client.describeVolumesInRegion(volume.getRegion(), volume.getId()));
    logger.trace("%s: looking for status %s: currently: %s", volume, Volume.Status.AVAILABLE,
            volume.getStatus());//from w ww .j a va 2s .  c o  m
    return volume.getStatus() == Volume.Status.AVAILABLE;
}

From source file:com.opengamma.provider.security.impl.RemoteSecurityEnhancer.java

@Override
public Security enhanceSecurity(Security security) {
    SecurityEnhancerRequest request = SecurityEnhancerRequest.create(security);
    SecurityEnhancerResult result = enhanceSecurities(request);
    return Iterables.getOnlyElement(result.getResultList());
}

From source file:org.jclouds.sqs.xml.RegexQueueHandler.java

@Override
public URI apply(HttpResponse response) {
    return Iterables.getOnlyElement(parse(returnStringIf200.apply(response)));
}

From source file:com.google.gerrit.server.query.Predicate.java

/** Combine the passed predicates into a single AND node. */
public static <T> Predicate<T> and(final Collection<? extends Predicate<T>> that) {
    if (that.size() == 1) {
        return Iterables.getOnlyElement(that);
    }//from w w  w  . j a  v  a  2s  .  c  o m
    return new AndPredicate<T>(that);
}