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

@Nullable
public static <T> T getOnlyElement(Iterable<? extends T> iterable, @Nullable T defaultValue) 

Source Link

Document

Returns the single element contained in iterable , or defaultValue if the iterable is empty.

Usage

From source file:org.apache.druid.client.selector.AbstractTierSelectorStrategy.java

@Nullable
@Override/*  w  w w .j  a  va2s . c o  m*/
public QueryableDruidServer pick(Int2ObjectRBTreeMap<Set<QueryableDruidServer>> prioritizedServers,
        DataSegment segment) {
    return Iterables.getOnlyElement(pick(prioritizedServers, segment, 1), null);
}

From source file:org.sonar.db.measure.MeasureDao.java

public Optional<MeasureDto> selectSingle(DbSession dbSession, MeasureQuery query) {
    List<MeasureDto> measures = selectByQuery(dbSession, query);
    return Optional.ofNullable(Iterables.getOnlyElement(measures, null));
}

From source file:org.jclouds.ec2.compute.functions.EC2SecurityGroupIdFromName.java

@Override
public String apply(String input) {
    checkNotNull(input, "input");
    String[] parts = AWSUtils.parseHandle(input);
    String region = parts[0];//  w w w .  j a  v a  2s  .co  m
    String name = parts[1];

    return Iterables
            .getOnlyElement(api.getSecurityGroupApi().get().describeSecurityGroupsInRegion(region, name), null)
            .getId();
}

From source file:org.netbeans.modules.android.grammars.UIClassDescriptors.java

static UIClassDescriptor findByFQName(WidgetData classData, LayoutElementType type, String fqClassName) {
    return Iterables.getOnlyElement(findByName(classData.data.get(type), fqName(fqClassName)), null);
}

From source file:org.apache.drill.exec.schema.ListSchema.java

@Override
public Field getField(String fieldName, int index) {
    Field field;//  w  ww  .  j  a v a2  s .co m
    if (isSingleTyped()) {
        field = Iterables.getOnlyElement(fields, null);
    } else {
        field = index < fields.size() ? fields.get(index) : null;
    }

    return field;
}

From source file:org.netbeans.modules.android.grammars.UIClassDescriptors.java

static UIClassDescriptor findParamsForName(WidgetData classData, String viewGroupName) {
    return Iterables.getOnlyElement(
            findByName(classData.data.get(LayoutElementType.LAYOUT_PARAM), outerClassName(viewGroupName)),
            null);// www.j  a  v  a  2 s.  c  om
}

From source file:com.sun.tools.hat.internal.server.OQLQuery.java

@Override
public void run() {
    startHtml("Object Query Language (OQL) query");
    String oql = Iterables.getOnlyElement(params.get("query"), null);
    out.println("<p align='center'><table>");
    out.println("<tr><td><b>");
    out.println("<a href='/'>All Classes (excluding platform)</a>");
    out.println("</b></td>");
    out.println("<td><b><a href='/oqlhelp/'>OQL Help</a></b></td></tr>");
    out.println("</table></p>");
    out.println("<form action='/oql/' method='get'>");
    out.println("<p align='center'>");
    out.println("<textarea name='query' cols=80 rows=10>");
    if (oql != null) {
        print(oql);/*from  w w w  .  java2s. co  m*/
    }
    out.println("</textarea>");
    out.println("</p>");
    out.println("<p align='center'>");
    out.println("<input type='submit' value='Execute'></input>");
    out.println("</p>");
    out.println("</form>");
    if (oql != null) {
        executeQuery(oql);
    }
    endHtml();
}

From source file:com.b2international.index.revision.DefaultRevisionSearcher.java

@Override
public <T extends Revision> T get(Class<T> type, long storageKey) throws IOException {
    final Query<T> query = Query.select(type).where(Expressions.exactMatch(Revision.STORAGE_KEY, storageKey))
            .limit(2).build();/*  ww  w .  jav  a 2s  .com*/
    return Iterables.getOnlyElement(search(query), null);
}

From source file:org.janusgraph.graphdb.relations.CacheVertexProperty.java

@Override
public InternalRelation it() {
    InternalRelation it = null;/*from   w  w  w.j  av a  2s.  c  om*/
    InternalVertex startVertex = getVertex(0);

    if (startVertex.hasAddedRelations() && startVertex.hasRemovedRelations()) {
        //Test whether this relation has been replaced
        final long id = super.longId();
        it = Iterables.getOnlyElement(startVertex.getAddedRelations(new Predicate<InternalRelation>() {
            @Override
            public boolean apply(@Nullable InternalRelation internalRelation) {
                return (internalRelation instanceof StandardVertexProperty)
                        && ((StandardVertexProperty) internalRelation).getPreviousID() == id;
            }
        }), null);
    }

    return (it != null) ? it : super.it();
}

From source file:org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex.java

@Override
public String name() {
    if (name == null) {
        JanusGraphVertexProperty<String> p;
        if (isLoaded()) {
            StandardJanusGraphTx tx = tx();
            p = (JanusGraphVertexProperty) Iterables
                    .getOnlyElement(/*ww w  . j  a  v a2  s .  c o  m*/
                            RelationConstructor.readRelation(this, tx.getGraph().getSchemaCache()
                                    .getSchemaRelations(longId(), BaseKey.SchemaName, Direction.OUT), tx),
                            null);
        } else {
            p = Iterables.getOnlyElement(query().type(BaseKey.SchemaName).properties(), null);
        }
        Preconditions.checkState(p != null, "Could not find type for id: %s", longId());
        name = p.value();
    }
    assert name != null;
    return JanusGraphSchemaCategory.getName(name);
}