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.sonar.batch.rule.QProfileSensor.java

public void analyse(Project project, SensorContext context) {
    for (String language : fs.languages()) {
        ModuleQProfiles.QProfile qProfile = moduleQProfiles.findByLanguage(language);
        if (qProfile != null) {
            dao.updateUsedColumn(qProfile.id(), true);
        }/*  w w  w . j  a v  a  2 s  . c  o m*/
    }
    if (fs.languages().size() == 1) {
        String language = Iterables.getOnlyElement(fs.languages());
        ModuleQProfiles.QProfile qProfile = moduleQProfiles.findByLanguage(language);
        if (qProfile != null) {
            Measure measure = new Measure(CoreMetrics.PROFILE, qProfile.name())
                    .setValue((double) qProfile.id());
            Measure measureVersion = new Measure(CoreMetrics.PROFILE_VERSION, qProfile.version().doubleValue());
            context.saveMeasure(measure);
            context.saveMeasure(measureVersion);
        }
    }
}

From source file:org.jclouds.gogrid.predicates.ServerLatestJobCompleted.java

@Override
public boolean apply(Server server) {
    checkNotNull(server, "Server must be a valid instance");
    checkNotNull(server.getName(), "Server must be a valid name");
    Job latestJob = Iterables.getOnlyElement(jobClient.getJobList(latestJobForObjectByName(server.getName())));
    return JobState.SUCCEEDED.equals(latestJob.getCurrentState());
}

From source file:dagger.android.processor.AndroidMapKeys.java

private static TypeMirror mapKeyValue(Class<? extends Annotation> annotation, Elements elements) {
    List<ExecutableElement> mapKeyMethods = methodsIn(
            elements.getTypeElement(annotation.getCanonicalName()).getEnclosedElements());
    TypeMirror returnType = Iterables.getOnlyElement(mapKeyMethods).getReturnType();
    // TODO(ronshapiro): replace with MoreTypes.asWildcard() when auto-common 0.9 is released
    return ((WildcardType) Iterables.getOnlyElement(MoreTypes.asDeclared(returnType).getTypeArguments()))
            .getExtendsBound();//from ww  w.  j  a v a  2  s.  c  om
}

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

@Override
public PlanNode replaceChildren(List<PlanNode> newChildren) {
    checkArgument(newChildren.size() == 1, "expected newChildren to contain 1 node");
    return new AssignUniqueId(getId(), Iterables.getOnlyElement(newChildren), idColumn);
}

From source file:org.sonar.java.checks.SillyEqualsCheck.java

@Override
protected void onMethodInvocationFound(MethodInvocationTree tree) {
    ExpressionTree firstArgument = Iterables.getOnlyElement(tree.arguments());
    Type argumentType = firstArgument.symbolType().erasure();
    if (argumentType.isPrimitive()) {
        argumentType = ((JavaType) argumentType).primitiveWrapperType();
    }//from   w w  w  .  jav a  2s. c o  m
    Type ownerType = getMethodOwnerType(tree).erasure();
    IdentifierTree methodInvocationName = MethodsHelper.methodName(tree);
    if (isLiteralNull(firstArgument)) {
        reportIssue(methodInvocationName,
                "Remove this call to \"equals\"; comparisons against null always return false; consider using '== null' to check for nullity.");
    } else if (ownerType.isArray()) {
        checkWhenOwnerIsArray(methodInvocationName, (Type.ArrayType) ownerType, argumentType);
    } else {
        checkWhenOwnerIsNotArray(methodInvocationName, ownerType, argumentType);
    }
}

From source file:org.apache.beam.runners.dataflow.worker.graph.RemoveFlattenInstructionsFunction.java

@Override
public MutableNetwork<Node, Edge> apply(MutableNetwork<Node, Edge> network) {
    for (Node node : ImmutableList.copyOf(Iterables.filter(network.nodes(), IsFlatten.INSTANCE))) {

        // For each successor instruction after the Flatten, connect it directly to the
        // predecessor PCollections of Flatten.
        Node flattenPCollection = Iterables.getOnlyElement(network.successors(node));
        for (Node successorInstruction : ImmutableList.copyOf(network.successors(flattenPCollection))) {
            for (Edge edge : ImmutableList
                    .copyOf(network.edgesConnecting(flattenPCollection, successorInstruction))) {
                for (Node predecessorPCollection : ImmutableList.copyOf(network.predecessors(node))) {
                    network.addEdge(predecessorPCollection, successorInstruction, edge.clone());
                }/*from w  ww .j  a v a2  s .  c  o  m*/
            }
        }

        // Remove the Flatten instruction and its output PCollection.
        network.removeNode(flattenPCollection);
        network.removeNode(node);
    }
    return network;
}

From source file:edu.uci.ics.jung.graph.DelegateCTree.java

@Override
public Optional<N> predecessor(N node) {
    checkNotNull(node, "node");
    Set<N> predecessors = delegate.predecessors(node);
    checkState(predecessors.size() <= 1);
    return predecessors.isEmpty() ? Optional.empty() : Optional.of(Iterables.getOnlyElement(predecessors));
}

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

@Override
public <T> T lookupDefault(final String vpcId, final Function<? super NetworkGroup, T> transform)
        throws VpcMetadataException {
    try {/*  w  w  w  . j a v a 2 s. c  om*/
        return Iterables.getOnlyElement(
                listByExample(NetworkGroup.namedForVpc(vpcId, NetworkGroups.defaultNetworkName()),
                        Predicates.alwaysTrue(), transform));
    } catch (NoSuchElementException e) {
        throw new VpcMetadataNotFoundException("Default security group not found for " + vpcId);
    }
}

From source file:grakn.core.graql.reasoner.utils.TarjanSCC.java

/**
 * A cycle isa a connected component that has more than one vertex or a single vertex linked to itself.
 * @return list of cycles in the graph/*from  w  w  w .  ja  va  2 s  .  co m*/
 */
public List<Set<T>> getCycles() {
    return scc.stream()
            .filter(cc -> cc.size() > 1
                    || graph.get(Iterables.getOnlyElement(cc)).contains(Iterables.getOnlyElement(cc)))
            .collect(Collectors.toList());
}

From source file:com.google.gerrit.server.query.Predicate.java

/** Combine the passed predicates into a single OR node. */
public static <T> Predicate<T> or(final Collection<? extends Predicate<T>> that) {
    if (that.size() == 1) {
        return Iterables.getOnlyElement(that);
    }//ww  w  . jav a 2 s .com
    return new OrPredicate<T>(that);
}