Example usage for com.google.common.base Predicates compose

List of usage examples for com.google.common.base Predicates compose

Introduction

In this page you can find the example usage for com.google.common.base Predicates compose.

Prototype

public static <A, B> Predicate<A> compose(Predicate<B> predicate, Function<A, ? extends B> function) 

Source Link

Document

Returns the composition of a function and a predicate.

Usage

From source file:com.eucalyptus.util.CollectionUtils.java

/**
 * Convenience method for a predicate on a property value.
 *
 * @param propertyValue The property value to match
 * @param propertyFunction The function to extract the property
 * @param <T> The predicate type/*from   ww w .  j  a  v a 2  s .co  m*/
 * @param <PT> The property type
 * @return A predicate that extracts a value to compare with the given value.
 */
public static <T, PT> Predicate<T> propertyPredicate(final PT propertyValue,
        final Function<T, PT> propertyFunction) {
    return Predicates.compose(Predicates.equalTo(propertyValue), propertyFunction);
}

From source file:com.isotrol.impe3.pms.core.impl.ExportJobManagerImpl.java

private ExportResult export(ExportJobEntity job) throws PMSException {
    NotFoundProviders.DEFAULT.checkCondition(Objects.equal(getUserId(), job.getCreated().getUser().getId()),
            job.getId());//from  w w w. ja  v a  2  s  .  c  o m
    final MessageLite msg;
    switch (job.getType()) {
    case CONTENT_TYPE:
        msg = loadContentTypes()
                .toPB(Predicates.compose(Predicates.in(job.getObjects()), ContentTypeObject.ID));
        break;
    case CONTENT_TYPE_ALL:
        msg = loadContentTypes().toPB();
        break;
    case CONNECTOR:
        msg = loadConnectors().toPB(fileManager,
                Predicates.compose(Predicates.in(job.getObjects()), ConnectorObject.ID));
        break;
    case CONNECTOR_ALL:
        msg = loadConnectors().toPB(fileManager);
        break;
    case MAPPING:
        msg = loadMappings().toPB(Predicates.compose(Predicates.in(job.getObjects()), SourceMappingObject.ID));
        break;
    case MAPPING_ALL:
        msg = loadMappings().toPB();
        break;
    case MAPPING_SETS:
        msg = loadMappings().get(job.getMainId()).toSetsPB();
        break;
    case MAPPING_CATEGORIES:
        msg = loadMappings().get(job.getMainId()).toCategoriesPB();
        break;
    case MAPPING_CONTENT_TYPES:
        msg = loadMappings().get(job.getMainId()).toContentTypesPB();
        break;
    case CATEGORY_LEVEL:
        msg = loadCategories().map2pb(job.getMainId(), false, false);
        break;
    case CATEGORY_LEVEL_ALL:
        msg = loadCategories().map2pb(job.getMainId(), false, true);
        break;
    case CATEGORY_NODE:
        msg = loadCategories().map2pb(job.getMainId(), true, false);
        break;
    case CATEGORY_NODE_ALL:
        msg = loadCategories().map2pb(job.getMainId(), true, true);
        break;
    case PORTAL_NAME:
        msg = loadContextGlobal().toPortal(job.getMainId()).toPortalNamePB();
        break;
    case PORTAL_BASE:
        msg = loadContextGlobal().toPortal(job.getMainId()).exportBases();
        break;
    case PORTAL_PROP:
        msg = loadContextGlobal().toPortal(job.getMainId()).exportProperties();
        break;
    case PORTAL_SET:
        msg = loadContextGlobal().toPortal(job.getMainId()).exportSets();
        break;
    case PORTAL_PAGE_ALL:
        msg = loadContextGlobal().toPortal(job.getMainId()).toDevice(job.getOtherId()).exportPages(fileManager);
        break;
    case COMPONENT:
        msg = loadContextGlobal().toPortal(job.getMainId()).getComponents().exportOwned(fileManager,
                Predicates.compose(Predicates.in(job.getObjects()), ComponentObject.ID));
        break;
    case COMPONENT_ALL:
        msg = loadContextGlobal().toPortal(job.getMainId()).getComponents().exportOwned(fileManager);
        break;
    case COMPONENT_OVR:
        msg = loadContextGlobal().toPortal(job.getMainId()).getComponents().exportOverriden(fileManager,
                Predicates.compose(Predicates.in(job.getObjects()), ComponentObject.ID));
        break;
    case COMPONENT_OVR_ALL:
        msg = loadContextGlobal().toPortal(job.getMainId()).getComponents().exportOverriden(fileManager);
        break;
    default:
        throw new EntityNotFoundException();
    }
    delete(job);
    return ExportResult.create(job.getType().getFileName(), msg);
}

