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

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

Introduction

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

Prototype

public static <T> T get(Iterable<T> iterable, int position) 

Source Link

Document

Returns the element at the specified position in an iterable.

Usage

From source file:org.apache.brooklyn.core.location.Machines.java

public static Maybe<String> getSubnetIp(Location where) {
    // TODO Too much duplication between the ip and hostname methods
    String result = null;/*from w w w  .j ava2  s  . com*/
    if (where instanceof HasSubnetHostname) {
        result = ((HasSubnetHostname) where).getSubnetIp();
    }
    if (where instanceof HasNetworkAddresses) {
        Set<String> privateAddrs = ((HasNetworkAddresses) where).getPrivateAddresses();
        if (privateAddrs.size() > 0) {
            result = Iterables.get(privateAddrs, 0);
        }
    }
    if (result == null && where instanceof MachineLocation) {
        InetAddress addr = ((MachineLocation) where).getAddress();
        if (addr != null)
            result = addr.getHostAddress();
    }
    log.debug("computed subnet host ip {} for {}", result, where);
    return Maybe.fromNullable(result);
}

From source file:org.jclouds.aws.s3.functions.BindRegionToXmlPayload.java

@Override
public void bindToRequest(HttpRequest request, Object input) {
    input = input == null ? Iterables.get(defaultRegions, 0) : input;
    checkArgument(input instanceof String, "this binder is only valid for Region!");
    String constraint = (String) input;
    String value = null;//from  ww  w. j a v a 2  s.  c  o m
    if (Iterables.contains(defaultRegions, constraint)) {
        // nothing to bind as this is default.
        return;
    } else if (Iterables.contains(regions, constraint)) {
        value = constraint;
    } else {
        if (constraint.equals(Region.EU_WEST_1)) {
            value = "EU";
        } else {
            logger.warn("region %s not in %s ", constraint, regions);
            value = constraint;
        }
    }
    String payload = String.format(
            "<CreateBucketConfiguration><LocationConstraint>%s</LocationConstraint></CreateBucketConfiguration>",
            value);
    super.bindToRequest(request, payload);
    request.getPayload().getContentMetadata().setContentType(MediaType.TEXT_XML);
}

From source file:org.jclouds.rimuhosting.miro.functions.ParseRimuHostingException.java

@Override
public Object apply(Exception e) {
    if (e instanceof HttpResponseException) {
        HttpResponseException responseException = (HttpResponseException) e;
        if (responseException.getContent() != null) {
            Type setType = new TypeToken<Map<String, RimuHostingResponse>>() {
            }.getType();//  w  w  w.  ja  v  a 2s .c o m
            String test = responseException.getContent();
            Map<String, RimuHostingResponse> responseMap = json.fromJson(test, setType);
            RimuHostingResponse firstResponse = Iterables.get(responseMap.values(), 0);
            String errorClass = firstResponse.getErrorInfo().getErrorClass();
            if (errorClass.equals("PermissionException"))
                throw new AuthorizationException(firstResponse.getErrorInfo().getErrorMessage(),
                        responseException);
            throw new RuntimeException(firstResponse.getErrorInfo().getErrorMessage(), e);
        }
    }
    return propagateOrNull(e);
}

From source file:edu.harvard.med.iccbl.screensaver.policy.DataSharingLevelMapper.java

public static ScreensaverUserRole getUserDslRoleForScreenTypeAndLevel(ScreenType screenType, int level) {
    SortedSet<ScreensaverUserRole> roles = UserDslRoles.get(screenType);
    ScreensaverUserRole role = Iterables.get(roles, roles.size() - level);
    assert (role.getRoleName().contains(Integer.toString(level)));
    return role;//  ww w  . j a  v  a2 s  .co  m
}

From source file:org.icgc.dcc.release.job.summarize.function.CreateFeatureTypeSummary.java

private static String resolveTypeName(String key) {
    val parts = Splitters.HASHTAG.split(key);

    return Iterables.get(parts, 1);
}

