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

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

Introduction

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

Prototype

public static <T> T find(Iterable<T> iterable, Predicate<? super T> predicate) 

Source Link

Document

Returns the first element in iterable that satisfies the given predicate; use this method only when such an element is known to exist.

Usage

From source file:bear.plugins.sh.SystemSession.java

protected OSInfo computeUnixFlavour() {

    //        versionScheme.parseVersion();

    try {/* ww  w . j a va  2 s .c  o m*/
        final String text = capture("cat /etc/issue");

        if (text == null)
            return null;

        UnixFlavour flavour = null;
        UnixSubFlavour subFlavour = null;
        org.eclipse.aether.version.Version version = null;

        if (text.contains("CentOS")) {
            flavour = UnixFlavour.CENTOS;
            subFlavour = UnixSubFlavour.CENTOS;

            String versionLine = Iterables.find(LINE_SPLITTER.split(text), Predicates2.contains("CentOS"));
            Matcher matcher = Pattern.compile("release\\s+([^\\s]+)").matcher(versionLine);

            if (!matcher.find()) {
                throw new ValidationException("could not parse OS version: " + versionLine);
            }

            version = Versions.VERSION_SCHEME.parseVersion(matcher.group(1));
        } else if (text.contains("Ubuntu")) {
            flavour = UnixFlavour.UBUNTU;
            subFlavour = UnixSubFlavour.UBUNTU;

            Matcher matcher = Pattern.compile("Ubuntu\\s+(\\d+\\.\\d+)").matcher(text);

            if (!matcher.find()) {
                throw new ValidationException("could not parse OS version: " + text);
            }

            version = Versions.VERSION_SCHEME.parseVersion(matcher.group(1));
        } else {
            throw new UnsupportedOperationException("todo support: " + text);
        }

        return new OSInfo(flavour, subFlavour, version);
    } catch (InvalidVersionSpecificationException e) {
        throw Exceptions.runtime(e);
    }
}