From source file:forge.item.SealedProduct.java

protected List<PaperCard> getRandomBasicLands(final String setCode, final int count) {
    Predicate<PaperCard> cardsRule = Predicates.and(IPaperCard.Predicates.printedInSet(setCode),
            Predicates.compose(CardRulesPredicates.Presets.IS_BASIC_LAND, PaperCard.FN_GET_RULES));
    return Aggregates.random(Iterables.filter(StaticData.instance().getCommonCards().getAllCards(), cardsRule),
            count);/*from w  w  w .  j ava 2 s. c  o m*/
}

From source file:zotmc.collect.recipe.RecipeFinder.java

private static Predicate<RecipeView> resultFilter(final ItemStack item) {
    checkNotNull(item);//from ww  w.  ja  v  a  2s  .  c o  m
    return Predicates.compose(new Predicate<ItemStack>() {
        @Override
        public boolean apply(ItemStack input) {
            return input != null && input.getItem() == item.getItem()
                    && input.getItemDamage() == item.getItemDamage()
                    && Objects.equal(item.stackTagCompound, input.stackTagCompound);
        }
    }, GET_OUTPUT);
}

From source file:com.eucalyptus.util.CollectionUtils.java

/**
 * Convenience method for a predicate on a property value.
 *
 * @param propertyValues The property values to match
 * @param propertyFunction The function to extract the property
 * @param <T> The predicate type/*from  w  w  w .  jav  a2  s.  c o  m*/
 * @param <PT> The property type
 * @return A predicate that extracts a value to compare with the given values.
 */
public static <T, PT> Predicate<T> propertyPredicate(final Collection<PT> propertyValues,
        final Function<T, PT> propertyFunction) {
    return Predicates.compose(Predicates.in(propertyValues), propertyFunction);
}

From source file:io.opencensus.stats.StatsTestUtil.java

/**
 * Asserts that the two sets of {@code DistributionAggregate}s are equivalent, with a given
 * tolerance. The tolerance is used when comparing the mean and sum of values. The order of the
 * {@code DistributionAggregate}s has no effect. The expected parameter is last, because it is
 * likely to be a larger expression./*from  w  w w . j  av  a2  s .c o  m*/
 *
 * @param tolerance the tolerance used for {@code double} comparison.
 * @param actual the actual test result.
 * @param expected the expected value.
 * @throws AssertionError if the {@code DistributionAggregate}s don't match.
 */
static void assertDistributionAggregatesEquivalent(double tolerance, Collection<DistributionAggregate> actual,
        Collection<DistributionAggregate> expected) {
    Function<DistributionAggregate, List<Tag>> getTagsFunction = new Function<DistributionAggregate, List<Tag>>() {
        @Override
        public List<Tag> apply(DistributionAggregate agg) {
            return agg.getTags();
        }
    };
    Iterable<List<Tag>> expectedTags = Iterables.transform(expected, getTagsFunction);
    Iterable<List<Tag>> actualTags = Iterables.transform(actual, getTagsFunction);
    Truth.assertThat(actualTags).containsExactlyElementsIn(expectedTags);
    for (DistributionAggregate expectedAgg : expected) {
        DistributionAggregate actualAgg = Iterables.find(actual,
                Predicates.compose(Predicates.equalTo(expectedAgg.getTags()), getTagsFunction));
        assertDistributionAggregateValuesEquivalent("DistributionAggregate tags=" + expectedAgg.getTags(),
                tolerance, expectedAgg, actualAgg);
    }
}

