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.jclouds.ibmdev.compute.suppliers.IBMDeveloperCloudLocationSupplier.java

@Override
public Set<? extends Location> get() {
    Builder<Location> locations = ImmutableSet.builder();
    Set<? extends org.jclouds.ibmdev.domain.Location> list = sync.listLocations();
    Location provider = Iterables.getOnlyElement(super.get());
    if (list.size() == 0)
        locations.add(provider);/*  w w  w.j  av  a 2  s . c o m*/
    else
        for (org.jclouds.ibmdev.domain.Location from : list) {
            LocationBuilder builder = new LocationBuilder().scope(LocationScope.ZONE).id(from.getId() + "")
                    .description(from.getName()).parent(provider);
            if (isoCodesById.containsKey(from.getId() + ""))
                builder.iso3166Codes(isoCodesById.get(from.getId() + ""));
            locations.add(builder.build());
        }
    return locations.build();
}

From source file:org.apache.druid.indexing.common.task.RestoreTask.java

@Override
public TaskStatus run(TaskToolbox toolbox) throws Exception {
    // Confirm we have a lock (will throw if there isn't exactly one element)
    final TaskLock myLock = Iterables.getOnlyElement(getTaskLocks(toolbox.getTaskActionClient()));

    if (!myLock.getDataSource().equals(getDataSource())) {
        throw new ISE("WTF?! Lock dataSource[%s] != task dataSource[%s]", myLock.getDataSource(),
                getDataSource());/* w w  w . j  a v a  2s .c  om*/
    }

    if (!myLock.getInterval().equals(getInterval())) {
        throw new ISE("WTF?! Lock interval[%s] != task interval[%s]", myLock.getInterval(), getInterval());
    }

    // List unused segments
    final List<DataSegment> unusedSegments = toolbox.getTaskActionClient()
            .submit(new SegmentListUnusedAction(myLock.getDataSource(), myLock.getInterval()));

    // Verify none of these segments have versions > lock version
    for (final DataSegment unusedSegment : unusedSegments) {
        if (unusedSegment.getVersion().compareTo(myLock.getVersion()) > 0) {
            throw new ISE("WTF?! Unused segment[%s] has version[%s] > task version[%s]",
                    unusedSegment.getIdentifier(), unusedSegment.getVersion(), myLock.getVersion());
        }

        log.info("OK to restore segment: %s", unusedSegment.getIdentifier());
    }

    final List<DataSegment> restoredSegments = new ArrayList<>();

    // Move segments
    for (DataSegment segment : unusedSegments) {
        final DataSegment restored = toolbox.getDataSegmentArchiver().restore(segment);
        if (restored != null) {
            restoredSegments.add(restored);
        } else {
            log.info("Segment [%s] did not move, not updating metadata", segment);
        }
    }

    if (restoredSegments.isEmpty()) {
        log.info("No segments restored");
    } else {
        // Update metadata for moved segments
        toolbox.getTaskActionClient()
                .submit(new SegmentMetadataUpdateAction(ImmutableSet.copyOf(restoredSegments)));
    }

    return TaskStatus.success(getId());
}

From source file:org.apache.druid.indexing.common.task.KillTask.java

@Override
public TaskStatus run(TaskToolbox toolbox) throws Exception {
    // Confirm we have a lock (will throw if there isn't exactly one element)
    final TaskLock myLock = Iterables.getOnlyElement(getTaskLocks(toolbox.getTaskActionClient()));

    if (!myLock.getDataSource().equals(getDataSource())) {
        throw new ISE("WTF?! Lock dataSource[%s] != task dataSource[%s]", myLock.getDataSource(),
                getDataSource());//from  w  w w.j a  va  2s  .co m
    }

    if (!myLock.getInterval().equals(getInterval())) {
        throw new ISE("WTF?! Lock interval[%s] != task interval[%s]", myLock.getInterval(), getInterval());
    }

    // List unused segments
    final List<DataSegment> unusedSegments = toolbox.getTaskActionClient()
            .submit(new SegmentListUnusedAction(myLock.getDataSource(), myLock.getInterval()));

    // Verify none of these segments have versions > lock version
    for (final DataSegment unusedSegment : unusedSegments) {
        if (unusedSegment.getVersion().compareTo(myLock.getVersion()) > 0) {
            throw new ISE("WTF?! Unused segment[%s] has version[%s] > task version[%s]",
                    unusedSegment.getIdentifier(), unusedSegment.getVersion(), myLock.getVersion());
        }

        log.info("OK to kill segment: %s", unusedSegment.getIdentifier());
    }

    // Kill segments
    for (DataSegment segment : unusedSegments) {
        toolbox.getDataSegmentKiller().kill(segment);
        toolbox.getTaskActionClient().submit(new SegmentNukeAction(ImmutableSet.of(segment)));
    }

    return TaskStatus.success(getId());
}

