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:org.eclipse.buildship.core.workspace.internal.EclipseVmUtil.java

private static Optional<IVMInstall> findRegisteredVM(String version) {
    Optional<IExecutionEnvironment> possibleExecutionEnvironment = findExecutionEnvironment(version);
    if (!possibleExecutionEnvironment.isPresent()) {
        return Optional.absent();
    }/*w  w  w  .j  a va2s.  co m*/

    IExecutionEnvironment executionEnvironment = possibleExecutionEnvironment.get();
    IVMInstall defaultVm = executionEnvironment.getDefaultVM();
    if (defaultVm != null) {
        return Optional.of(defaultVm);
    } else {
        IVMInstall firstVm = Iterables.getFirst(Arrays.asList(executionEnvironment.getCompatibleVMs()), null);
        return Optional.fromNullable(firstVm);
    }
}

From source file:net.holmes.core.common.MimeType.java

/**
 * Instantiates a new mime type./*from w  w w. j ava  2s . com*/
 *
 * @param mimeType mime type
 */
private MimeType(final String mimeType) {
    this.mimeType = mimeType;
    Iterable<String> iterable = Splitter.on('/').split(mimeType);
    this.type = MediaType.getByValue(Iterables.getFirst(iterable, ""));
    this.subType = Iterables.getLast(iterable, "");
}

From source file:io.airlift.discovery.client.testing.StaticServiceSelector.java

public StaticServiceSelector(Iterable<ServiceDescriptor> serviceDescriptors) {
    Preconditions.checkNotNull(serviceDescriptors, "serviceDescriptors is null");

    ServiceDescriptor serviceDescriptor = Iterables.getFirst(serviceDescriptors, null);
    if (serviceDescriptor != null) {
        this.type = serviceDescriptor.getType();
        this.pool = serviceDescriptor.getPool();
    } else {//from   w w  w.j  a  v  a  2s. c o  m
        this.type = "unknown";
        this.pool = DEFAULT_POOL;
    }

    for (ServiceDescriptor descriptor : serviceDescriptors) {
        Preconditions.checkArgument(descriptor.getType().equals(type));
        Preconditions.checkArgument(descriptor.getPool().equals(pool));
    }
    this.serviceDescriptors = ImmutableList.copyOf(serviceDescriptors);
}

From source file:tds.student.performance.utils.InitializeSegmentsHelper.java

public static Integer maximumSegmentPosition(List<OpportunitySegmentInsert> segmentList) {

    OpportunitySegmentInsert maximumPosition = Iterables.getFirst(Ordering.natural().nullsLast().reverse()
            .onResultOf(new Function<OpportunitySegmentInsert, Integer>() {
                public Integer apply(OpportunitySegmentInsert segment) {
                    return segment.getSegmentPosition();
                }//www  .jav a2  s .  c om
            }).sortedCopy(segmentList), null);

    return maximumPosition == null ? null : maximumPosition.getSegmentPosition();
}

From source file:org.gradle.plugins.ide.internal.generator.XmlPersistableConfigurationObject.java

@Nullable
public static Node findFirstChildNamed(@Nullable Node root, String name) {
    return root == null ? null : Iterables.getFirst(getChildren(root, name), null);
}

From source file:ezbake.frack.submitter.util.ClasspathUtil.java

public static Optional<String> findClassInJar(final File jarFile)
        throws MalformedURLException, ClassNotFoundException {
    log.debug("Looking for class in jar {}", jarFile.getAbsolutePath());
    Optional<String> className = Optional.absent();
    // Get the builder class, have to use a separate classloader
    URLClassLoader contextLoader = new URLClassLoader(new URL[] { jarFile.toURI().toURL() },
            Thread.currentThread().getContextClassLoader());
    final String[] builderClass = new String[] { null };
    Thread finderThread = new Thread(new Runnable() {
        @Override//from w w w . jav  a  2s  . c  o m
        public void run() {
            try {
                URL url = jarFile.toURI().toURL();
                URLClassLoader loader = new URLClassLoader(new URL[] { url });
                final Class pipelineBuilder = Thread.currentThread().getContextClassLoader()
                        .loadClass("ezbake.frack.api.PipelineBuilder");

                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.setUrls(jarFile.toURI().toURL());
                builder.addClassLoader(loader);
                Reflections reflections = new Reflections(builder);
                Set<Class<?>> types = reflections.getSubTypesOf(pipelineBuilder);
                if (types.size() > 0) {
                    Class<?> type = Iterables.getFirst(types, null);
                    if (type != null) {
                        builderClass[0] = type.getCanonicalName();
                    }
                }
            } catch (MalformedURLException e) {
                log.error("Could not access jar file", e);
            } catch (ClassNotFoundException e) {
                log.error(
                        "Could not find builder class in provided jar file, ensure that the pipeline project is dependent on 'frack'");
            }
        }
    });
    finderThread.setContextClassLoader(contextLoader);
    finderThread.start();
    try {
        finderThread.join();
        if (builderClass[0] != null) {
            className = Optional.of(builderClass[0]);
        }
    } catch (InterruptedException e) {
        log.error(String.format("Thread interrupted while attempting to find PipelineBuilder class in %s",
                jarFile.getName()), e);
    }

    return className;
}

From source file:org.eclipse.xtext.parsetree.reconstr.impl.DefaultHiddenTokenHelper.java

@Inject
private void setGrammar(RuleNames names) {
    wsRule = Iterables.getFirst(names.getRulesBySimpleName("WS"), null);
}

From source file:net.sourceforge.fenixedu.domain.cms.UnitAcronymSiteTemplateController.java

private Unit getAssemblyUnit() {
    return Iterables.getFirst(Unit.readUnitsByAcronym(unitAcronym, true), null);
}

From source file:org.jclouds.cloudstack.functions.ParseIdToNameEntryFromHttpResponse.java

public Map.Entry<String, String> apply(HttpResponse response) {
    checkNotNull(response, "response");
    Map<String, String> toParse = parser.apply(response);
    checkNotNull(toParse, "parsed result from %s", response);
    return Iterables.getFirst(toParse.entrySet(), null);
}

From source file:see.util.Reduce.java

/**
 * Fold a sequence without an initial value.
 *
 * @param values sequence to fold/*from   w ww . j ava2  s  .co m*/
 * @param func reducing function
 * @param <T> input and result type
 * @return reduce result
 */
public static <T> T reduce(Iterable<? extends T> values, FoldFunction<? super T, T> func) {
    Preconditions.checkArgument(!Iterables.isEmpty(values), "Cannot reduce empty list");

    return fold(Iterables.getFirst(values, null), Iterables.skip(values, 1), func);
}