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.axemblr.provisionr.cloudstack.core.Zones.java

public static boolean hasSecurityGroupEnabled(final CloudStackClient cloudStackClient, final String zoneName) {
    Set<Zone> ourZone = Sets.filter(cloudStackClient.getZoneClient().listZones(), new Predicate<Zone>() {
        @Override//from  w  w w .ja  va  2s  . c  om
        public boolean apply(Zone input) {
            return input.getName().equals(zoneName);
        }
    });
    return Iterables.getOnlyElement(ourZone).isSecurityGroupsEnabled();
}

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

/** Asserts result contains single row with an int field. */
public static SerializableFunction<Iterable<Row>, Void> matchesScalar(int expected) {
    return records -> {
        Row row = Iterables.getOnlyElement(records);
        assertNotNull(row);/*from  w  w  w .  ja v a2 s.  com*/
        assertEquals(expected, (int) row.getInt32(0));
        return null;
    };
}

From source file:org.apache.beam.sdk.values.TaggedPValue.java

public static TaggedPValue ofExpandedValue(PValue value) {
    return of(Iterables.getOnlyElement(value.expand().keySet()), value);
}

From source file:org.guicerecipes.support.CompositeCloser.java

/**
 * Returns a {@link Closer} for the given lists of closer strategies or returning null if the collection is empty
 *//*from   w  ww.  j  a  v a2s . co m*/
public static Closer newInstance(Collection<Closer> closers) {
    if (closers.isEmpty()) {
        return null;
    }
    if (closers.size() == 1) {
        return Iterables.getOnlyElement(closers);
    }
    return new CompositeCloser(closers);
}

From source file:com.teradata.benchto.service.model.AggregatedMeasurement.java

public static AggregatedMeasurement aggregate(MeasurementUnit unit, Collection<Double> values) {
    if (values.size() < 2) {
        Double value = Iterables.getOnlyElement(values);
        return new AggregatedMeasurement(unit, value, value, value, 0.0, 0.0);
    }/*from   w w  w.ja  va 2s.c om*/
    DescriptiveStatistics statistics = new DescriptiveStatistics(
            values.stream().mapToDouble(Double::doubleValue).toArray());

    double stdDevPercent = 0.0;
    if (statistics.getStandardDeviation() > 0.0) {
        stdDevPercent = (statistics.getStandardDeviation() / statistics.getMean()) * 100;
    }

    return new AggregatedMeasurement(unit, statistics.getMin(), statistics.getMax(), statistics.getMean(),
            statistics.getStandardDeviation(), stdDevPercent);
}

From source file:org.apache.provisionr.cloudstack.core.Zones.java

public static boolean hasSecurityGroupEnabled(final CloudStackClient cloudStackClient, final String zoneName) {
    Set<Zone> ourZone = Sets.filter(cloudStackClient.getZoneClient().listZones(), new Predicate<Zone>() {
        @Override/*  w w  w  .  ja  v  a 2  s .  c o m*/
        public boolean apply(Zone input) {
            return input != null && input.getName().equals(zoneName);
        }
    });
    return Iterables.getOnlyElement(ourZone).isSecurityGroupsEnabled();
}

From source file:sklearn.TypeUtil.java

static public DataType getDataType(List<?> objects, DataType defaultDataType) {
    Function<Object, Class<?>> function = new Function<Object, Class<?>>() {

        @Override/*from  ww  w. jav a 2 s  . c  o m*/
        public Class<?> apply(Object object) {
            return object.getClass();
        }
    };

    Set<Class<?>> clazzes = new HashSet<>(Lists.transform(objects, function));

    Class<?> clazz = Iterables.getOnlyElement(clazzes);

    DataType dataType = TypeUtil.dataTypes.get(clazz);
    if (dataType != null) {
        return dataType;
    }

    return defaultDataType;
}

From source file:com.opengamma.engine.marketdata.manipulator.NodeExtractor.java

protected static String getSingleProperty(final ValueSpecification spec, final String propertyName) {
    final ValueProperties properties = spec.getProperties();
    final Set<String> curves = properties.getValues(propertyName);
    return Iterables.getOnlyElement(curves);
}

From source file:org.sosy_lab.cpachecker.cpa.smg.refiner.SMGCEGARUtils.java

public static SMGPrecision extractSMGPrecision(final ARGReachedSet pReached, ARGState state) {
    FluentIterable<Precision> precisions = Precisions.asIterable(pReached.asReachedSet().getPrecision(state));

    precisions = precisions.filter((Precision prec) -> {
        return prec instanceof SMGPrecision;
    });//  w  w  w . j a  va  2s . c om

    return (SMGPrecision) Iterables.getOnlyElement(precisions);
}

From source file:org.eclipse.recommenders.rcp.utils.CompilerBindingsTestUtils.java

public static org.eclipse.jdt.internal.compiler.ast.ASTNode getCompilerAstNode(CharSequence code)
        throws Exception {
    JavaProjectFixture fixture = new JavaProjectFixture(ResourcesPlugin.getWorkspace(), "test");
    Pair<CompilationUnit, Set<Integer>> parseResult = fixture.parseWithMarkers(code.toString());
    CompilationUnit cu = parseResult.getFirst();
    int start = Iterables.getOnlyElement(parseResult.getSecond());

    AST ast = cu.getAST();/* w  ww . j  ava2 s . com*/
    ASTNode node = NodeFinder.perform(cu, start, 0);
    return getCorrespondingNode(ast, node);
}