001
002package com.commercetools.api.predicates.query;
003
004import static com.commercetools.api.models.InternalUtils.toStream;
005
006import java.util.Collection;
007import java.util.stream.Collectors;
008
009public interface ContainsPredicateBuilder<T, TValue> extends ComparablePredicateBuilder<T, TValue> {
010
011    /**
012     * create a predicate with the "contains any" operator
013     * @param value the values to be contained
014     * @return a combination predicate
015     */
016    default CombinationQueryPredicate<T> containsAny(final Iterable<TValue> value) {
017        Collection<QueryPredicate> p = toStream(value).map(this::format).collect(Collectors.toList());
018        ;
019
020        return combinationFn().apply(predicate().operator(PredicateOperator.CONTAINS.toString())
021                .right(ContainerQueryPredicate.of()
022                        .parent(ConstantQueryPredicate.of().constant("any"))
023                        .inner(CollectionQueryPredicate.of().predicates(p))));
024    }
025
026    /**
027     * create a predicate with the "contains all" operator
028     * @param value the values to be contained
029     * @return a combination predicate
030     */
031    default CombinationQueryPredicate<T> containsAll(final Iterable<TValue> value) {
032        Collection<QueryPredicate> p = toStream(value).map(this::format).collect(Collectors.toList());
033        ;
034        return combinationFn().apply(predicate().operator(PredicateOperator.CONTAINS.toString())
035                .right(ContainerQueryPredicate.of()
036                        .parent(ConstantQueryPredicate.of().constant("all"))
037                        .inner(CollectionQueryPredicate.of().predicates(p))));
038    }
039
040    /**
041     * creates a predicate with the "contains any" operator and the use of a predicate input variable
042     * @param variable the variable name
043     * @return a combination predicate
044     */
045    default CombinationQueryPredicate<T> containsAnyVar(final String variable) {
046        return combinationFn().apply(predicate().operator(PredicateOperator.CONTAINS.toString())
047                .right(ContainerQueryPredicate.of()
048                        .parent(ConstantQueryPredicate.of().constant("any"))
049                        .renderInnerWithOutParentheses(true)
050                        .inner(VariableQueryPredicate.of().constant(variable))));
051    }
052
053    /**
054     * creates a predicate with the "contains all" operator and the use of a predicate input variable
055     * @param variable the variable name
056     * @return a combination predicate
057     */
058    default CombinationQueryPredicate<T> containsAllVar(final String variable) {
059        return combinationFn().apply(predicate().operator(PredicateOperator.CONTAINS.toString())
060                .right(ContainerQueryPredicate.of()
061                        .parent(ConstantQueryPredicate.of().constant("all"))
062                        .renderInnerWithOutParentheses(true)
063                        .inner(VariableQueryPredicate.of().constant(variable))));
064    }
065
066}