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:com.opengamma.financial.analytics.model.forex.option.callspreadblack.FXDigitalCallSpreadBlackCurveSensitivityFunction.java

@Override
protected Set<ComputedValue> getResult(final InstrumentDerivative fxDigital,
        final ForexOptionDataBundle<?> data, final ComputationTarget target,
        final Set<ValueRequirement> desiredValues, final FunctionInputs inputs, final ValueSpecification spec,
        final FunctionExecutionContext executionContext) {
    final String spreadName = Iterables.getOnlyElement(desiredValues)
            .getConstraint(CalculationPropertyNamesAndValues.PROPERTY_CALL_SPREAD_VALUE);
    final double spread = Double.parseDouble(spreadName);
    final PresentValueCurveSensitivityCallSpreadBlackForexCalculator calculator = new PresentValueCurveSensitivityCallSpreadBlackForexCalculator(
            spread);//w w w.ja va 2s . co m
    final MultipleCurrencyInterestRateCurveSensitivity result = fxDigital.accept(calculator, data);
    return Collections.singleton(new ComputedValue(spec, result));
}

From source file:com.gravitant.cloud.adapters.provision.providers.VolumeDetached.java

public boolean apply(Attachment attachment) {
    logger.trace("looking for volume %s", attachment.getVolumeId());
    Volume volume = Iterables
            .getOnlyElement(client.describeVolumesInRegion(attachment.getRegion(), attachment.getVolumeId()));

    /*If attachment size is 0 volume is detached for sure.*/
    if (volume.getAttachments().size() == 0) {
        return true;
    }//from w  w  w . jav  a 2  s .  c om

    /* But if attachment size is > 0, then the attachment could be in any state. 
     * So we need to check if the status is DETACHED (return true) or not (return false). 
     */
    Attachment lastAttachment = Sets.newTreeSet(volume.getAttachments()).last();
    logger.trace("%s: looking for status %s: currently: %s", lastAttachment, Attachment.Status.DETACHED,
            lastAttachment.getStatus());
    return lastAttachment.getStatus() == Attachment.Status.DETACHED;
}

From source file:com.opengamma.financial.analytics.model.forex.forward.FXForwardMultiValuedFunction.java

@Override
public Set<ValueSpecification> getResults(final FunctionCompilationContext context,
        final ComputationTarget target, final Map<ValueSpecification, ValueRequirement> inputs) {
    String payCurveName = null;//  w  ww . ja va2s . co m
    String payCurveCalculationConfig = null;
    String receiveCurveName = null;
    String receiveCurveCalculationConfig = null;
    for (final Map.Entry<ValueSpecification, ValueRequirement> entry : inputs.entrySet()) {
        final ValueRequirement requirement = entry.getValue();
        if (requirement.getValueName().equals(ValueRequirementNames.YIELD_CURVE)) {
            final ValueProperties constraints = requirement.getConstraints();
            if (constraints.getProperties().contains(ValuePropertyNames.PAY_CURVE)) {
                payCurveName = Iterables.getOnlyElement(constraints.getValues(ValuePropertyNames.CURVE));
                payCurveCalculationConfig = Iterables
                        .getOnlyElement(constraints.getValues(ValuePropertyNames.CURVE_CALCULATION_CONFIG));
            } else if (constraints.getProperties().contains(ValuePropertyNames.RECEIVE_CURVE)) {
                receiveCurveName = Iterables.getOnlyElement(constraints.getValues(ValuePropertyNames.CURVE));
                receiveCurveCalculationConfig = Iterables
                        .getOnlyElement(constraints.getValues(ValuePropertyNames.CURVE_CALCULATION_CONFIG));
            }
        }
    }
    assert payCurveName != null;
    assert receiveCurveName != null;
    final ValueProperties properties = getResultProperties(target, payCurveName, receiveCurveName,
            payCurveCalculationConfig, receiveCurveCalculationConfig).get();
    return Collections
            .singleton(new ValueSpecification(getValueRequirementName(), target.toSpecification(), properties));
}

From source file:xml.entity.immutableelement.InternalElement.java

@Override
public ImmutableElement child(final String name) {
    final FluentIterable<ImmutableElement> collection = FluentIterable.from(children)
            .filter(ImmutableElements.byName(name));

    return Iterables.getOnlyElement(collection);
}

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

@Override
public Credentials apply(HttpResponse input) {
    Map<String, Credentials> returnVal = parser.apply(input);
    checkState(!(returnVal.size() > 1),
            "expecting only 1 credential in response, but had more: " + returnVal.keySet());
    return (returnVal.size() > 0) ? Iterables.getOnlyElement(returnVal.values()) : null;
}

From source file:org.eclipse.xtext.scoping.impl.MapBasedScope.java

/**
 * @since 2.3/* ww  w .  j  a  v  a2 s. c  o  m*/
 */
public static IScope createScope(IScope parent, Collection<IEObjectDescription> descriptions) {
    if (descriptions.size() == 1) {
        IEObjectDescription description = Iterables.getOnlyElement(descriptions);
        return new MapBasedScope(parent, Collections.singletonMap(description.getName(), description), false);
    } else if (descriptions.isEmpty()) {
        return parent;
    }
    return createScope(parent, descriptions, false);
}

From source file:org.apache.calcite.materialize.SqlLatticeStatisticProvider.java

@Override
public int cardinality(Lattice lattice, Lattice.Column column) {
    final String sql = lattice.countSql(ImmutableBitSet.of(column.ordinal));
    final Table table = new MaterializationService.DefaultTableFactory().createTable(lattice.rootSchema, sql,
            ImmutableList.<String>of());
    final Object[] values = Iterables.getOnlyElement(((ScannableTable) table).scan(null));
    return ((Number) values[0]).intValue();
}

From source file:com.opengamma.master.historicaltimeseries.impl.DefaultHistoricalTimeSeriesSelector.java

@Override
public ManageableHistoricalTimeSeriesInfo select(Collection<ManageableHistoricalTimeSeriesInfo> candidates,
        String selectionKey) {/*from w  w w .  j  a va  2  s .  c om*/
    selectionKey = Objects.firstNonNull(selectionKey, HistoricalTimeSeriesRatingFieldNames.DEFAULT_CONFIG_NAME);

    //IGN-139 - avoid rating unless we have to
    switch (candidates.size()) {
    case 0:
        return null;
    case 1:
        return Iterables.getOnlyElement(candidates);
    default:
        // Pick best using rules from configuration
        HistoricalTimeSeriesRating rating = _configSource.getLatestByName(HistoricalTimeSeriesRating.class,
                selectionKey);
        if (rating == null) {
            s_logger.warn("Resolver failed to find configuration: {}", selectionKey);
            return null;
        }
        return bestMatch(candidates, rating);
    }
}

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

@Override
public Entry<String, MessageIdAndMD5> getResult() {
    try {//  w ww.  j  a  v a  2 s .  c o m
        return Iterables.getOnlyElement(ImmutableMap.of(id, builder.build()).entrySet());
    } finally {
        builder = MessageIdAndMD5.builder();
    }
}

From source file:com.axemblr.provisionr.amazon.core.ImageTableQuery.java

public String singleResult() {
    final Map<String, String> filters = filtersBuilder.build();
    final ImmutableSet.Builder<String> builder = ImmutableSet.builder();

    for (String row : table.getTable().rowKeySet()) {
        if (matchesFilters(table.getTable().row(row), filters)) {
            builder.add(table.getTable().get(row, "ami-id"));
        }//from w ww . ja v a  2  s.co m
    }
    return Iterables.getOnlyElement(builder.build());
}