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.bennavetta.util.tycho.impl.DefaultBundleGenerator.java

@Override
public String getSymbolicName(Artifact artifact) {
    if (artifact.getGroupId().indexOf('.') == -1) {
        // Find the first package with classes in it
        try (JarFile jar = new JarFile(artifact.getFile())) // commons-logging:commons-logging -> org.apache.commons.logging
        {/*from   ww  w . ja  va  2 s  .com*/
            List<String> contents = new ArrayList<>();
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                contents.add(entry.getName());
            }
            // sort by number of slashes
            Collections.sort(contents, PATH_COMPONENTS);
            for (String path : contents) {
                if (path.endsWith(".class")) {
                    path = path.substring(0, path.lastIndexOf('/')).replace('/', '.');
                    if (path.startsWith("/")) {
                        path = path.substring(1);
                    }
                    return path;
                }
            }
        } catch (IOException e) {
            return null;
        }
    } else if (Iterables.getLast(Splitter.on('.').split(artifact.getGroupId()))
            .equals(artifact.getArtifactId())) {
        return artifact.getGroupId(); // org.apache.maven:maven -> org.apache.maven
    } else {
        String gidEnd = Iterables.getLast(Splitter.on('.').split(artifact.getGroupId()));
        if (Iterables.getFirst(Splitter.on('.').split(artifact.getArtifactId()), null).equals(gidEnd)) {
            // org.apache.maven:maven-core -> org.apache.maven.core
            return artifact.getGroupId() + "."
                    + PUNCTUATION.trimFrom(artifact.getArtifactId().substring(gidEnd.length()));
        } else {
            return artifact.getGroupId() + "." + artifact.getArtifactId(); // groupId + "." + artifactId
        }
    }
    return null;
}

From source file:org.obm.servlet.filter.qos.handlers.KeyRequestsInfo.java

public QoSContinuation nextContinuation() {
    return Iterables.getFirst(continuations, null);
}

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

