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:org.jclouds.location.suppliers.implicit.OnlyLocationOrFirstRegionOptionallyMatchingRegionId.java

@Override
@Singleton/*w  w w .  j av  a2s.com*/
public Location get() {
    String region = regionSupplier.get();
    Predicate<Location> locationPredicate = region == null ? Predicates.<Location>or(isZone(), isRegion())
            : isZoneOrRegionWhereRegionIdEquals(region);
    Set<? extends Location> locations = locationsSupplier.get();
    if (locationsSupplier.get().size() == 1)
        return getOnlyElement(locationsSupplier.get());
    try {
        Location toReturn = Iterables.find(locations, locationPredicate);
        return toReturn.getScope() == LocationScope.REGION ? toReturn : toReturn.getParent();
    } catch (NoSuchElementException e) {
        throw new NoSuchElementException(String.format("couldn't find region matching %s in %s",
                locationPredicate, transform(locations, ToIdAndScope.INSTANCE)));
    }
}

From source file:org.robotninjas.barge.jaxrs.HttpClusterConfig.java

@Override
public Replica getReplica(String info) {
    try {/*from www.  j  a  v  a2 s .  co m*/
        URI uri = new URI(info);

        if (local.match(uri))
            return local;

        return Iterables.find(remote(), match(uri));
    } catch (URISyntaxException e) {
        throw new RaftHttpException(info + " is not a valid URI, cannot find a corresponding replica", e);
    }
}

From source file:org.jclouds.aws.s3.blobstore.functions.BucketToResourceMetadata.java

private Location getLocation(BucketMetadata from) {
    try {/*from   w w  w .  jav a2 s  .  c o  m*/
        Set<? extends Location> locations = this.locations.get();
        final String region = client.getBucketLocation(from.getName());
        assert region != null : String.format("could not get region for %s", from.getName());
        if (region != null) {
            try {
                return Iterables.find(locations, new Predicate<Location>() {

                    @Override
                    public boolean apply(Location input) {
                        return input.getId().equalsIgnoreCase(region.toString());
                    }

                });
            } catch (NoSuchElementException e) {
                logger.error("could not get location for region %s in %s", region, locations);
            }
        } else {
            logger.error("could not get region for %s", from.getName());
        }
    } catch (ContainerNotFoundException e) {
        logger.error(e, "could not get region for %s, as service suggests the bucket doesn't exist",
                from.getName());
    }
    return null;
}

From source file:org.jclouds.trmk.vcloud_0_8.suppliers.OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault.java

public ReferenceType defaultReferenceType(Iterable<ReferenceType> referenceTypes) {
    return Iterables.find(referenceTypes, defaultSelector);
}

From source file:hudson.plugins.libvirt.PluginImpl.java

public Hypervisor getServer(final String host) {

    return Iterables.find(getServers(), new Predicate<Hypervisor>() {
        public boolean apply(@Nullable Hypervisor input) {
            return host.equals(input.getHypervisorHost());
        }//from   w  ww. j  ava 2  s.co m
    });
}

From source file:org.jclouds.ec2.compute.functions.EC2SecurityGroupToSecurityGroup.java

private Location findLocationWithId(final String locationId) {
    if (locationId == null)
        return null;
    try {/*from   ww  w .j  a  va  2s.c o  m*/
        Location location = Iterables.find(locations.get(), new Predicate<Location>() {

            @Override
            public boolean apply(Location input) {
                return input.getId().equals(locationId);
            }

        });
        return location;

    } catch (NoSuchElementException e) {
        logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
        return null;
    }
}

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

@Override
public Image apply(VAppTemplate from) {
    checkNotNull(from, "VAppTemplate");
    Envelope ovf = templateToEnvelope.apply(from);

    ImageBuilder builder = new ImageBuilder();
    builder.ids(from.getHref().toASCIIString());
    builder.uri(from.getHref());/*from  w ww .jav  a 2s .  co  m*/
    builder.name(from.getName());
    Link vdc = Iterables.find(checkNotNull(from, "from").getLinks(),
            LinkPredicates.typeEquals(VCloudDirectorMediaType.VDC));
    if (vdc != null) {
        builder.location(findLocationForResource.apply(vdc));
    } else {
        // otherwise, it could be in a public catalog, which is not assigned to a VDC
    }
    builder.description(from.getDescription() != null ? from.getDescription() : from.getName());
    builder.operatingSystem(CIMOperatingSystem.toComputeOs(ovf));
    builder.status(toPortableImageStatus.get(from.getStatus()));
    return builder.build();
}

From source file:org.jclouds.aws.ec2.EC2ContextBuilder.java

@Override
public Injector buildInjector() {
    try {/*from w w w .j  ava  2  s .co  m*/
        Iterables.find(modules, Predicates.instanceOf(ConfigureELBModule.class));
    } catch (NoSuchElementException e) {
        Iterable<Module> infra = Iterables.filter(modules, new Predicate<Module>() {
            public boolean apply(Module input) {
                return input.getClass().isAnnotationPresent(ConfiguresExecutorService.class)
                        || input.getClass().isAnnotationPresent(ConfiguresHttpCommandExecutorService.class)
                        || instanceOf(LoggingModule.class).apply(input);
            }

        });
        modules.add(new ConfigureELBModule(infra, properties));
    }
    return super.buildInjector();
}

From source file:com.isotrol.impe3.pms.core.obj.RoutingDomainsObject.java

/**
 * Builds a collection from a set of entities.
 * @param domains Routing domains.//  ww w  . j  a  v a2 s . c  om
 * @return The requested collection.
 */
public static RoutingDomainsObject of(Iterable<RoutingDomainEntity> domains) {
    try {
        final RoutingDomainEntity drd = Iterables.find(domains, ENTITY_DEFAULT);
        final RoutingBase offline = RoutingBase.of(drd.getOfflineBase(), drd.getOfflineAbsBase());
        return new RoutingDomainsObject(offline, domains);
    } catch (NoSuchElementException e) {
        throw new IllegalArgumentException("No default routing domain");
    }
}

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

@Override
public Hardware apply(Vm from) {
    checkNotNull(from, "VApp");
    // TODO make this work with composite vApps
    if (from == null)
        return null;

    VirtualHardwareSection hardware = findVirtualHardwareSectionForVm.apply(from);
    HardwareBuilder builder = rasdToHardwareBuilder.apply(hardware.getItems());
    builder.location(findLocationForResource.apply(Iterables.find(checkNotNull(from, "from").getLinks(),
            LinkPredicates.typeEquals(VCloudDirectorMediaType.VDC))));
    builder.ids(from.getHref().toASCIIString()).name(from.getName())
            .supportsImage(ImagePredicates.idEquals(from.getHref().toASCIIString()));
    builder.hypervisor("VMware");
    return builder.build();
}