Example usage for com.google.common.collect Iterables getFirst

List of usage examples for com.google.common.collect Iterables getFirst

Introduction

In this page you can find the example usage for com.google.common.collect Iterables getFirst.

Prototype

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

Source Link

Document

Returns the first element in iterable or defaultValue if the iterable is empty.

Usage

From source file:com.google.idea.blaze.base.run.targetfinder.TargetFinder.java

@Nullable
public TargetIdeInfo findFirstTarget(Project project, Predicate<TargetIdeInfo> predicate) {
    return Iterables.getFirst(findTargets(project, predicate), null);
}

From source file:org.jclouds.examples.rackspace.clouddatabases.CreateInstance.java

/**
 * @return Flavor The first Flavor available.
 */
private Flavor getFlavor() {
    return Iterables.getFirst(flavorApi.list(), null);
}

From source file:ratpack.launch.internal.LaunchConfigsInternal.java

public static LaunchConfigData createFromProperties(String workingDir, ClassLoader classLoader,
        Properties overrideProperties, Properties defaultProperties) {
    String configResourceValue = overrideProperties.getProperty(CONFIG_RESOURCE_PROPERTY,
            CONFIG_RESOURCE_DEFAULT);/*from w  ww. j  a  v a  2  s.  c o  m*/
    URL configResourceUrl = classLoader.getResource(configResourceValue);

    Path configPath;
    Path baseDir;

    if (configResourceUrl == null) {
        configPath = Paths.get(configResourceValue);
        if (!configPath.isAbsolute()) {
            configPath = Paths.get(workingDir, configResourceValue);
        }

        baseDir = configPath.getParent();
    } else {
        configPath = resourceToPath(configResourceUrl);
        baseDir = configPath.getParent();
        if (baseDir == null && configPath.getFileSystem().provider() instanceof ZipFileSystemProvider) {
            baseDir = Iterables.getFirst(configPath.getFileSystem().getRootDirectories(), null);
        }

        if (baseDir == null) {
            throw new LaunchException("Cannot determine base dir given config resource: " + configPath);
        }
    }

    return createFromFile(classLoader, baseDir, configPath, overrideProperties, defaultProperties);
}

From source file:org.jclouds.cloudstack.compute.strategy.AdvancedNetworkOptionsConverter.java

@Override
public DeployVirtualMachineOptions apply(CloudStackTemplateOptions templateOptions,
        Map<String, Network> networks, String zoneId, DeployVirtualMachineOptions options) {
    // security groups not allowed.
    // at least one network must be given to CloudStack,
    // but jclouds will try to autodetect an appropriate network if none given.
    checkArgument(templateOptions.getSecurityGroupIds().isEmpty(),
            "security groups cannot be specified for locations (zones) that use advanced networking");
    if (templateOptions.getNetworkIds().size() > 0) {
        options.networkIds(templateOptions.getNetworkIds());
    } else if (templateOptions.getIpsToNetworks().isEmpty()) {
        checkArgument(!networks.isEmpty(), "please setup a network for zone: " + zoneId);
        Network defaultNetworkInZone = Iterables.getFirst(
                filter(networks.values(), and(defaultNetworkInZone(zoneId), supportsStaticNAT())), null);
        if (defaultNetworkInZone == null) {
            defaultNetworkInZone = Iterables.getFirst(filter(networks.values(), isIsolatedNetwork()), null);
        }//from w w  w .  j  a va 2  s.  c o  m
        if (defaultNetworkInZone == null) {
            throw new IllegalArgumentException(
                    "please choose a specific network in zone " + zoneId + ": " + networks);
        } else {
            options.networkId(defaultNetworkInZone.getId());
        }
    }
    return options;
}

From source file:org.gradle.model.collection.internal.DomainObjectContainerModelProjection.java