From source file:org.apache.brooklyn.util.collections.CollectionFunctionals.java

public static Predicate<Iterable<?>> sizeEquals(int targetSize) {
    return Predicates.compose(Predicates.equalTo(targetSize), CollectionFunctionals.sizeFunction());
}

From source file:com.twitter.aurora.scheduler.thrift.aop.AopModule.java

@Override
protected void configure() {
    requireBinding(CapabilityValidator.class);

    // Layer ordering:
    // Log -> CapabilityValidator -> FeatureToggle -> StatsExporter -> APIVersion ->
    // SchedulerThriftInterface

    // TODO(Sathya): Consider using provider pattern for constructing interceptors to facilitate
    // unit testing without the creation of Guice injectors.
    bindThriftDecorator(new LoggingInterceptor());

    // Note: it's important that the capability interceptor is only applied to AuroraAdmin.Iface
    // methods, and does not pick up methods on AuroraSchedulerManager.Iface.
    MethodInterceptor authInterceptor = new UserCapabilityInterceptor();
    requestInjection(authInterceptor);/*w w w .ja  v a2s.com*/
    bindInterceptor(THRIFT_IFACE_MATCHER, GuiceUtils.interfaceMatcher(AuroraAdmin.Iface.class, true),
            authInterceptor);

    install(new PrivateModule() {
        @Override
        protected void configure() {
            // Ensure that the provided methods exist on the decorated interface.
            List<Method> methods = ImmutableList.copyOf(AuroraSchedulerManager.Iface.class.getMethods());
            for (String toggledMethod : toggledMethods.keySet()) {
                Preconditions.checkArgument(
                        Iterables.any(methods, Predicates.compose(Predicates.equalTo(toggledMethod), GET_NAME)),
                        String.format("Method %s was not found in class %s", toggledMethod,
                                AuroraSchedulerManager.Iface.class));
            }

            bind(new TypeLiteral<Map<String, Boolean>>() {
            }).toInstance(toggledMethods);
            bind(IsFeatureEnabled.class).in(Singleton.class);
            Key<Predicate<Method>> predicateKey = Key.get(new TypeLiteral<Predicate<Method>>() {
            });
            bind(predicateKey).to(IsFeatureEnabled.class);
            expose(predicateKey);
        }
    });
    bindThriftDecorator(new FeatureToggleInterceptor());
    bindThriftDecorator(new ThriftStatsExporterInterceptor());
    bindThriftDecorator(new APIVersionInterceptor());
}

From source file:org.sosy_lab.cpachecker.core.algorithm.precondition.PreconditionHelper.java

static List<ARGState> transformPath(ARGPath pPath) {
    List<ARGState> result = from(pPath.asStatesList()).skip(1).filter(Predicates
            .compose(PredicateAbstractState.FILTER_ABSTRACTION_STATES, toState(PredicateAbstractState.class)))
            .toList();/*from  w w  w  . ja v  a2 s .c o m*/

    //    assert from(result).allMatch(new Predicate<ARGState>() {
    //      @Override
    //      public boolean apply(ARGState pInput) {
    //        boolean correct = pInput.getChildren().size() <= 1;
    //        assert correct : "PreconditionWriter expects abstraction states to have only one children, but this state has more:" + pInput;
    //        return correct;
    //      }
    //    });

    assert pPath.getLastState() == result.get(result.size() - 1);
    return result;
}

From source file:com.eucalyptus.util.CollectionUtils.java

/**
 * Convenience method for a predicate on a property value.
 *
 * @param propertyValue The property value to match
 * @param propertyFunction The function to extract the collection property
 * @param <T> The predicate type//from   w  ww .ja  v  a2s .c o m
 * @param <PCT> The property collection type
 * @param <PIT> The property collection item type
 * @return A predicate that extracts a value to compare with the given value.
 */
public static <T, PIT, PCT extends Collection<? super PIT>> Predicate<T> propertyContainsPredicate(
        final PIT propertyValue, final Function<T, PCT> propertyFunction) {
    return Predicates.compose(contains(propertyValue), propertyFunction);
}