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

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

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

import com.fasterxml.jackson.annotation.JsonCreator;
import io.sphere.sdk.queries.PagedResult;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public class PagedSearchResult extends PagedResult {

    private final Map facets;

    @JsonCreator
    PagedSearchResult(final Integer offset, final Integer total, final List results, final Map facets) {
        super(offset, total, results);
        this.facets = facets;
    }

    public Map getFacetsResults() {
        return facets;
    }

    public FacetResult getFacetResult(final String spherePath) {
        return facets.get(spherePath);
    }

    public TermFacetResult getTermFacetResult(final TermFacetExpression facetExpression) {
        return Optional.ofNullable(getFacetResult(facetExpression.resultPath())).map(facetResult -> {
            if (facetResult instanceof TermFacetResult) {
                return (TermFacetResult) facetResult;
            } else {
                throw new ClassCastException("Facet result is not of type TermFacetResult: " + facetResult);
            }
        }).orElse(null);
    }

    public RangeFacetResult getRangeFacetResult(final RangeFacetExpression facetExpression) {
        return Optional.ofNullable(getFacetResult(facetExpression.resultPath())).map(facetResult -> {
            if (facetResult instanceof RangeFacetResult) {
                return (RangeFacetResult) facetResult;
            } else {
                throw new ClassCastException("Facet result is not of type RangeFacetResult: " + facetResult);
            }
        }).orElse(null);
    }

    public FilteredFacetResult getFilteredFacetResult(final FilteredFacetExpression facetExpression) {
        return Optional.ofNullable(getFacetResult(facetExpression.resultPath())).map(facetResult -> {
            if (facetResult instanceof FilteredFacetResult) {
                return (FilteredFacetResult) facetResult;
            } else {
                throw new ClassCastException("Facet result is not of type FilterFacetResult: " + facetResult);
            }
        }).orElse(null);
    }

    @Override
    public List getResults() {
        return super.getResults();
    }

    @Override
    public Integer getOffset() {
        return super.getOffset();
    }

    @Override
    public int size() {
        return super.size();
    }

    @Override
    public Integer getTotal() {
        return super.getTotal();
    }

    @Override
    public Optional head() {
        return super.head();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy