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:de.cosmocode.commons.validation.TrueRule.java

@Override
public Object find(Iterable<Object> iterable, Object defaultValue) {
    Preconditions.checkNotNull(iterable, "Iterable");
    return Iterables.getFirst(iterable, defaultValue);
}

From source file:com.facebook.buck.model.FlavorDomain.java

public Optional<Flavor> getFlavor(Set<Flavor> flavors) {
    Sets.SetView<Flavor> match = Sets.intersection(translation.keySet(), flavors);
    if (match.size() > 1) {
        throw new FlavorDomainException(
                String.format("multiple \"%s\" flavors: %s", name, Joiner.on(", ").join(match)));
    }//from   w ww. j  av a2 s.  c o  m

    return Optional.ofNullable(Iterables.getFirst(match, null));
}

From source file:us.physion.ovation.ui.editor.EntityUtilities.java

public static OvationEntity insertResources(Folder folder, File[] files, List<Resource> addedResources,
        List<Folder> addedFolders) {
    OvationEntity result = null;/*www  .  j a  v  a  2s  .  com*/

    for (File f : files) {
        if (f.isDirectory()) {
            Folder subFolder = folder.addFolder(f.getName());
            addedFolders.add(subFolder);

            FilenameFilter filter = (File dir, String name) -> {
                return !name.startsWith("."); // no hidden files
            };

            insertResources(subFolder, f.listFiles(filter), addedResources, addedFolders);
        } else {
            try {
                Resource r = folder.addResource(f.getName(), f.toURI().toURL(), ContentTypes.getContentType(f));
                addedResources.add(r);
            } catch (IOException ex) {
                throw new OvationException("Unable to add Resource(s)", ex);
            }
        }
    }

    return addedFolders.isEmpty() ? Iterables.getFirst(addedResources, null)
            : Iterables.getFirst(addedFolders, null);
}

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

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

From source file:com.ignorelist.kassandra.steam.scraper.PathResolver.java

public Set<Path> findAllLibraryDirectories() throws IOException, RecognitionException {
    Path steamApps = findSteamApps();
    Set<Path> libraryDirectories = new LinkedHashSet<>();
    libraryDirectories.add(steamApps);//from   w  ww.j a  va2  s .  com
    Path directoryDescriptor = steamApps.resolve("libraryfolders.vdf");
    if (Files.exists(directoryDescriptor)) {
        InputStream vdfStream = Files.newInputStream(directoryDescriptor, StandardOpenOption.READ);
        VdfRoot vdfRoot = VdfParser.parse(vdfStream);
        IOUtils.closeQuietly(vdfStream);
        final VdfNode nodeLibrary = Iterables.getFirst(vdfRoot.getChildren(), null);
        if (null != nodeLibrary) {
            for (VdfAttribute va : nodeLibrary.getAttributes()) {
                //System.err.println(va);
                try {
                    Integer.parseInt(va.getName());
                    Path libraryDirectory = Paths.get(va.getValue());
                    libraryDirectories.add(resolveAppsDirectory(libraryDirectory));
                } catch (NumberFormatException nfe) {
                } catch (IllegalStateException ise) {
                    System.err.println(ise);
                }
            }
        }
    }

    return libraryDirectories;
}

From source file:com.google.devtools.build.lib.analysis.actions.ParamFileHelper.java

/**
 * Returns a params file artifact or null for a given command description.
 *
 *  <p>Returns null if parameter files are not to be used according to paramFileInfo, or if the
 * command line is short enough that a parameter file is not needed.
 *
 * <p>Make sure to add the returned artifact (if not null) as an input of the corresponding
 * action.//from  ww w .  j  a  va2  s. co  m
 *
 * @param executableArgs leading arguments that should never be wrapped in a parameter file
 * @param arguments arguments to the command (in addition to executableArgs), OR
 * @param commandLine a {@link CommandLine} that provides the arguments (in addition to
 *        executableArgs)
 * @param paramFileInfo parameter file information
 * @param configuration the configuration
 * @param analysisEnvironment the analysis environment
 * @param outputs outputs of the action (used to construct a filename for the params file)
 */
static Artifact getParamsFileMaybe(List<String> executableArgs, @Nullable Iterable<String> arguments,
        @Nullable CommandLine commandLine, @Nullable ParamFileInfo paramFileInfo,
        BuildConfiguration configuration, AnalysisEnvironment analysisEnvironment, Iterable<Artifact> outputs) {
    if (paramFileInfo == null) {
        return null;
    }
    if (!paramFileInfo.always()
            && getParamFileSize(executableArgs, arguments, commandLine) < configuration.getMinParamFileSize()) {
        return null;
    }

    Artifact output = Iterables.getFirst(outputs, null);
    Preconditions.checkNotNull(output);
    PathFragment paramFilePath = ParameterFile.derivePath(output.getRootRelativePath());
    return analysisEnvironment.getDerivedArtifact(paramFilePath, output.getRoot());
}

From source file:com.github.blacklocus.rdsecho.utl.RdsFind.java

public Optional<DBInstance> instance(Predicate<DBInstance> predicate) {
    return Optional.fromNullable(Iterables.getFirst(instances(predicate), null));
}

From source file:blue.lapis.pore.impl.entity.PoreVillager.java

@Override
public void setProfession(Profession profession) {
    Career career = Iterables.getFirst(
            Pore.getGame().getRegistry().getCareers(ProfessionConverter.of(profession)), Careers.FARMER);
    assert career != null;
    getHandle().getCareerData().type().set(career);
}

From source file:com.dmdirc.parser.irc.integration.util.DockerContainerRule.java

@Override
@SuppressWarnings("resource")
protected void before() throws Throwable {
    super.before();

    client = DockerClientBuilder.getInstance().build();
    client.pullImageCmd(image).exec(new PullImageResultCallback()).awaitSuccess();

    container = client.createContainerCmd(image).exec().getId();
    client.startContainerCmd(container).exec();

    final ContainerNetwork network = Iterables.getFirst(
            client.inspectContainerCmd(container).exec().getNetworkSettings().getNetworks().values(), null);
    Assume.assumeNotNull(network);//from   w  ww  .  j ava 2s .  co m

    ip = network.getIpAddress();
}

From source file:basedefense.common.grid.SurveillanceGridCache.java

/**
 * {@inheritDoc}
 */
@Override
public ISurveillanceNetworkController getController() {
    return Iterables.getFirst(controllerSet, null);
}