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.sqs.xml.IdHandler.java

@Override
public Map.Entry<String, String> apply(String in) {
    return Iterables.getOnlyElement(ImmutableMap.of(in, in).entrySet());
}

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

@Override
public T apply(HttpResponse arg0) {
    Map<String, T> map = json.apply(arg0);
    return Iterables.getOnlyElement(map.values());
}

From source file:com.opengamma.financial.convention.DefaultConventionSource.java

@Override
public Convention getConvention(final ExternalId identifier) {
    final ConventionSearchResult result = _conventionMaster
            .searchConvention(new ConventionSearchRequest(identifier));
    final int size = result.getResults().size();
    switch (size) {
    case 0:/*from   w  w w  .  j av a2 s  . co m*/
        return null;
    case 1:
        return Iterables.getOnlyElement(result.getResults()).getConvention();
    default:
        throw new OpenGammaRuntimeException(
                "Multiple matches (" + size + ") to " + identifier + "; expecting one");
    }
}

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

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

From source file:org.jclouds.gogrid.functions.ParseImageFromJsonResponse.java

@Override
public ServerImage apply(HttpResponse arg0) {
    return Iterables.getOnlyElement(parser.apply(arg0));
}

From source file:dagger.producers.monitoring.internal.Monitors.java

/**
 * Returns a monitor factory that delegates to the given factories, and ensures that any method
 * called on this object, even transitively, does not throw a {@link RuntimeException} or return
 * null./*from w w  w .jav a2  s .  co m*/
 *
 * <p>If the delegate monitors throw an {@link Error}, then that will escape this monitor
 * implementation. Errors are treated as unrecoverable conditions, and may cause the entire
 * component's execution to fail.
 */
public static ProductionComponentMonitor.Factory delegatingProductionComponentMonitorFactory(
        Collection<? extends ProductionComponentMonitor.Factory> factories) {
    if (factories.isEmpty()) {
        return ProductionComponentMonitor.Factory.noOp();
    } else if (factories.size() == 1) {
        return new NonThrowingProductionComponentMonitor.Factory(Iterables.getOnlyElement(factories));
    } else {
        return new DelegatingProductionComponentMonitor.Factory(factories);
    }
}

From source file:org.apache.drill.exec.physical.impl.limit.LimitBatchCreator.java

@Override
public LimitRecordBatch getBatch(FragmentContext context, Limit config, List<RecordBatch> children)
        throws ExecutionSetupException {
    return new LimitRecordBatch(config, context, Iterables.getOnlyElement(children));
}

From source file:eu.interedition.collatex.util.ParallelSegmentationApparatus.java

public static void generate(VariantGraphRanking ranking, GeneratorCallback callback) {

    callback.start();/*from w  ww.  j av  a2 s.c o m*/

    final Set<Witness> allWitnesses = ranking.witnesses();
    for (Iterator<Map.Entry<Integer, Collection<VariantGraph.Vertex>>> rowIt = ranking.getByRank().asMap()
            .entrySet().iterator(); rowIt.hasNext();) {
        final Map.Entry<Integer, Collection<VariantGraph.Vertex>> row = rowIt.next();
        final int rank = row.getKey();
        final Collection<VariantGraph.Vertex> vertices = row.getValue();

        if (vertices.size() == 1 && Iterables.getOnlyElement(vertices).tokens().isEmpty()) {
            // skip start and end vertex
            continue;
        }

        // spreading vertices with same rank according to their registered transpositions
        final Multimap<Integer, VariantGraph.Vertex> verticesByTranspositionRank = HashMultimap.create();
        for (VariantGraph.Vertex v : vertices) {
            int transpositionRank = 0;
            for (VariantGraph.Transposition transposition : v.transpositions()) {
                for (VariantGraph.Vertex tv : transposition) {
                    transpositionRank += (ranking.apply(tv).intValue() - rank);
                }
            }
            verticesByTranspositionRank.put(transpositionRank, v);
        }

        // render segments
        for (Iterator<Integer> transpositionRankIt = Ordering.natural()
                .immutableSortedCopy(verticesByTranspositionRank.keySet()).iterator(); transpositionRankIt
                        .hasNext();) {
            final Multimap<Witness, Token> tokensByWitness = HashMultimap.create();
            for (VariantGraph.Vertex v : verticesByTranspositionRank.get(transpositionRankIt.next())) {
                for (Token token : v.tokens()) {
                    tokensByWitness.put(token.getWitness(), token);
                }
            }

            final SortedMap<Witness, Iterable<Token>> cellContents = Maps.newTreeMap(Witness.SIGIL_COMPARATOR);
            for (Witness witness : allWitnesses) {
                cellContents.put(witness,
                        tokensByWitness.containsKey(witness)
                                ? Iterables.unmodifiableIterable(tokensByWitness.get(witness))
                                : Collections.<Token>emptySet());
            }

            callback.segment(cellContents);
        }
    }

    callback.end();
}

From source file:com.opengamma.master.security.impl.AbstractSecurityLoader.java

@Override
public UniqueId loadSecurity(ExternalIdBundle externalIdBundle) {
    SecurityLoaderRequest request = SecurityLoaderRequest.create(externalIdBundle);
    SecurityLoaderResult result = loadSecurities(request);
    if (result.getResultMap().size() == 0) {
        throw new OpenGammaRuntimeException("Unable to load security: " + externalIdBundle);
    }//ww  w  .  java  2s.c o m
    return Iterables.getOnlyElement(result.getResultMap().values());
}

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

public static SerializableFunction<Iterable<Row>, Void> matchesScalar(float expected, float delta) {

    return input -> {
        Row row = Iterables.getOnlyElement(input);
        assertNotNull(row);//from   ww  w . jav a2s .  co  m
        assertEquals(expected, row.getFloat(0), delta);
        return null;
    };
}