public static String getBuilderTypeDescriptionForCreatableTypes(
        Collection<? extends Class<?>> createableTypes) {
    StringBuilder sb = new StringBuilder(CollectionBuilder.class.getName());
    if (createableTypes.size() == 1) {
        @SuppressWarnings("ConstantConditions")
        String onlyType = Iterables.getFirst(createableTypes, null).getName();
        sb.append("<").append(onlyType).append(">");
    } else {/*  w  ww. jav a2  s .c om*/
        sb.append("<T>; where T is one of [");
        Joiner.on(", ").appendTo(sb,
                CollectionUtils.sort(Iterables.transform(createableTypes, new Function<Class<?>, String>() {
                    public String apply(Class<?> input) {
                        return input.getName();
                    }
                })));
        sb.append("]");
    }
    return sb.toString();
}

From source file:org.dcache.gridsite.SrmCredentialStore.java

@Override
public void put(DelegationIdentity id, GSSCredential credential) throws DelegationException {
    try {//from w  ww  . ja  v a 2 s .c  o  m
        Iterable<String> fqans = GSSUtils.getFQANsFromGSSCredential(vomsDir, caDir, credential);
        String primaryFqan = Iterables.getFirst(fqans, null);

        RequestCredential srmCredential = new RequestCredential(nameFromId(id), primaryFqan, credential,
                _store);
        _store.saveRequestCredential(srmCredential);
    } catch (AuthorizationException | GSSException | RuntimeException e) {
        throw new DelegationException("failed to save credential: " + e.getMessage());
    }
}

From source file:io.helixservice.feature.restservice.controller.Request.java

/**
 * Gets the first value found for the given header
 *
 * @param headerName Header name/*ww w.j ava  2 s  .c  o m*/
 * @param defaultValue Default value if the header does not exist
 * @return The first header value found, or the default value
 */
public String getHeader(String headerName, String defaultValue) {
    return Iterables.getFirst(headersUppercase.get(headerName.toUpperCase()), defaultValue);
}

From source file:org.opendaylight.yangtools.binding.data.codec.impl.DataContainerCodecPrototype.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private DataContainerCodecPrototype(final Class<?> cls, final YangInstanceIdentifier.PathArgument arg,
        final T nodeSchema, final CodecContextFactory factory) {
    super();/* w  w w. j a v  a  2  s.co m*/
    this.bindingClass = cls;
    this.yangArg = arg;
    this.schema = nodeSchema;
    this.factory = factory;
    this.bindingArg = new InstanceIdentifier.Item(bindingClass);

    if (arg instanceof AugmentationIdentifier) {
        this.namespace = Iterables.getFirst(((AugmentationIdentifier) arg).getPossibleChildNames(), null)
                .getModule();
    } else {
        this.namespace = arg.getNodeType().getModule();
    }
}

From source file:org.xacml4j.v30.marshal.json.AttributeSerializer.java

private void serializeValue(JsonSerializationContext context, JsonObject o, Collection<AttributeExp> values) {
    checkArgument(values != null && !values.isEmpty(), "Attribute value is mandatory.");
    AttributeExp firstValue = Iterables.getFirst(values, null);
    o.addProperty(DATA_TYPE_PROPERTY, firstValue.getType().getShortDataTypeId());
    Optional<TypeToGSon> toGson = TypeToGSon.Types.getIndex().get(firstValue.getType());
    checkState(toGson.isPresent());//from   w  ww .  j  a v a2 s. co  m
    if (values.size() == 1) {
        o.add(VALUE_PROPERTY, toGson.get().toJson(firstValue, context));
        return;
    }
    JsonArray array = new JsonArray();
    for (AttributeExp a : values) {
        checkArgument(firstValue.getType().equals(a.getType()));
        array.add(toGson.get().toJson(a, context));
    }
    o.add(VALUE_PROPERTY, array);
}

From source file:org.obm.push.minig.imap.command.LoginCommand.java

@Override
protected String commandToBeLogged(String sent) {
    String requestId = Iterables.getFirst(Splitter.on(" ").split(sent), "");
    return requestId + " " + passwordFilteredCommand;
}