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

com.rbmhtechnology.vind.model.ComplexFieldDescriptor Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package com.rbmhtechnology.vind.model;

import com.rbmhtechnology.vind.api.query.filter.Filter;
import com.rbmhtechnology.vind.model.value.LatLng;

import java.util.List;
import java.util.function.Function;

/**
 * Created by fonso on 13.02.17.
 */
public abstract class ComplexFieldDescriptor extends FieldDescriptor {
    private Function> facetFunction;
    private Function storeFunction;
    private Function> fullTextFunction;
    private Function> suggestFunction;
    private Function> advanceFilter;
    private Class facetType;
    private Class storeType;
    private boolean filter;

    protected ComplexFieldDescriptor(String fieldName, Class type, Class facet, Class storedType) {
        super(fieldName, type);
        this.facetType = facet;
        this.storeType = storedType;
    }

    protected ComplexFieldDescriptor setFullText(boolean fullText,Function> lambda) {
        super.setFullText(fullText);
        this.fullTextFunction = lambda;
        return this;
    }

    protected ComplexFieldDescriptor setStored(boolean stored,Function lambda) {
        super.setStored(stored);
        this.storeFunction = lambda;
        return this;
    }

    protected ComplexFieldDescriptor setFacet(boolean facet,Function> lambda) {
        super.setFacet(facet);
        this.facetFunction = lambda;
        return this;
    }

    protected ComplexFieldDescriptor setSuggest(boolean suggest,Function> lambda) {
        super.setSuggest(suggest);
        this.suggestFunction = lambda;
        return this;
    }

    protected ComplexFieldDescriptor setAdvanceFilter(boolean filter,Function> lambda) {
        this.filter = filter;
        this.advanceFilter = lambda;
        return this;
    }

    public Function> getFacetFunction() {
        return facetFunction;
    }

    public Function> getFullTextFunction() {
        return fullTextFunction;
    }

    public Function> getSuggestFunction() {
        return suggestFunction;
    }

    public Function getStoreFunction() {
        return storeFunction;
    }

    public Function> getAdvanceFilter() {
        return advanceFilter;
    }

    public Class getFacetType() {
        return facetType;
    }

    public Class getStoreType() {
        return storeType;
    }

    public boolean isAdvanceFilter() {
        return filter;
    }

    /**
     * Instantiates a new {@link Filter} to checking if a field value is not empty.
     * @return A configured filter for the field.
     */
    public Filter isNotEmpty() {
        return isNotEmpty(Filter.DEFAULT_SCOPE);
    }

    /**
     * Instantiates a new {@link Filter} to checking if a field value is not empty.
     * @param scope Enum {@link Filter.Scope} describing the scope to perform the filter on.
     * @return A configured filter for the field.
     */
    public Filter isNotEmpty(Filter.Scope scope) {

        switch (scope) {
            case Suggest:
            return new Filter.NotEmptyFilter(this.getName(), scope);
            default:
                if(CharSequence.class.isAssignableFrom(this.facetType)) {
                    return new Filter.NotEmptyTextFilter(this.getName(), scope);
                }
                if(LatLng.class.isAssignableFrom(this.facetType)) {
                    return new Filter.NotEmptyLocationFilter(this.getName(), scope);
                }

                return new Filter.NotEmptyFilter(this.getName(), scope);
        }
    }

    /**
     * Instantiates a new {@link Filter} to checking if a field value is empty.
     * @return A configured filter for the field.
     */
    public Filter isEmpty() {
        return new Filter.NotFilter(this.isNotEmpty());
    }

    /**
     * Instantiates a new {@link Filter} to checking if a field value is empty.
     * @param scope Enum {@link Filter.Scope} describing the scope to perform the filter on.
     * @return A configured filter for the field.
     */
    public Filter isEmpty(Filter.Scope scope) {
        return new Filter.NotFilter( this.isNotEmpty(scope));
    }
}