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.apache.beam.runners.core.ReplacementOutputs.java

public static Map<PValue, ReplacementOutput> singleton(List<TaggedPValue> original, PValue replacement) {
    TaggedPValue taggedReplacement = Iterables.getOnlyElement(replacement.expand());
    return ImmutableMap.<PValue, ReplacementOutput>builder().put(taggedReplacement.getValue(),
            ReplacementOutput.of(Iterables.getOnlyElement(original), taggedReplacement)).build();
}

From source file:com.axemblr.provisionr.cloudstack.core.Networks.java

/**
 * Returns the first network with the given name.
 *
 * @throws NoSuchElementException   if no network is found
 * @throws IllegalArgumentException if more networks with the same name are found
 *///from  ww  w.  jav  a2  s .c  om
public static Network getByName(CloudStackClient client, final String networkName) {
    Set<Network> networks = Sets.filter(client.getNetworkClient().listNetworks(), new Predicate<Network>() {
        @Override
        public boolean apply(Network input) {
            return input.getName().equals(networkName);
        }
    });
    return Iterables.getOnlyElement(networks);
}

From source file:org.apache.beam.sdk.extensions.sql.utils.RowAsserts.java

/** Asserts result contains single row with an long field. */
public static SerializableFunction<Iterable<Row>, Void> matchesScalar(long expected) {
    return records -> {
        Row row = Iterables.getOnlyElement(records);
        assertNotNull(row);/*from w w w  .java  2 s. c o  m*/
        assertEquals(expected, (long) row.getInt64(0));
        return null;
    };
}

From source file:com.cinchapi.common.collect.Multimaps.java

private static Object flatten(Collection<Object> value) {
    if (value.isEmpty()) {
        return null;
    } else if (value.size() == 1) {
        return Iterables.getOnlyElement(value);
    } else {/*w ww  .  ja v  a 2  s  . com*/
        return value;
    }
}

From source file:org.apache.beam.runners.core.construction.ReplacementOutputs.java

public static Map<PValue, ReplacementOutput> singleton(Map<TupleTag<?>, PValue> original, PValue replacement) {
    Entry<TupleTag<?>, PValue> originalElement = Iterables.getOnlyElement(original.entrySet());
    TupleTag<?> replacementTag = Iterables.getOnlyElement(replacement.expand().entrySet()).getKey();
    return Collections.singletonMap(replacement,
            ReplacementOutput.of(TaggedPValue.of(originalElement.getKey(), originalElement.getValue()),
                    TaggedPValue.of(replacementTag, replacement)));
}

From source file:com.wrmsr.wava.java.lang.tree.JTrees.java

public static JBlock jblockify(List<JStatement> statements) {
    if (statements.size() == 1) {
        return jblockify(Iterables.getOnlyElement(statements));
    } else {/*from  www  . j a  v  a 2s  .c  om*/
        return new JBlock(
                statements.stream().map(JTrees::jflatten).flatMap(List::stream).collect(toImmutableList()));
    }
}

From source file:brooklyn.entity.rebind.transformer.CompoundTransformerLoader.java

public static CompoundTransformer load(String contents) {
    CompoundTransformer.Builder builder = CompoundTransformer.builder();
    Iterable<Object> toplevel = Yamls.parseAll(contents);
    @SuppressWarnings("unchecked")
    Map<String, Map<?, ?>> rules = (Map<String, Map<?, ?>>) ((Map<?, ?>) Iterables.getOnlyElement(toplevel));
    for (Map.Entry<String, Map<?, ?>> entry : rules.entrySet()) {
        addRule(builder, entry.getKey(), entry.getValue());
    }//from   ww w  .  j a  v a 2  s.  c om
    return builder.build();
}

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

/** Generates a single token of a given {@link TokenType}. */
public static String createToken(TokenType type, StringGenerator generator) {
    return Iterables.getOnlyElement(createTokens(type, generator, 1));
}

From source file:com.google.devtools.build.android.xml.ResourcesAttribute.java

public static ResourcesAttribute from(SerializeFormat.DataValueXml proto) {
    Map.Entry<String, String> attribute = Iterables.getOnlyElement(proto.getAttributeMap().entrySet());
    return of(attribute.getKey(), attribute.getValue());
}

From source file:com.google.caliper.runner.worker.dryrun.DryRunModule.java

@Provides
@Reusable/*from   www .j  a  v  a 2 s. c o m*/
static Target provideTarget(Set<Experiment> experiments) {
    checkArgument(!experiments.isEmpty(), "Dry-run component bound no experiments.");

    Set<Target> targets = new HashSet<>();
    for (Experiment experiment : experiments) {
        targets.add(experiment.target());
    }

    checkArgument(targets.size() == 1, "All dry-run component experiments should have the same target. "
            + "Experiments for %s different targets were found.", targets.size());

    return Iterables.getOnlyElement(targets);
}