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

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

From source file:org.mule.module.http.internal.listener.HttpListenerConnectionManager.java

@Override
public void initialise() throws InitialisationException {
    Collection<TcpServerSocketProperties> tcpServerSocketPropertiesBeans = muleContext.getRegistry()
            .lookupObjects(TcpServerSocketProperties.class);
    TcpServerSocketProperties tcpServerSocketProperties = new DefaultTcpServerSocketProperties();

    if (tcpServerSocketPropertiesBeans.size() == 1) {
        tcpServerSocketProperties = Iterables.getOnlyElement(tcpServerSocketPropertiesBeans);
    } else if (tcpServerSocketPropertiesBeans.size() > 1) {
        throw new InitialisationException(
                CoreMessages.createStaticMessage(
                        "Only one global TCP server socket properties bean should be defined in the config"),
                this);
    }//from   w  w w. ja  va2  s  . c o  m

    String threadNamePrefix = ThreadNameHelper.getPrefix(muleContext) + LISTENER_THREAD_NAME_PREFIX;
    try {
        httpServerManager = new GrizzlyServerManager(threadNamePrefix, httpListenerRegistry,
                tcpServerSocketProperties);
    } catch (IOException e) {
        throw new InitialisationException(e, this);
    }

}

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

@Override
public <T> T lookupDefault(final String vpcId, final Function<? super NetworkAcl, T> transform)
        throws VpcMetadataException {
    try {/*  w w w. j  a  v  a 2  s.  c  om*/
        return Iterables.getOnlyElement(listByExample(NetworkAcl.exampleDefault(), Predicates.alwaysTrue(),
                Restrictions.eq("vpc.displayName", vpcId), Collections.singletonMap("vpc", "vpc"), transform));
    } catch (NoSuchElementException e) {
        throw new VpcMetadataNotFoundException("Default network acl not found for " + vpcId);
    }
}

From source file:com.opengamma.financial.analytics.model.credit.isda.cdsoption.ISDACreditDefaultSwapOptionRR01Function.java

@Override
protected Set<ComputedValue> getComputedValue(final CreditDefaultSwapOptionDefinition definition,
        final ISDADateCurve yieldCurve, final double vol, final ZonedDateTime[] calibrationTenors,
        final double[] marketSpreads, final HazardRateCurve hazardRateCurve, final ZonedDateTime valuationTime,
        final ComputationTarget target, final ValueProperties properties) {
    final Double recoveryRateCurveBump = Double.valueOf(Iterables.getOnlyElement(
            properties.getValues(CreditInstrumentPropertyNamesAndValues.PROPERTY_RECOVERY_RATE_CURVE_BUMP)));
    final RecoveryRateBumpType recoveryRateBumpType = RecoveryRateBumpType.valueOf(Iterables.getOnlyElement(
            properties.getValues(CreditInstrumentPropertyNamesAndValues.PROPERTY_RECOVERY_RATE_BUMP_TYPE)));
    final double rr01 = CALCULATOR.getRecoveryRate01CreditDefaultSwapOption(valuationTime, definition, vol,
            yieldCurve, hazardRateCurve, calibrationTenors, marketSpreads, recoveryRateCurveBump,
            recoveryRateBumpType);//from www.  ja va  2 s  . c  o m
    final ValueSpecification spec = new ValueSpecification(ValueRequirementNames.RR01, target.toSpecification(),
            properties);
    return Collections.singleton(new ComputedValue(spec, rr01));
}

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

@Override
public <T> T lookupMain(final String vpcId, final Function<? super RouteTable, T> transform)
        throws VpcMetadataException {
    try {//from www  . jav  a 2s .  c  om
        return Iterables.getOnlyElement(listByExample(RouteTable.exampleMain(), Predicates.alwaysTrue(),
                Restrictions.eq("vpc.displayName", vpcId), Collections.singletonMap("vpc", "vpc"), transform));
    } catch (NoSuchElementException e) {
        throw new VpcMetadataNotFoundException("Main route table not found for " + vpcId);
    }
}

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

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

From source file:com.google.devtools.build.lib.analysis.config.transitions.ComposingPatchTransition.java

@Override
public BuildOptions patch(BuildOptions options) {
    return Iterables.getOnlyElement(delegate.split(options));
}

From source file:org.jclouds.softlayer.compute.functions.DatacenterToLocation.java

@Override
public Location apply(Datacenter datacenter) {
    return new LocationBuilder().scope(LocationScope.ZONE).metadata(ImmutableMap.<String, Object>of())
            .description(datacenter.getLongName()).id(Long.toString(datacenter.getId()))
            .iso3166Codes(createIso3166Codes(datacenter.getLocationAddress()))
            .parent(Iterables.getOnlyElement(provider.get())).build();
}

From source file:org.eclipse.xtext.xbase.typesystem.util.BoundTypeArgumentMerger.java