From source file:io.prestosql.sql.planner.plan.LimitNode.java

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren) {
    return new LimitNode(getId(), Iterables.getOnlyElement(newChildren), count, isPartial());
}

From source file:io.prestosql.sql.planner.plan.ExplainAnalyzeNode.java

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren) {
    return new ExplainAnalyzeNode(getId(), Iterables.getOnlyElement(newChildren), outputSymbol, isVerbose());
}

From source file:org.jclouds.location.suppliers.all.RegionToProviderOrJustProvider.java

@Override
public Set<? extends Location> get() {
    Builder<Location> locations = ImmutableSet.builder();
    Location provider = Iterables.getOnlyElement(justProvider.get());
    Set<String> regions = regionsSupplier.get();
    Map<String, Supplier<Set<String>>> isoCodesById = isoCodesByIdSupplier.get();
    if (regions.size() == 0)
        return locations.add(provider).build();
    else/*ww  w .ja  va  2  s  .  co m*/
        for (String region : regions) {
            LocationBuilder builder = new LocationBuilder().scope(LocationScope.REGION).id(region)
                    .description(region).parent(provider);
            if (isoCodesById.containsKey(region))
                builder.iso3166Codes(isoCodesById.get(region).get());
            locations.add(builder.build());
        }
    return locations.build();
}

From source file:org.apache.beam.runners.direct.ViewOverrideFactory.java

@Override
public PCollection<ElemT> getInput(List<TaggedPValue> inputs, Pipeline p) {
    return (PCollection<ElemT>) Iterables.getOnlyElement(inputs).getValue();
}

From source file:com.urswolfer.gerrit.client.rest.http.projects.BranchApiRestClient.java

@Override
public BranchInfo get() throws RestApiException {
    JsonElement jsonElement = gerritRestClient.getRequest(branchUrl());
    return Iterables.getOnlyElement(branchInfoParser.parseBranchInfos(jsonElement));
}

From source file:org.trancecode.xproc.step.CompareStepProcessor.java

private static boolean compare(final Processor processor, final XdmNode sourceNode,
        final XdmNode alternateNode) {
    try {// ww  w. ja v  a  2s .  com
        final XPathCompiler xpathCompiler = processor.newXPathCompiler();
        final QName node1 = new QName("node1");
        final QName node2 = new QName("node2");
        xpathCompiler.declareVariable(node1);
        xpathCompiler.declareVariable(node2);

        final XPathSelector selector = xpathCompiler.compile("deep-equal($node1, $node2)").load();
        selector.setVariable(node1, sourceNode);
        selector.setVariable(node2, alternateNode);

        final XdmValue result = selector.evaluate();
        final XdmItem resultNode = Iterables.getOnlyElement(result);
        assert resultNode.isAtomicValue() : resultNode;
        return ((XdmAtomicValue) resultNode).getBooleanValue();
    } catch (final SaxonApiException e) {
        throw new IllegalStateException(e);
    }
}

From source file:io.druid.sql.calcite.expression.builtin.MillisToTimestampOperatorConversion.java

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature,
        final RexNode rexNode) {
    // Nothing to do, just leave the operand unchanged. Druid treats millis and timestamps the same internally.
    final RexCall call = (RexCall) rexNode;
    return Expressions.toDruidExpression(plannerContext, rowSignature,
            Iterables.getOnlyElement(call.getOperands()));
}