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.ec2.predicates.PlacementGroupDeleted.java

private PlacementGroup refresh(PlacementGroup group) {
    return Iterables.getOnlyElement(client.getPlacementGroupServices()
            .describePlacementGroupsInRegion(group.getRegion(), group.getName()));
}

From source file:org.jclouds.ec2.predicates.InstanceStateStopped.java

private RunningInstance refresh(RunningInstance instance) {
    return Iterables.getOnlyElement(
            Iterables.getOnlyElement(client.describeInstancesInRegion(instance.getRegion(), instance.getId())));
}

From source file:com.opengamma.financial.analytics.model.bondfutureoption.BondFutureOptionBlackPV01Function.java

@Override
protected Set<ComputedValue> getResult(final InstrumentDerivative bondFutureOption,
        final YieldCurveWithBlackCubeBundle data, final MultiCurveCalculationConfig curveCalculationConfig,
        final ValueSpecification spec, final FunctionInputs inputs, final Set<ValueRequirement> desiredValues,
        final BondFutureOptionSecurity security) {
    final String curveName = Iterables.getOnlyElement(desiredValues).getConstraint(ValuePropertyNames.CURVE)
            + "_" + FinancialSecurityUtils.getCurrency(security);
    final Map<String, Double> pv01 = CALCULATOR.visit(bondFutureOption, data);
    if (!pv01.containsKey(curveName)) {
        throw new OpenGammaRuntimeException("Could not get PV01 for " + curveName);
    }//from   w  w w. jav a  2s  .  c om
    return Collections.singleton(new ComputedValue(spec, pv01.get(curveName)));
}

From source file:com.opengamma.financial.analytics.model.pnl.VolatilityWeightedHistoricalValuationPnLFunction.java

@Override
protected String getPriceSeriesStart(ValueProperties outputConstraints) {
    if (super.getPriceSeriesStart(outputConstraints) == null) {
        return null;
    }/*from  w  w  w .  j  a  v  a2  s.  c o  m*/
    Set<String> volatilityWeightingStartDates = outputConstraints
            .getValues(VolatilityWeightingFunctionUtils.VOLATILITY_WEIGHTING_START_DATE_PROPERTY);
    if (volatilityWeightingStartDates == null || volatilityWeightingStartDates.size() != 1) {
        return null;
    }
    return Iterables.getOnlyElement(volatilityWeightingStartDates);
}

From source file:com.eucalyptus.compute.vpc.persist.PersistenceVpcs.java

@Override
public <T> T lookupDefault(final OwnerFullName ownerFullName, final Function<? super Vpc, T> transform)
        throws VpcMetadataException {
    try {/*from   ww w.j  a v a2  s  . co  m*/
        return Iterables.getOnlyElement(
                listByExample(Vpc.exampleDefault(ownerFullName), Predicates.alwaysTrue(), transform));
    } catch (NoSuchElementException e) {
        throw new VpcMetadataNotFoundException(qualifyOwner("Default VPC not found", ownerFullName));
    }
}

From source file:org.apache.calcite.adapter.druid.UnarySuffixOperatorConversion.java

@Override
public String toDruidExpression(RexNode rexNode, RelDataType rowType, DruidQuery druidQuery) {
    final RexCall call = (RexCall) rexNode;

    final List<String> druidExpressions = DruidExpressions.toDruidExpressions(druidQuery, rowType,
            call.getOperands());//w w  w  . jav  a 2 s  . co m

    if (druidExpressions == null) {
        return null;
    }

    return DruidQuery.format("(%s %s)", Iterables.getOnlyElement(druidExpressions), druidOperator);
}

From source file:com.opengamma.financial.analytics.model.credit.isda.cdx.ISDACDXAsSingleNameJumpToDefaultFunction.java

@Override
protected Set<ComputedValue> getComputedValue(final CreditDefaultSwapDefinition definition,
        final ISDADateCurve yieldCurve, final ZonedDateTime[] times, final double[] marketSpreads,
        final ZonedDateTime valuationDate, final ComputationTarget target, final ValueProperties properties,
        final FunctionInputs inputs) {
    final PriceType priceType = PriceType.valueOf(Iterables.getOnlyElement(
            properties.getValues(CreditInstrumentPropertyNamesAndValues.PROPERTY_CDS_PRICE_TYPE)));
    final double jumpToDefault = CALCULATOR.getValueOnDefaultCreditDefaultSwap(valuationDate, definition,
            yieldCurve, times, marketSpreads, priceType);
    final ValueSpecification spec = new ValueSpecification(ValueRequirementNames.JUMP_TO_DEFAULT,
            target.toSpecification(), properties);
    return Collections.singleton(new ComputedValue(spec, jumpToDefault));
}

From source file:com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.java

/**
 * Checks that exactly one header named {@code headerName} is present and
 * returns its value.//from ww  w . j  a  v a2 s  .c  o  m
 */
static String getSingleHeader(HTTPResponse resp, String headerName) {
    return Iterables.getOnlyElement(getHeaders(resp, headerName));
}

From source file:com.opengamma.financial.convention.DefaultConventionSource.java

@Override
public Convention getConvention(final ExternalIdBundle identifiers) {
    final ConventionSearchResult result = _conventionMaster
            .searchConvention(new ConventionSearchRequest(identifiers));
    final int size = result.getResults().size();
    switch (size) {
    case 0:/*www .  j a v  a 2  s  .  co m*/
        return null;
    case 1:
        return Iterables.getOnlyElement(result.getResults()).getConvention();
    default:
        throw new OpenGammaRuntimeException(
                "Multiple matches (" + size + ") to " + identifiers + "; expecting one");
    }
}

From source file:io.druid.indexing.common.task.ArchiveTask.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));

    if (!myLock.getDataSource().equals(getDataSource())) {
        throw new ISE("WTF?! Lock dataSource[%s] != task dataSource[%s]", myLock.getDataSource(),
                getDataSource());/*  ww  w  .j  a  v a 2 s. 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 archive segment: %s", unusedSegment.getIdentifier());
    }

    // Move segments
    for (DataSegment segment : unusedSegments) {
        final DataSegment archivedSegment = toolbox.getDataSegmentArchiver().archive(segment);
        toolbox.getTaskActionClient().submit(new SegmentMetadataUpdateAction(ImmutableSet.of(archivedSegment)));
    }

    return TaskStatus.success(getId());
}