public LightweightMergedBoundTypeArgument merge(Collection<LightweightBoundTypeArgument> allArguments,
        ITypeReferenceOwner owner) {// www.  ja  v a  2  s. c o  m
    if (allArguments.isEmpty())
        return null;
    if (allArguments.size() == 1) {
        LightweightBoundTypeArgument argument = Iterables.getOnlyElement(allArguments);
        return getSingleArgumentAsMergedArgument(argument);
    }
    List<LightweightTypeReference> invariantTypes = Lists.newArrayListWithCapacity(0);
    List<VarianceInfo> invariantVariances = Lists.newArrayListWithCapacity(0);
    List<LightweightTypeReference> invariantTypesFromOut = Lists.newArrayListWithCapacity(0);
    List<VarianceInfo> invariantVariancesFromOut = Lists.newArrayListWithCapacity(0);
    List<LightweightTypeReference> outTypes = Lists.newArrayListWithCapacity(0);
    List<LightweightTypeReference> constraintOutTypes = Lists.newArrayListWithCapacity(0);
    List<VarianceInfo> outVariances = Lists.newArrayListWithCapacity(0);
    List<LightweightTypeReference> inTypes = Lists.newArrayListWithCapacity(0);
    List<VarianceInfo> inVariances = Lists.newArrayListWithCapacity(0);
    Set<Object> seenOrigin = Sets.newHashSet();
    for (LightweightBoundTypeArgument boundTypeArgument : allArguments) {
        Object origin = boundTypeArgument.getOrigin();
        switch (boundTypeArgument.getDeclaredVariance()) {
        case INVARIANT:
            processBoundTypeArgument(boundTypeArgument, invariantTypes, invariantVariances, origin, seenOrigin);
            break;
        case OUT:
            BoundTypeArgumentSource source = boundTypeArgument.getSource();
            if (invariantTypes.isEmpty() && isTransitiveHintFromReslved(boundTypeArgument, origin, source)) {
                invariantTypesFromOut.add(boundTypeArgument.getTypeReference());
                if (seenOrigin.add(origin)) {
                    invariantVariancesFromOut.add(VarianceInfo.INVARIANT);
                }
            }
            if (source == BoundTypeArgumentSource.CONSTRAINT) {
                constraintOutTypes.add(boundTypeArgument.getTypeReference());
            } else {
                outTypes.add(boundTypeArgument.getTypeReference());
            }
            addVariance(boundTypeArgument, outVariances, origin, seenOrigin);
            break;
        case IN:
            processBoundTypeArgument(boundTypeArgument, inTypes, inVariances, origin, seenOrigin);
            break;
        }
    }
    LightweightTypeReference type = null;
    VarianceInfo variance = null;
    if (outTypes.isEmpty() && inTypes.isEmpty()) {
        outTypes = constraintOutTypes;
    }
    if (!invariantTypes.isEmpty()) {
        type = invariantTypes.get(0);
        variance = VarianceInfo.INVARIANT.mergeDeclaredWithActuals(invariantVariances);
        if (variance == null && invariantVariances.contains(VarianceInfo.IN) && invariantTypes.size() > 1) {
            type = getCommonSuperTypes(invariantTypes, owner);
        } else if (!invariantTypesFromOut.isEmpty()) {
            LightweightTypeReference fromOut = invariantTypesFromOut.get(0);
            if (fromOut.isAssignableFrom(type)) {
                type = fromOut;
                variance = VarianceInfo.INVARIANT;
            }
        }
        if (!outVariances.isEmpty()) {
            VarianceInfo outVariance = VarianceInfo.OUT.mergeDeclaredWithActuals(outVariances);
            variance = VarianceInfo.OUT.mergeInvariance(variance, outVariance);
        } else if (!inVariances.isEmpty()) {
            VarianceInfo inVariance = VarianceInfo.IN.mergeDeclaredWithActuals(inVariances);
            variance = VarianceInfo.IN.mergeInvariance(variance, inVariance);
        }
    } else if (!outTypes.isEmpty()) {
        type = getCommonSuperTypes(outTypes, owner);
        variance = VarianceInfo.OUT.mergeDeclaredWithActuals(outVariances);
        if (!inVariances.isEmpty()) {
            LightweightTypeReference inType = getMostSpecialType(inTypes);
            boolean conformant = type.isAssignableFrom(inType,
                    new TypeConformanceComputationArgument(false, true, false, false, true, false));
            if (conformant) {
                VarianceInfo inVariance = VarianceInfo.IN.mergeDeclaredWithActuals(inVariances);
                variance = VarianceInfo.IN.mergeWithOut(variance, inVariance, conformant);
            } else {
                boolean reverseConformant = inType.isAssignableFrom(type,
                        new TypeConformanceComputationArgument(false, false, false, false, true, false));
                if (reverseConformant && variance == VarianceInfo.INVARIANT
                        && VarianceInfo.IN.mergeDeclaredWithActuals(inVariances) == VarianceInfo.INVARIANT) {
                    if (VarianceInfo.IN.mergeDeclaredWithActuals(outVariances) != null) {
                        type = inType;
                        variance = VarianceInfo.OUT;
                    }
                } else {
                    VarianceInfo inVariance = VarianceInfo.IN.mergeDeclaredWithActuals(inVariances);
                    variance = VarianceInfo.IN.mergeWithOut(variance, inVariance, conformant);
                }
            }
        }
    } else if (!inTypes.isEmpty()) {
        type = getMostSpecialType(inTypes);
        variance = VarianceInfo.IN.mergeDeclaredWithActuals(inVariances);
    }
    return new LightweightMergedBoundTypeArgument(type, variance);
}

From source file:org.obiba.opal.core.cfg.OpalConfiguration.java

public <T extends OpalConfigurationExtension> T getExtension(Class<T> type) {
    try {/*from  ww w .j av  a2 s .c o  m*/
        return Iterables.getOnlyElement(Iterables.filter(extensions, type));
    } catch (IndexOutOfBoundsException e) {
        throw new NoSuchElementException();
    }
}