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.trancecode.xml.saxon.SaxonAxis.java

public static XdmNode childElement(final XdmNode node) {
    return Iterables.getOnlyElement(childElements(node), null);
}

From source file:org.trancecode.xml.saxon.SaxonAxis.java

public static XdmNode childElement(final XdmNode node, final Collection<QName> elementNames) {
    return Iterables.getOnlyElement(childElements(node, elementNames), null);
}

From source file:com.clarkparsia.sbol.SBOLObjectFinder.java

public static <T extends SBOLObject> T findObject(final SBOLVisitable visitable, final Predicate<T> condition,
        Class<T> cls) {//  w ww  .j  a  v a 2 s .com
    return Iterables.getOnlyElement(findObjects(visitable, condition, cls), null);
}

From source file:com.threerings.servlet.util.QueryBuilder.java

/**
 * Retrieve the only value for the given key, or the default value if it's not defined. If the
 * key has more than one value defined, an IllegalArgumentException will be thrown.
 *///  w ww.j a v a  2 s.c  o m
public String getOnly(String key, String defaultValue) {
    return Iterables.getOnlyElement(get(key), defaultValue);
}

From source file:org.apache.aurora.scheduler.updater.InstanceActionHandler.java

static Optional<IScheduledTask> getExistingTask(MutableStoreProvider storeProvider, IInstanceKey instance) {

    return Optional.fromNullable(Iterables.getOnlyElement(
            storeProvider.getTaskStore().fetchTasks(Query.instanceScoped(instance).active()), null));
}

From source file:com.b2international.snowowl.datastore.request.job.GetJobRequest.java

@Override
public RemoteJobEntry execute(ServiceProvider context) {
    RemoteJobEntry entry = Iterables.getOnlyElement(
            context.service(RemoteJobTracker.class).search(RemoteJobEntry.Expressions.id(id), 2), null);
    if (entry == null) {
        throw new NotFoundException("job", id);
    } else {//from  w  w  w. java  2s .  c om
        return entry;
    }
}

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

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

From source file:com.palantir.atlasdb.keyvalue.remoting.OctetStreamDelegateDecoder.java

@Override
public Object decode(feign.Response response, Type type) throws IOException, DecodeException, FeignException {
    Collection<String> contentTypes = response.headers().get(HttpHeaders.CONTENT_TYPE);
    if (contentTypes != null && contentTypes.size() == 1
            && Iterables.getOnlyElement(contentTypes, "").equals(MediaType.APPLICATION_OCTET_STREAM)) {
        if (response.body() == null || response.body().length() == null) {
            return null;
        }//from   w  ww  . j a  v  a2s  . c  o  m
        byte[] data = new byte[response.body().length()];
        int bytesRead = 0;
        int bytesLeft = response.body().length();
        while (bytesLeft > 0) {
            int ret = response.body().asInputStream().read(data, bytesRead, bytesLeft);
            if (ret < 0) {
                throw new RuntimeException("Unexpected end of stream");
            }
            bytesLeft -= ret;
            bytesRead += ret;
        }
        return data;
    }
    return delegate.decode(response, type);
}

From source file:pl.softech.eav.example.SimpleInMemmoryRepository.java

protected E findOne(Predicate<E> predicate) {
    return Iterables.getOnlyElement(Iterables.filter(key2entity.values(), predicate), null);
}

From source file:io.druid.client.selector.AbstractTierSelectorStrategy.java

@Override
public QueryableDruidServer pick(Int2ObjectRBTreeMap<Set<QueryableDruidServer>> prioritizedServers,
        DataSegment segment) {/*w ww.j a va  2  s . c  om*/
    return Iterables.getOnlyElement(pick(prioritizedServers, segment, 1), null);
}