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

com.blackbirdai.client.model.FacetParam Maven / Gradle / Ivy

package com.blackbirdai.client.model;

import com.blackbirdai.client.InvalidRequestException;
import com.blackbirdai.client.util.Joiners;
import com.blackbirdai.client.util.Patterns;
import com.google.common.base.Optional;

import java.util.Collections;
import java.util.List;

/**
 * Container for facet parameter.
 */
abstract public class FacetParam extends Param {
    protected final String field;
    protected final List excludeTags;
    protected final Optional key;

    protected FacetParam(String field, List excludeTags, Optional key) {
        this.field = field;
        this.excludeTags = excludeTags;
        this.key = key;
    }

    /**
     * Builds a enum-facet parameter with exclude-tags.
     */
    public static FacetParam enumFacet(String field, int num, List excludeTags) {
        return new EnumFacetParam(field, excludeTags, Optional.of(num), Optional.absent());
    }

    /**
     * Builds a enum-facet parameter.
     */
    public static FacetParam enumFacet(String field, int num) {
        return new EnumFacetParam(field, Collections.EMPTY_LIST, Optional.of(num), Optional.absent());
    }

    /**
     * Builds a enum-facet parameter with exclude-tags using default 'num' parameter.
     */
    public static FacetParam enumFacet(String field, List excludeTags) {
        return new EnumFacetParam(field, excludeTags, Optional.absent(), Optional.absent());
    }

    /**
     * Builds a enum-facet parameter using default 'num' parameter.
     */
    public static FacetParam enumFacet(String field) {
        return new EnumFacetParam(field, Collections.EMPTY_LIST, Optional.absent(), Optional.absent());
    }

    /**
     * Builds a enum-facet parameter with exclude-tags and explicit key.
     */
    public static FacetParam enumFacet(String field, int num, List excludeTags, String key) {
        return new EnumFacetParam(field, excludeTags, Optional.of(num), Optional.of(key));
    }

    /**
     * Builds a enum-facet parameter with explicit key.
     */
    public static FacetParam enumFacet(String field, int num, String key) {
        return new EnumFacetParam(field, Collections.EMPTY_LIST, Optional.of(num), Optional.of(key));
    }

    /**
     * Builds a enum-facet parameter with exclude-tags using default 'num' parameter with explicit key.
     */
    public static FacetParam enumFacet(String field, List excludeTags, String key) {
        return new EnumFacetParam(field, excludeTags, Optional.absent(), Optional.of(key));
    }

    /**
     * Builds a enum-facet parameter using default 'num' parameter with explicit key.
     */
    public static FacetParam enumFacet(String field, String key) {
        return new EnumFacetParam(field, Collections.EMPTY_LIST, Optional.absent(), Optional.of(key));
    }

    /**
     * Builds a histogram-facet parameter with exclude-tags.
     */
    public static FacetParam histFacet(String field, int start, int end, int gap, List excludeTags) {
        return new HistogramFacetParam(field, excludeTags, start, end, gap, Optional.absent());
    }

    /**
     * Builds a histogram-facet parameter.
     */
    public static FacetParam histFacet(String field, int start, int end, int gap) {
        return new HistogramFacetParam(field, Collections.EMPTY_LIST, start, end, gap, Optional.absent());
    }

    /**
     * Builds a histogram-facet parameter with exclude-tags and explicit key.
     */
    public static FacetParam histFacet(String field, int start, int end, int gap, List excludeTags, String key) {
        return new HistogramFacetParam(field, excludeTags, start, end, gap, Optional.of(key));
    }

    /**
     * Builds a histogram-facet parameter with explicit key.
     */
    public static FacetParam histFacet(String field, int start, int end, int gap, String key) {
        return new HistogramFacetParam(field, Collections.EMPTY_LIST, start, end, gap, Optional.of(key));
    }

    /**
     * Builds a range-facet parameter with exclude-tags.
     */
    public static FacetParam rangeFacet(String field, List excludeTags) {
        return new RangeFacetParam(field, excludeTags, Optional.absent());
    }

    /**
     * Builds a range-facet parameter with exclude-tags and explicit key.
     */
    public static FacetParam rangeFacet(String field, List excludeTags, String key) {
        return new RangeFacetParam(field, excludeTags, Optional.of(key));
    }

    /**
     * Builds a range-facet parameter.
     */
    public static FacetParam rangeFacet(String field) {
        return new RangeFacetParam(field, Collections.EMPTY_LIST, Optional.absent());
    }

    /**
     * Builds a range-facet parameter with explicit key.
     */
    public static FacetParam rangeFacet(String field, String key) {
        return new RangeFacetParam(field, Collections.EMPTY_LIST, Optional.of(key));
    }

    protected StringBuilder appendCommonQueryStr(StringBuilder builder) throws InvalidRequestException {
        if (!Patterns.FIELD_P.matcher(field).matches()) {
            throw new InvalidRequestException("Invalid field-name found: " + field);
        }
        builder.append("facet=field=");
        builder.append(field);
        if (excludeTags.size() > 0) {
            builder.append("/ex=");
            for (String tag: excludeTags) {
                if (!Patterns.TAG_P.matcher(tag).matches()) {
                    throw new InvalidRequestException("Invalid tag found: " + tag);
                }
            }
            builder.append(Joiners.COMMA_JOINER.join(excludeTags));
        }
        if (key.isPresent()) {
            builder.append("/key=");
            builder.append(key.get());
        }
        return builder;
    }
}

class EnumFacetParam extends FacetParam {
    private final Optional num;

    EnumFacetParam(String field, List excludeTags, Optional num, Optional key) {
        super(field, excludeTags, key);
        this.num = num;
    }

    @Override
    StringBuilder appendQueryStr(StringBuilder builder) throws InvalidRequestException {
        appendCommonQueryStr(builder);
        builder.append("/type=enum");
        if (num.isPresent()) {
            builder.append("/num=");
            builder.append(num.get());
        }
        return builder;
    }
}

class HistogramFacetParam extends FacetParam {
    private final int start;
    private final int end;
    private final int gap;

    HistogramFacetParam(String field, List excludeTags, int start, int end, int gap, Optional key) {
        super(field, excludeTags, key);
        this.start = start;
        this.end = end;
        this.gap = gap;
    }

    @Override
    StringBuilder appendQueryStr(StringBuilder builder) throws InvalidRequestException {
        appendCommonQueryStr(builder);
        builder.append("/type=hist/range=[");
        builder.append(start);
        builder.append(':');
        builder.append(end);
        builder.append(':');
        builder.append(gap);
        builder.append(']');
        return builder;
    }
}

class RangeFacetParam extends FacetParam {
    RangeFacetParam(String field, List excludeTags, Optional key) {
        super(field, excludeTags, key);
    }

    @Override
    StringBuilder appendQueryStr(StringBuilder builder) throws InvalidRequestException {
        appendCommonQueryStr(builder);
        builder.append("/type=range");
        return builder;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy