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.rackspace.clouddns.v1.functions.ParseOnlyRecord.java

@Override
public RecordDetail apply(HttpResponse response) {
    Map<String, List<RawRecord>> records = json.apply(response);

    if (records == null)
        return null;

    RawRecord rawRecord = Iterables.getOnlyElement(records.get("records"));

    return toRecordDetails.apply(rawRecord);
}

From source file:com.opengamma.financial.analytics.model.forex.option.callspreadblack.FXDigitalCallSpreadBlackVegaFunction.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 PresentValueBlackVolatilitySensitivityCallSpreadBlackForexCalculator calculator = new PresentValueBlackVolatilitySensitivityCallSpreadBlackForexCalculator(
            spread);//from ww w.j  a va 2  s . co m
    final PresentValueForexBlackVolatilitySensitivity result = fxDigital.accept(calculator, data);
    final CurrencyAmount vegaValue = result.toSingleValue();
    return Collections.singleton(new ComputedValue(spec, vegaValue.getAmount()));
}

From source file:com.opengamma.financial.analytics.timeseries.VolatilityWeightedFXReturnSeriesFunction.java

@Override
protected String getSpotSeriesStart(final ValueProperties constraints) {
    final Set<String> lambdas = constraints
            .getValues(VolatilityWeightingFunctionUtils.VOLATILITY_WEIGHTING_LAMBDA_PROPERTY);
    if (lambdas == null || lambdas.size() != 1) {
        return null;
    }// ww  w. j  a  va  2 s  .  c  om
    final Set<String> startDates = constraints.getValues(HistoricalTimeSeriesFunctionUtils.START_DATE_PROPERTY);
    if (startDates == null || startDates.size() != 1) {
        return null;
    }
    final String startDate = Iterables.getOnlyElement(startDates);
    final Set<String> volWeightingStartDates = constraints
            .getValues(VolatilityWeightingFunctionUtils.VOLATILITY_WEIGHTING_START_DATE_PROPERTY);
    if (volWeightingStartDates == null || volWeightingStartDates.size() != 1) {
        // NOTE jonathan 2013-04-29 -- should start a day earlier so the result after weighting starts at the startDate,
        // but need to know previous date with data
        return startDate;
    }
    return Iterables.getOnlyElement(volWeightingStartDates);
}

From source file:jp.xet.uncommons.wicket.model.OnlyElementModel.java

@Override
protected T load() {
    Iterable<T> iterable = collectionModel.getObject();
    try {//w w  w  . j a  va 2 s .  c o  m
        return Iterables.getOnlyElement(iterable);
    } catch (NoSuchElementException e) {
        return null;
    } catch (IllegalArgumentException e) {
        return null;
    }
}

From source file:com.opengamma.financial.analytics.model.irfutureoption.IRFutureOptionSABRPresentValueFunction.java

@Override
protected Set<ComputedValue> getResult(final FunctionExecutionContext context,
        final Set<ValueRequirement> desiredValues, final FunctionInputs inputs, final ComputationTarget target,
        final InstrumentDerivative irFutureOption, final SABRInterestRateDataBundle data) {
    final double pv = irFutureOption.accept(CALCULATOR, data);
    final ValueRequirement desiredValue = Iterables.getOnlyElement(desiredValues);
    final ValueProperties properties = desiredValue.getConstraints().copy()
            .with(ValuePropertyNames.FUNCTION, getUniqueId()).get();
    final ValueSpecification spec = new ValueSpecification(getValueRequirementNames()[0],
            target.toSpecification(), properties);
    return Collections.singleton(new ComputedValue(spec, pv));
}

From source file:org.trancecode.xproc.PipelineResult.java

public XdmNode readNode(final String portName) {
    return Iterables.getOnlyElement(readNodes(portName));
}

From source file:dagger2.internal.codegen.writer.AnnotationWriter.java

@Override
public Appendable write(Appendable appendable, Context context) throws IOException {
    appendable.append('@');
    annotationName.write(appendable, context);
    if (!memberMap.isEmpty()) {
        appendable.append('(');
        if (memberMap.size() == 1) {
            Entry<String, Writable> onlyEntry = Iterables.getOnlyElement(memberMap.entrySet());
            if (!onlyEntry.getKey().equals("value")) {
                appendable.append(onlyEntry.getKey()).append(" = ");
            }/*w ww . ja v  a 2s .c  om*/
            onlyEntry.getValue().write(appendable, context);
        }
        appendable.append(')');
    }
    return appendable;
}

From source file:org.apache.druid.sql.calcite.expression.UnaryFunctionOperatorConversion.java

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature,
        final RexNode rexNode) {
    return OperatorConversions.convertCall(plannerContext, rowSignature, rexNode,
            operands -> DruidExpression.fromExpression(StringUtils.format("%s(%s)", druidOperator,
                    Iterables.getOnlyElement(operands).getExpression())));
}

From source file:com.android.providers.contacts.aggregation.util.ContactAggregatorHelper.java

/**
 * If two connected components have disjoint accounts, merge them.
 * If there is any uncertainty, keep them separate.
 *///w  w w. java2  s  .  c o  m
@VisibleForTesting
public static void mergeComponentsWithDisjointAccounts(Set<Set<Long>> connectedRawContactSets,
        Map<Long, Long> rawContactsToAccounts) {
    // Index to rawContactIds mapping
    final Map<Integer, Set<Long>> rawContactIds = new HashMap<>();
    // AccountId to indices mapping
    final Map<Long, Set<Integer>> accounts = new HashMap<>();

    int index = 0;
    for (Set<Long> rIds : connectedRawContactSets) {
        rawContactIds.put(index, rIds);
        for (Long rId : rIds) {
            long acctId = rawContactsToAccounts.get(rId);
            Set<Integer> s = accounts.get(acctId);
            if (s == null) {
                s = new HashSet<Integer>();
            }
            s.add(index);
            accounts.put(acctId, s);
        }
        index++;
    }

    connectedRawContactSets.clear();
    for (Long accountId : accounts.keySet()) {
        final Set<Integer> s = accounts.get(accountId);
        if (s.size() > 1) {
            for (Integer i : s) {
                final Set<Long> rIdSet = rawContactIds.get(i);
                if (rIdSet != null && !rIdSet.isEmpty()) {
                    connectedRawContactSets.add(rIdSet);
                    rawContactIds.remove(i);
                }
            }
        }
    }

    final Set<Long> mergedSet = new HashSet<>();
    for (Long accountId : accounts.keySet()) {
        final Set<Integer> s = accounts.get(accountId);
        if (s.size() == 1) {
            Set<Long> ids = rawContactIds.get(Iterables.getOnlyElement(s));
            if (ids != null && !ids.isEmpty()) {
                mergedSet.addAll(ids);
            }
        }
    }
    connectedRawContactSets.add(mergedSet);
}

From source file:org.apache.beam.runners.samza.translation.ConfigContext.java

@SuppressWarnings("unchecked")
public <OutT extends PValue> OutT getOutput(PTransform<?, OutT> transform) {
    return (OutT) Iterables.getOnlyElement(this.currentTransform.getOutputs().values());
}