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:me.emily.oauth2.OAuth2Native.java

public String requestAccess(Iterable<String> scopes) {
    GoogleAuthorizationCodeFlow flow = startFlow(scopes);

    String build = flow.newAuthorizationUrl()
            .setRedirectUri(Iterables.getFirst(clientSecrets.getInstalled().getRedirectUris(), "")).build();

    return build;

}

From source file:com.urswolfer.intellij.plugin.gerrit.ui.changesbrowser.CommitMessageFormatter.java

private String getParentLine() {
    Set<String> parents = gitCommit.getParentsHashes();
    if (parents.size() == 1) {
        String parent = Iterables.getFirst(parents, null);
        return String.format(PARENT_PATTERN, parent);
    } else if (parents.size() > 1) {
        String allParents = Joiner.on(MERGE_PATTERN_DELIMITER).join(parents);
        return String.format(MERGE_PATTERN, allParents);
    } else {// w ww .j a  v a2s  .co m
        throw new IllegalArgumentException(
                "Cannot handle commit here: '" + gitCommit.getShortHash().getString() + "'.");
    }
}

From source file:gg.uhc.uhc.messages.BaseMessageTemplates.java

@Override
public Mustache getTemplate(String key) {
    // compile the tempalte from the config value and store it in our map
    if (!templates.containsKey(key)) {
        templates.put(key, templating.compile(new StringReader(getRaw(key)), key));
    }//from w  w  w .j a v  a2  s  .  c  om

    return Iterables.getFirst(templates.get(key), null);
}

From source file:com.netflix.explorers.jersey.ExplorerResource.java

public ExplorerResource(String name, Map<String, Provider<ViewableResource>> controllers,
        Map<RestKey, Provider<RestResource>> restResources) {
    super(name);/*  w  w  w . j  ava 2 s  . co  m*/
    this.controllers = controllers;
    this.restResources = restResources;

    if (!controllers.isEmpty()) {
        LOG.info(String.format("Explorer '%s' initialized with controllers '%s'", name,
                this.controllers.keySet()));
        defaultController = Iterables.getFirst(controllers.keySet(), null);
    }

    if (restResources != null) {
        LOG.info(String.format("Explorer '%s' initialized with r '%s'", name, this.restResources.keySet()));
    }
}

From source file:net.freifunk.autodeploy.device.DeviceServiceImpl.java

@Override
public Device findSupportedDevice(final String deviceString) {
    final Iterable<Device> matches = Iterables.filter(_deployersByDevice.keySet(), new Predicate<Device>() {

        @Override/*from   w ww .ja v a 2 s  . c o m*/
        public boolean apply(final Device device) {
            return device != null && device.asString().equals(deviceString);
        }
    });

    if (Iterables.size(matches) > 1) {
        throw new IllegalStateException("More than one device found: " + deviceString);
    }
    return Iterables.getFirst(matches, null);
}

From source file:com.b2international.snowowl.core.domain.CollectionResource.java

/**
 * @return an {@link Optional} item in this collection resource
 *///from   w  ww  .j  a  v  a 2  s. c o m
@JsonIgnore
public Optional<T> first() {
    return Optional.ofNullable(Iterables.getFirst(getItems(), null));
}

From source file:com.facebook.buck.testutil.Zip.java

/**
 * Open the zip file for reading and/or writing. Note that this constructor will create
 * {@code zip} if {@code forWriting} is true and the file does not exist.
 *
 * @param zip The zip file to operate on. The name must end with ".zip" or ".jar".
 * @param forWriting Whether the zip file should be opened for writing or not.
 * @throws IOException Should something terrible occur.
 */// w  w  w .j  a v  a 2 s.com
public Zip(File zip, boolean forWriting) throws IOException {
    assertTrue("zip name must end with .zip for file type detection to work",
            zip.getName().endsWith(".zip") || zip.getName().endsWith(".jar"));

    URI uri = URI.create("jar:" + zip.toURI());
    fs = FileSystems.newFileSystem(uri, ImmutableMap.of("create", String.valueOf(forWriting)));

    root = Iterables.getFirst(fs.getRootDirectories(), null);
    assertNotNull("Unable to determine root of file system: " + fs, root);
}

From source file:com.zulily.omicron.Utils.java

/**
 * Returns a hostname from the current host
 * <p>//  ww w  . ja  v  a  2s.  c o  m
 * TODO: Platform dependent
 *
 * @return Either the configured host name, or the hostname of the local IP
 */
public static String getHostName() {
    return System.getenv().containsKey("HOSTNAME")
            ? Iterables.getFirst(DOT_SPLITTER.split(System.getenv("HOSTNAME")), getInetHost())
            : getInetHost();
}

From source file:com.gnapse.metric.Property.java

/**
 * Creates a new derived property in the given universe with the specified names and
 * factorization.//from  w w w. j  a va  2s.  c  o  m
 *
 * @param universe the universe where this property is defined
 * @param names the names of this property
 * @param factors the factors that define this property as a derivation of other properties
 * @throws MetricException if any of the property names is a duplicate of a property previously
 *     defined, or if there's a property already defined in the universe with the same dimension
 */
Property(Universe universe, List<String> names, Factorization<Property> factors) throws MetricException {

    boolean isDerived = (factors != null) && !factors.isEmpty() && !factors.isSingleItem();

    checkArgument(names != null && !names.isEmpty());
    this.name = Iterables.getFirst(names, null);
    this.names = Collections.unmodifiableSet(Sets.newLinkedHashSet(names));

    this.universe = checkNotNull(universe);
    this.dimensions = isDerived ? reduceFactors(factors) : Factorization.factor(this, 1);
    universe.registerNewProperty(this);

    if (isDerived) {
        Factorization<Unit> baseUnitFactors = factors.transformItems(getPropertyBaseUnitFn);
        registerNewUnit(new Unit(this, baseUnitFactors, true));
    }
}

From source file:org.jclouds.aws.ec2.functions.ImportOrReturnExistingKeypair.java

@VisibleForTesting
KeyPair importOrReturnExistingKeypair(String region, String group, String publicKeyMaterial) {
    checkNotNull(region, "region");
    checkNotNull(group, "group");
    checkNotNull(publicKeyMaterial, "publicKeyMaterial");
    logger.debug(">> importing keyPair region(%s) group(%s)", region, group);
    KeyPair keyPair = null;/*from w w w  .j  a v a2s.c o  m*/
    // loop for eventual consistency or race condition.
    // as this command is idempotent, it should be ok
    while (keyPair == null)
        try {
            keyPair = ec2Api.getKeyPairApi().get().importKeyPairInRegion(region, "jclouds#" + group,
                    publicKeyMaterial);
            keyPair = addFingerprintToKeyPair(publicKeyMaterial, keyPair);
            logger.debug("<< imported keyPair(%s)", keyPair);
        } catch (IllegalStateException e) {
            keyPair = Iterables.getFirst(
                    ec2Api.getKeyPairApi().get().describeKeyPairsInRegion(region, "jclouds#" + group), null);
            if (keyPair != null) {
                keyPair = addFingerprintToKeyPair(publicKeyMaterial, keyPair);
                logger.debug("<< retrieved existing keyPair(%s)", keyPair);
            }
        }
    return keyPair;
}