private static URI getSrcUri(Elements elements) {
    final Element first = Iterables.getFirst(elements, null);
    if (null == first) {
        return null;
    }// w ww. j a v  a2s.c  om
    String srcString = first.attr("src");
    if (null == srcString) {
        return null;
    }
    try {
        for (Map.Entry<String, String> replacement : URL_REPLACE.entrySet()) {
            srcString = srcString.replace(replacement.getKey(), replacement.getValue());
        }
        return new URI(srcString);
    } catch (URISyntaxException ex) {
        Logger.getLogger(HtmlTagLoader.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:org.obm.service.user.UserServiceImpl.java

private boolean isValidCalendarString(String calendar) {
    String username = Iterables.getFirst(Splitter.on('@').omitEmptyStrings().split(calendar), null);
    return (username != null);
}

From source file:uk.ac.cam.cl.dtg.picky.parser.deviceanalyser.DeviceAnalyserEntryParser.java

@Override
public Attributes getFileAttributes() {
    Attributes attributes = new Attributes();

    List<String> daysSorted = days.stream().filter(s -> s.matches("\\d{4}-\\d{2}-\\d{2}")).sorted()
            .collect(Collectors.toList());

    attributes.put(ATTRIBUTE_DAYS, daysSorted.size());

    if (!daysSorted.isEmpty()) {
        attributes.put(ATTRIBUTE_FIRST_DAY, Iterables.getFirst(daysSorted, null));
        attributes.put(ATTRIBUTE_LAST_DAY, Iterables.getLast(daysSorted, null));
    }//from   w  w  w . ja v a2 s .  co  m

    DeviceInfo deviceInfo = deviceInfoParser.getDeviceInfo();
    if (deviceInfo.apiversion != null)
        attributes.put(ATTRIBUTE_APIVERSION, deviceInfo.apiversion);
    if (deviceInfo.device != null)
        attributes.put(ATTRIBUTE_DEVICE, deviceInfo.device);
    if (deviceInfo.devicemodel != null)
        attributes.put(ATTRIBUTE_DEVICEMODEL, deviceInfo.devicemodel);
    if (deviceInfo.dpi != null)
        attributes.put(ATTRIBUTE_DPI, deviceInfo.dpi);
    if (deviceInfo.locale != null)
        attributes.put(ATTRIBUTE_LOCALE, deviceInfo.locale);
    if (deviceInfo.getCountry() != null)
        attributes.put(ATTRIBUTE_LOCALE_COUNTRY, deviceInfo.getCountry());
    if (deviceInfo.manufacturer != null)
        attributes.put(ATTRIBUTE_MANUFACTURER, deviceInfo.manufacturer);
    if (deviceInfo.osbuildtype != null)
        attributes.put(ATTRIBUTE_OSBUILDTYPE, deviceInfo.osbuildtype);
    if (deviceInfo.osstring != null)
        attributes.put(ATTRIBUTE_OSSTRING, deviceInfo.osstring);
    if (deviceInfo.resolution != null)
        attributes.put(ATTRIBUTE_RESOLUTION, deviceInfo.resolution);
    attributes.put(ATTRIBUTE_ROOTED, deviceInfo.getRooted());

    return attributes;
}

From source file:edu.sdsc.scigraph.owlapi.ReasonerUtil.java

boolean shouldReason() {
    if (!reasoner.isConsistent()) {
        logger.warning("Not reasoning on " + ont + " because it is inconsistent.");
        return false;
    }/*  w w  w  .j a  v a2 s  . c  o  m*/
    Collection<OWLClass> unsatisfiableClasses = getUnsatisfiableClasses();
    if (!unsatisfiableClasses.isEmpty()) {
        logger.warning("Not reasoning on " + ont + " because " + unsatisfiableClasses.size()
                + " classes are unsatisfiable");
        logger.warning("For instance: " + Iterables.getFirst(unsatisfiableClasses, null).getIRI().toString()
                + " unsatisfiable");
        return false;
    }
    return true;
}

From source file:eu.trentorise.opendata.disiclient.UrlMapper.java

/**
 * Extracts from multimap the first item correspond to {@code property}
 *
 * @throws IllegalArgumentException if there is not exactly one {
 * @property}//  w w  w  .  j  a  v  a 2  s. co m
 */
private String getFirst(Multimap<String, String> m, String property) {
    int sz = m.get(property).size();
    if (sz != 1) {
        throw new IllegalArgumentException("Expected one " + property + ", found " + sz + " instead.");
    } else {
        return Iterables.getFirst(m.get(property), "");
    }
}

From source file:com.google.gitiles.FakeHttpServletRequest.java

@Override
public String getParameter(String name) {
    return Iterables.getFirst(parameters.get(name), null);
}

From source file:org.eclipse.incquery.patternlanguage.emf.specification.builder.NameToSpecificationMap.java

/**
 * Returns a specification with the selected status
 * @param status//from w  ww  .  j  a va2  s  .c  o  m
 * @return a specification with the selected status, or null if no such specification is available
 */
public IQuerySpecification<?> getSpecificationWithStatus(final PQueryStatus status) {
    return Iterables.getFirst(getSpecificationsWithStatus(status), null);
}

From source file:com.davidbracewell.conversion.impl.MapConverter.java

@Override
public T apply(Object obj) {
    if (obj == null) {
        return null;
    }//www .ja v  a2 s. com
    T map = mapSupplier.get();

    if (obj instanceof Map) {
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) obj).entrySet()) {
            map.put(keyConverter.apply(entry.getKey()), valueConverter.apply(entry.getValue()));
        }
        return map;
    }

    if (obj instanceof CharSequence) {
        return Cast.as(MapUtils.fromString(obj.toString(), keyConverter, valueConverter));
    } else if (obj.getClass().isArray()
            && Map.Entry.class.isAssignableFrom(obj.getClass().getComponentType())) {
        for (Object o : Convert.convert(obj, Iterable.class)) {
            Map.Entry<?, ?> e = Cast.as(o);
            map.put(keyConverter.apply(e.getKey()), valueConverter.apply(e.getValue()));
        }
        return map;
    } else if (obj instanceof Map.Entry) {
        Map.Entry<?, ?> e = Cast.as(obj);
        map.put(keyConverter.apply(e.getKey()), valueConverter.apply(e.getValue()));
        return map;
    } else if (obj instanceof Iterable) {
        Object o = Iterables.getFirst(Cast.as(obj, Iterable.class), null);
        if (o != null && o instanceof Map.Entry) {
            for (Object inner : Cast.as(obj, Iterable.class)) {
                Map.Entry<?, ?> e = Cast.as(inner);
                map.put(keyConverter.apply(e.getKey()), valueConverter.apply(e.getValue()));
            }
            return map;
        }
    }

    try {
        return Cast
                .as(MapUtils.fillMap(map, Convert.convert(obj, Iterable.class), keyConverter, valueConverter));
    } catch (Exception e) {
        //ignore
    }

    log.fine("Cannot convert {0} to a Map.", obj.getClass());
    return null;
}