List of usage examples for com.google.common.collect Iterables tryFind
public static <T> Optional<T> tryFind(Iterable<T> iterable, Predicate<? super T> predicate)
From source file:org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase.java
protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) { final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements, Predicates.instanceOf(type)); return possible.isPresent() ? type.cast(possible.get()) : null; }
From source file:org.killbill.billing.beatrix.util.InvoiceChecker.java
public void checkInvoiceNoAudits(final Invoice invoice, final CallContext context, final List<ExpectedInvoiceItemCheck> expected) throws InvoiceApiException { final List<InvoiceItem> actual = invoice.getInvoiceItems(); Assert.assertEquals(actual.size(), expected.size(), String.format("Expected items: %s, actual items: %s", expected, actual)); for (final ExpectedInvoiceItemCheck cur : expected) { boolean found = false; // First try to find exact match; this is necessary because the for loop below might encounter a similar item -- for instance // same type, same dates, but different amount and choke. final InvoiceItem foundItem = Iterables.tryFind(actual, new Predicate<InvoiceItem>() { @Override//from w w w. j ava2s .co m public boolean apply(final InvoiceItem input) { if (input.getInvoiceItemType() != cur.getType() || (cur.shouldCheckDates() && input.getStartDate().compareTo(cur.getStartDate()) != 0)) { return false; } if (input.getAmount().compareTo(cur.getAmount()) != 0) { return false; } if (!cur.shouldCheckDates() || (cur.getEndDate() == null && input.getEndDate() == null) || (cur.getEndDate() != null && input.getEndDate() != null && cur.getEndDate().compareTo(input.getEndDate()) == 0)) { return true; } return false; } }).orNull(); if (foundItem != null) { continue; } // If we could not find it, we still loop again, so that error message helps to debug when there is a 'similar' item. for (final InvoiceItem in : actual) { // Match first on type and start date if (in.getInvoiceItemType() != cur.getType() || (cur.shouldCheckDates() && in.getStartDate().compareTo(cur.getStartDate()) != 0)) { continue; } if (in.getAmount().compareTo(cur.getAmount()) != 0) { log.info(String.format( "Found item type = %s and startDate = %s but amount differ (actual = %s, expected = %s) ", cur.getType(), cur.getStartDate(), in.getAmount(), cur.getAmount())); continue; } if (!cur.shouldCheckDates() || (cur.getEndDate() == null && in.getEndDate() == null) || (cur.getEndDate() != null && in.getEndDate() != null && cur.getEndDate().compareTo(in.getEndDate()) == 0)) { found = true; break; } log.info(String.format( "Found item type = %s and startDate = %s, amount = %s but endDate differ (actual = %s, expected = %s) ", cur.getType(), cur.getStartDate(), in.getAmount(), in.getEndDate(), cur.getEndDate())); } if (!found) { final StringBuilder debugBuilder = new StringBuilder(); debugBuilder.append(String.format("Invoice id=[%s], targetDate=[%s]", invoice.getId(), invoice.getTargetDate())); for (final InvoiceItem actualInvoiceItem : actual) { debugBuilder.append(String.format("\n type=[%s] startDate=[%s] endDate=[%s] amount=[%s]", actualInvoiceItem.getInvoiceItemType(), actualInvoiceItem.getStartDate(), actualInvoiceItem.getEndDate(), actualInvoiceItem.getAmount())); } final String failureMessage = String.format( "Failed to find invoice item type = %s and startDate = %s, amount = %s, endDate = %s for invoice id %s\n%s", cur.getType(), cur.getStartDate(), cur.getAmount(), cur.getEndDate(), invoice.getId(), debugBuilder.toString()); Assert.fail(failureMessage); } } }
From source file:domainapp.dom.menu.Menu.java
@Programmatic public MenuItem findItem(final String menuItemName) { final Optional<MenuItem> menuItemIfAny = Iterables.tryFind(getItems(), menuItem -> Objects.equal(menuItem.getName(), menuItemName)); return menuItemIfAny.orNull(); }
From source file:gg.uhc.uhc.modules.ModuleRegistry.java
public <T extends Module> Optional<T> get(Class<T> klass) { return (Optional<T>) Iterables.tryFind(modules.values(), Predicates.instanceOf(klass)); }
From source file:com.thinkbiganalytics.metadata.sla.DefaultServiceLevelAgreementScheduler.java
private String getUniqueName(String name) { String uniqueName = name;//from ww w. j a v a 2 s .c om final String checkName = name; String matchingName = Iterables.tryFind(scheduledJobNames.values(), new Predicate<String>() { @Override public boolean apply(String s) { return s.equalsIgnoreCase(checkName); } }).orNull(); if (matchingName != null) { //get numeric string after '-'; if (StringUtils.contains(matchingName, "-")) { String number = StringUtils.substringAfterLast(matchingName, "-"); if (StringUtils.isNotBlank(number)) { number = StringUtils.trim(number); if (StringUtils.isNumeric(number)) { Integer num = Integer.parseInt(number); num++; uniqueName += "-" + num; } else { uniqueName += "-1"; } } } else { uniqueName += "-1"; } } return uniqueName; }
From source file:com.thinkbiganalytics.feedmgr.service.category.InMemoryFeedManagerCategoryService.java
@Override public FeedCategory getCategoryBySystemName(final String name) { return Iterables.tryFind(categories.values(), new Predicate<FeedCategory>() { @Override//from w w w .ja v a 2 s . c o m public boolean apply(FeedCategory feedCategory) { return feedCategory.getSystemName().equalsIgnoreCase(name); } }).orNull(); }
From source file:org.killbill.billing.catalog.DefaultUsage.java
public DefaultUsage(final Usage in, UsagePriceOverride override, Currency currency) { this.name = in.getName(); this.usageType = in.getUsageType(); this.tierBlockPolicy = in.getTierBlockPolicy(); this.billingPeriod = in.getBillingPeriod(); this.billingMode = in.getBillingMode(); this.limits = (DefaultLimit[]) in.getLimits(); this.blocks = (DefaultBlock[]) in.getBlocks(); this.tiers = new DefaultTier[in.getTiers().length]; for (int i = 0; i < in.getTiers().length; i++) { if (override != null && override.getTierPriceOverrides() != null) { final TieredBlock[] curTieredBlocks = in.getTiers()[i].getTieredBlocks(); final TierPriceOverride overriddenTier = Iterables .tryFind(override.getTierPriceOverrides(), new Predicate<TierPriceOverride>() { @Override public boolean apply(final TierPriceOverride input) { if (input != null) { final List<TieredBlockPriceOverride> blockPriceOverrides = input .getTieredBlockPriceOverrides(); for (TieredBlockPriceOverride blockDef : blockPriceOverrides) { String unitName = blockDef.getUnitName(); Double max = blockDef.getMax(); Double size = blockDef.getSize(); for (TieredBlock curTieredBlock : curTieredBlocks) { if (unitName.equals(curTieredBlock.getUnit().getName()) && Double.compare(size, curTieredBlock.getSize()) == 0 && Double.compare(max, curTieredBlock.getMax()) == 0) { return true; } }/*from w w w .ja v a 2 s. com*/ } } return false; } }).orNull(); tiers[i] = (overriddenTier != null) ? new DefaultTier(in.getTiers()[i], overriddenTier, currency) : (DefaultTier) in.getTiers()[i]; } else { tiers[i] = (DefaultTier) in.getTiers()[i]; } } }
From source file:org.ow2.petals.cloud.manager.core.actions.PuppetRunAction.java
/** * Search the puppet script property like {name:puppet-destination, type:file, value:XXX}. If not found, generate one. * * @param node//ww w . j av a 2 s . c om * @param context * @return * @throws CloudManagerException */ @Override protected String getRemoteScriptPath(Node node, Context context) throws CloudManagerException { return Iterables.tryFind(node.getProperties(), new Predicate<Property>() { public boolean apply(org.ow2.petals.cloud.manager.api.deployment.Property input) { return input.getType().equalsIgnoreCase(Constants.FILE_TYPE) && input.getValue().endsWith(".pp") && input.getName().equalsIgnoreCase(Constants.PUPPET_DESTINATION); } }).or(getDefaultPuppetDestination()).getValue(); }
From source file:org.icgc.dcc.submission.dictionary.model.FileSchema.java
public Optional<Field> field(final String name) { return Iterables.tryFind(fields, new Predicate<Field>() { @Override//from ww w . j a v a 2 s. co m public boolean apply(Field input) { return input.getName().equals(name); } }); }
From source file:clocker.docker.location.strategy.basic.GroupPlacementStrategy.java
@Override public List<DockerHostLocation> filterLocations(List<DockerHostLocation> locations, Entity entity) { if (locations == null || locations.isEmpty()) { return ImmutableList.of(); }//from ww w . j a v a2 s . co m if (getDockerInfrastructure() == null) config().set(DOCKER_INFRASTRUCTURE, Iterables.getLast(locations).getDockerInfrastructure()); List<DockerHostLocation> available = MutableList.copyOf(locations); boolean requireExclusive = config().get(REQUIRE_EXCLUSIVE); try { acquireMutex(entity.getApplicationId(), "Filtering locations for " + entity); } catch (InterruptedException ie) { Exceptions.propagate(ie); // Should never happen... } // Find hosts with entities from our application deployed there Iterable<DockerHostLocation> sameApplication = Iterables.filter(available, hasApplicationId(entity.getApplicationId())); // Check if hosts have any deployed entities that share a parent with the input entity Optional<DockerHostLocation> sameParent = Iterables.tryFind(sameApplication, childrenOf(entity.getParent())); if (sameParent.isPresent()) { LOG.debug("Returning {} (same parent) for {} placement", sameParent.get(), entity); return ImmutableList.copyOf(sameParent.asSet()); } // Remove hosts if they have any entities from our application deployed there Iterables.removeIf(available, hasApplicationId(entity.getApplicationId())); if (requireExclusive) { Iterables.removeIf(available, nonEmpty()); } LOG.debug("Returning {} for {} placement", Iterables.toString(available), entity); return available; }