All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sphere.sdk.search.TermExpression Maven / Gradle / Ivy

There is a newer version: 1.0.0-M26
Show newest version
package io.sphere.sdk.search;

import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nullable;
import java.util.Optional;

import static io.sphere.sdk.utils.IterableUtils.toStream;
import static java.util.stream.Collectors.joining;

abstract class TermExpression extends SearchModelExpression {
    private final Iterable terms;

    TermExpression(final SearchModel searchModel, final TypeSerializer typeSerializer, final Iterable terms, @Nullable final String alias) {
        super(searchModel, typeSerializer, alias);
        this.terms = terms;
    }

    @Override
    protected String serializedValue() {
        return Optional.ofNullable(toTermExpression()).map(e -> ":" + e).orElse("");
    }

    /**
     * Turns a group of terms into an expression of the form "term1,term2,..."
     * @return the generated term expression.
     */
    @Nullable
    private String toTermExpression() {
        return StringUtils.trimToNull(toStream(terms).map(t -> serializer().apply(t))
                .filter(t -> !t.isEmpty()).collect(joining(",")));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy