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.errorprone.bugpatterns.IsInstanceOfClass.java

static String buildReplacement(MethodInvocationTree tree, VisitorState state) {

    Operand lhs = classify((JCTree) ASTHelpers.getReceiver(tree.getMethodSelect()), state);
    Operand rhs = classify((JCTree) Iterables.getOnlyElement(tree.getArguments()), state);

    // expr.getClass().isInstance(Bar.class) -> expr instanceof Bar
    if (lhs.kind() == Kind.GET_CLASS && rhs.kind() == Kind.LITERAL) {
        return String.format("%s instanceof %s", lhs.value(), rhs.value());
    }//  w ww  .j  av  a  2 s.  c  o m

    // expr1.getClass().isInstance(expr2.getClass()) -> expr2.getClass().isInstance(expr1)
    if (lhs.kind() == Kind.GET_CLASS && rhs.kind() == Kind.GET_CLASS) {
        return String.format("%s.getClass().isInstance(%s)", rhs.value(), lhs.value());
    }

    // Foo.class.isInstance(Bar.class) -> Bar.class == Class.class
    if (lhs.kind() == Kind.LITERAL && rhs.kind() == Kind.LITERAL) {
        return String.format("%s.class == Class.class", rhs.value()); // !!
    }

    // Foo.class.isInstance(expr.getClass()) -> expr instanceof Foo
    if (lhs.kind() == Kind.LITERAL && rhs.kind() == Kind.GET_CLASS) {
        return String.format("%s instanceof %s", rhs.value(), lhs.value());
    }

    // clazz.isInstance(expr.getClass()) -> clazz.isInstance(expr)
    if (rhs.kind() == Kind.GET_CLASS) {
        return String.format("%s.isInstance(%s)", lhs.source(), rhs.value());
    }

    // expr.getClass().isInstance(clazz) -> clazz.isInstance(expr)
    if (lhs.kind() == Kind.GET_CLASS) {
        return String.format("%s.isInstance(%s)", rhs.source(), lhs.value());
    }

    // clazz1.isInstance(clazz2) -> clazz2.isAssignableFrom(clazz1)
    // clazz.isInstance(Bar.class) -> Bar.class.isAssignableFrom(clazz)
    // Foo.class.isInstance(clazz) -> clazz.isAssignableFrom(Foo.class)
    return String.format("%s.isAssignableFrom(%s)", rhs.source(), lhs.source());
}

From source file:google.registry.util.X509Utils.java

/**
 * Loads an ASCII-armored public X.509 certificate.
 *
 * @throws CertificateParsingException on parsing errors.
 *///from   www.  j  a va  2 s. c  o  m
public static X509Certificate loadCertificate(InputStream input) throws CertificateParsingException {
    try {
        return Iterables.getOnlyElement(
                FluentIterable.from(CertificateFactory.getInstance("X.509").generateCertificates(input))
                        .filter(X509Certificate.class));
    } catch (CertificateException e) { // CertificateParsingException by specification.
        propagateIfInstanceOf(e, CertificateParsingException.class);
        throw new CertificateParsingException(e);
    } catch (NoSuchElementException e) {
        throw new CertificateParsingException("No X509Certificate found.");
    } catch (IllegalArgumentException e) {
        throw new CertificateParsingException("Multiple X509Certificate found.");
    }
}

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

public String getOutputId(TransformHierarchy.Node node) {
    return getIdForPValue(Iterables.getOnlyElement(node.getOutputs().values()));
}

From source file:org.onebusaway.voicexml.actions.StopSearchAction.java

@Override
public String execute() {
    _matches = _nycTransitDataService.getStops(makeSearchQuery(_stopCode)).getStops();

    if (_matches.size() == 1) {
        _stopId = Iterables.getOnlyElement(_matches).getId();
        return "showStopResult";
    } else {/*from  ww w.  java  2s  . c  om*/
        return SUCCESS;
    }
}

From source file:com.google.errorprone.bugpatterns.CatchAndPrintStackTrace.java

@Override
public Description matchCatch(CatchTree tree, VisitorState state) {
    List<? extends StatementTree> statements = tree.getBlock().getStatements();
    if (statements.size() != 1) {
        return NO_MATCH;
    }/*w  w w  .  j  av  a 2s.  c o m*/
    StatementTree statement = Iterables.getOnlyElement(statements);
    if (!MATCHER.matches(statement, state)) {
        return NO_MATCH;
    }
    return describeMatch(statement);
}

From source file:com.opengamma.financial.analytics.model.curve.HardCodedG2ppParametersFunction.java

@Override
public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs,
        final ComputationTarget target, final Set<ValueRequirement> desiredValues)
        throws AsynchronousExecution {
    final ValueProperties properties = Iterables.getOnlyElement(desiredValues).getConstraints().copy().get();
    final ValueSpecification spec = new ValueSpecification(G2PP_PARAMETERS, ComputationTargetSpecification.NULL,
            properties);/*from   ww w  . ja va2s .c  om*/
    return Collections.singleton(new ComputedValue(spec, CONSTANT_PARAMETERS));
}

From source file:com.google.devtools.build.lib.rules.android.ApplicationManifest.java

public static ApplicationManifest fromResourcesRule(RuleContext ruleContext) {
    final AndroidResourcesProvider resources = AndroidCommon.getAndroidResources(ruleContext);
    if (resources == null) {
        ruleContext.attributeError("manifest", "a resources or manifest attribute is mandatory.");
        return null;
    }// ww w  .j  av a 2  s.  c  om
    return new ApplicationManifest(ruleContext,
            Iterables.getOnlyElement(resources.getDirectAndroidResources()).getManifest());
}

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

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

From source file:com.google.template.soy.types.UnionType.java

/**
 * Create a union from a collection of types.
 *
 * @param members Member types of the union.
 * @return Union of those types. If there is exactly one distinct type in members, then this will
 *     not be a UnionType.// w w  w .ja v  a 2s . com
 */
public static SoyType of(Collection<SoyType> members) {
    ImmutableSet<SoyType> flattenedMembers = flatten(members);
    if (flattenedMembers.size() == 1) {
        return Iterables.getOnlyElement(flattenedMembers);
    }
    // unions with the error type should just resolve to the error type to simplify analysis.
    if (flattenedMembers.contains(ErrorType.getInstance())) {
        return ErrorType.getInstance();
    }
    return new UnionType(flattenedMembers);
}

From source file:org.jclouds.ec2.compute.predicates.SecurityGroupPresent.java

protected SecurityGroup refresh(RegionAndName securityGroup) {
    return Iterables.getOnlyElement(client.getSecurityGroupApi().get()
            .describeSecurityGroupsInRegion(securityGroup.getRegion(), securityGroup.getName()));
}