From source file:org.graylog.plugins.pipelineprocessor.ast.expressions.IndexedAccessExpression.java

@Override
public Object evaluateUnsafe(EvaluationContext context) {
    final Object idxObj = this.index.evaluateUnsafe(context);
    final Object indexable = indexableObject.evaluateUnsafe(context);
    if (idxObj == null || indexable == null) {
        return null;
    }/*from   ww  w  .j av a2s.co m*/

    if (idxObj instanceof Long) {
        int idx = Ints.saturatedCast((long) idxObj);
        if (indexable.getClass().isArray()) {
            return Array.get(indexable, idx);
        } else if (indexable instanceof List) {
            return ((List) indexable).get(idx);
        } else if (indexable instanceof Iterable) {
            return Iterables.get((Iterable) indexable, idx);
        }
        throw new IllegalArgumentException("Object '" + indexable + "' is not an Array, List or Iterable.");
    } else if (idxObj instanceof String) {
        final String idx = idxObj.toString();
        if (indexable instanceof Map) {
            return ((Map) indexable).get(idx);
        }
        throw new IllegalArgumentException("Object '" + indexable + "' is not a Map.");
    }
    throw new IllegalArgumentException("Index '" + idxObj + "' is not a Long or String.");
}

From source file:org.jclouds.trmk.vcloud_0_8.functions.OrgNameCatalogNameItemNameToEndpoint.java

@SuppressWarnings("unchecked")
public URI apply(Object from) {
    Iterable<Object> orgCatalog = (Iterable<Object>) checkNotNull(from, "args");
    Object org = Iterables.get(orgCatalog, 0);
    Object catalog = Iterables.get(orgCatalog, 1);
    Object catalogItem = Iterables.get(orgCatalog, 2);
    if (org == null)
        org = defaultOrg.get().getName();
    if (catalog == null)
        catalog = defaultCatalog.get().getName();
    try {/*from   w w w . j  av  a 2 s. c o  m*/
        Map<String, ? extends org.jclouds.trmk.vcloud_0_8.domain.Catalog> catalogs = checkNotNull(
                orgCatalogMap.get().get(org));
        return catalogs.get(catalog).get(catalogItem).getHref();
    } catch (NullPointerException e) {
        throw new NoSuchElementException(
                org + "/" + catalog + "/" + catalogItem + " not found in " + orgCatalogMap.get());
    }
}

From source file:org.jclouds.vcloud.director.v1_5.functions.OrgNameAndCatalogNameToEndpoint.java

@SuppressWarnings("unchecked")
public URI apply(Object from) {
    Iterable<Object> orgCatalog = (Iterable<Object>) checkNotNull(from, "args");
    Object org = Iterables.get(orgCatalog, 0);
    Object catalog = Iterables.get(orgCatalog, 1);
    if (org == null && catalog == null)
        return defaultCatalog.get().getHref();
    else if (org == null)
        org = defaultOrg.get().getName();

    try {/*w w w.  j  a  v a2s .  com*/
        Set<Reference> catalogs = checkNotNull(orgMap.get().get(org)).getCatalogs();
        return catalog == null ? Iterables.getLast(catalogs).getHref()
                : Iterables.find(catalogs, nameEquals((String) catalog)).getHref();
    } catch (NullPointerException e) {
        throw new NoSuchElementException(org + "/" + catalog + " not found in " + orgMap.get());
    }
}

From source file:org.nuxeo.ecm.core.query.sql.model.SelectClause.java

public Operand get(int i) {
    return Iterables.get(elements.values(), i);
}

From source file:org.richfaces.request.UploadedFile25.java

public String getHeader(String headerName) {
    String lcHeaderName = headerName.toLowerCase(Locale.US);
    Collection<String> headers = headersMap.get(lcHeaderName);

    if (headers.isEmpty()) {
        return null;
    }// w w w. j  a  v  a 2s .co  m

    return Iterables.get(headers, 0);
}