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.google.devtools.build.lib.analysis.actions.ExecutableSymlinkAction.java

@Override
public void execute(ActionExecutionContext actionExecutionContext) throws ActionExecutionException {
    Path inputPath = actionExecutionContext.getExecutor().getExecRoot().getRelative(getInputPath());
    try {/*from   w  ww .j  av  a2  s  . c  o m*/
        // Validate that input path is a file with the executable bit is set.
        if (!inputPath.isFile()) {
            throw new ActionExecutionException(
                    "'" + Iterables.getOnlyElement(getInputs()).prettyPrint() + "' is not a file", this, false);
        }
        if (!inputPath.isExecutable()) {
            throw new ActionExecutionException("failed to create symbolic link '"
                    + Iterables.getOnlyElement(getOutputs()).prettyPrint() + "': file '"
                    + Iterables.getOnlyElement(getInputs()).prettyPrint() + "' is not executable", this, false);
        }
    } catch (IOException e) {
        throw new ActionExecutionException("failed to create symbolic link '"
                + Iterables.getOnlyElement(getOutputs()).prettyPrint() + "' to the '"
                + Iterables.getOnlyElement(getInputs()).prettyPrint() + "' due to I/O error: " + e.getMessage(),
                e, this, false);
    }

    super.execute(actionExecutionContext);
}

From source file:com.opengamma.financial.analytics.model.forex.option.callspreadblack.FXDigitalCallSpreadBlackPresentValueFunction.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 PresentValueCallSpreadBlackForexCalculator calculator = new PresentValueCallSpreadBlackForexCalculator(
            spread);//from w w w  .  jav a 2 s  .  co  m
    final MultipleCurrencyAmount result = fxDigital.accept(calculator, data);
    final CurrencyAmount ca = result.getCurrencyAmounts()[0];
    final String expectedCurrency = spec.getProperty(ValuePropertyNames.CURRENCY);
    if (!expectedCurrency.equals(ca.getCurrency().getCode())) {
        throw new OpenGammaRuntimeException("Expected currency " + expectedCurrency
                + " does not equal result currency " + ca.getCurrency());
    }
    final double amount = ca.getAmount();
    return Collections.singleton(new ComputedValue(spec, amount));
}

From source file:org.jclouds.aws.ec2.binders.BindUserGroupsToIndexedFormParams.java

private void checkValidUserGroup(Object input) {
    Iterable<?> values = (Iterable<?>) input;
    long size = Iterables.size(values);
    checkArgument(size == 0 || (size == 1 && Iterables.getOnlyElement(values).equals("all")),
            "only supported UserGroup is 'all'");
}

From source file:com.opengamma.financial.analytics.model.forex.option.callspreadblack.FXDigitalCallSpreadBlackVegaPointFunction.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 .ja  va 2s  .c om*/
    final PresentValueForexBlackVolatilitySensitivity result = fxDigital.accept(calculator, data);
    return Collections.singleton(new ComputedValue(spec, result));
}

From source file:org.atlasapi.remotesite.synd.PodcastGraphExtractor.java

protected Encoding encodingFrom(List<SyndEnclosure> enclosures) {
    Encoding encoding = new Encoding();

    SyndEnclosure enclosure = Iterables.getOnlyElement(enclosures);
    encoding.setDataSize(enclosure.getLength() / 1024);

    MimeType audioCoding = AudioFormat.fromAltName(enclosure.getType());
    if (audioCoding == MimeType.AUDIO_MPEG) {
        encoding.setAudioCoding(audioCoding);
    }// w w w .j a  v a 2  s. co  m
    MimeType videoCoding = VideoFormat.fromAltName(enclosure.getType());
    if (videoCoding == MimeType.VIDEO_MPEG) {
        encoding.setVideoCoding(videoCoding);
    }
    MimeType containerFormat = ContainerFormat.fromAltName(enclosure.getType());
    if (containerFormat != null) {
        encoding.setDataContainerFormat(containerFormat);
    } else {
        log.warn("unknownDataContainerFormat " + enclosure.getUrl() + " : " + enclosure.getType());
    }
    return encoding;
}

From source file:io.druid.sql.calcite.expression.UnarySuffixOperatorConversion.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)",
                    Iterables.getOnlyElement(operands).getExpression(), druidOperator)));
}

From source file:io.prestosql.plugin.localfile.LocalFileRecordSet.java

public LocalFileRecordSet(LocalFileTables localFileTables, LocalFileSplit split,
        List<LocalFileColumnHandle> columns) {
    this.columns = requireNonNull(columns, "column handles is null");
    requireNonNull(split, "split is null");

    ImmutableList.Builder<Type> types = ImmutableList.builder();
    for (LocalFileColumnHandle column : columns) {
        types.add(column.getColumnType());
    }/*from   w  w w  .  j a  va  2s.  co  m*/
    this.columnTypes = types.build();
    this.address = Iterables.getOnlyElement(split.getAddresses());
    this.effectivePredicate = split.getEffectivePredicate();
    this.tableName = split.getTableName();

    this.localFileTables = requireNonNull(localFileTables, "localFileTables is null");
}

From source file:org.jclouds.http.functions.UnwrapOnlyNestedJsonValue.java

@SuppressWarnings("unchecked")
@Override//from   w  w  w.ja  va  2  s  .  com
public T apply(HttpResponse arg0) {
    Map<String, Map<String, T>> map = json.apply(arg0);
    if (map == null || map.size() == 0)
        return null;
    Map<String, T> map1 = Iterables.getOnlyElement(map.values());
    if (map1 == null || map1.size() == 0) {
        if (type.getRawType().isAssignableFrom(Set.class))
            return (T) ImmutableSet.of();
        else if (type.getRawType().isAssignableFrom(List.class))
            return (T) ImmutableList.of();
        else if (type.getRawType().isAssignableFrom(Map.class))
            return (T) ImmutableMap.of();
        return null;
    }
    return Iterables.getOnlyElement(map1.values());
}

From source file:io.druid.sql.calcite.expression.UnaryPrefixOperatorConversion.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:org.jclouds.ec2.compute.loaders.RegionAndIdToImage.java

@Override
public Image load(RegionAndName key) throws ExecutionException {
    try {//  w w w. ja va  2 s.  co m
        org.jclouds.ec2.domain.Image image = Iterables.getOnlyElement(
                sync.getAMIApi().get().describeImagesInRegion(key.getRegion(), imageIds(key.getName())));
        return parser.apply(image);
    } catch (Exception e) {
        throw new ExecutionException(message(key, e), e);
    }
}