com.gravityrd.recengclient.webshop.GravityRecommendationContextBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-client Show documentation
Show all versions of java-client Show documentation
Gravity Java client to access recommendation engine.
package com.gravityrd.recengclient.webshop;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.gravityrd.receng.web.webshop.jsondto.GravityNameValue;
import com.gravityrd.receng.web.webshop.jsondto.GravityRecommendationContext;
import com.gravityrd.receng.web.webshop.jsondto.facet.FacetRequest;
import com.gravityrd.receng.web.webshop.jsondto.facet.FacetRequest.Filter;
import com.gravityrd.receng.web.webshop.jsondto.facet.FacetRequest.FilterLogic;
import com.gravityrd.receng.web.webshop.jsondto.facet.DynamicRangeFacetRequest;
import com.gravityrd.receng.web.webshop.jsondto.facet.Range;
import com.gravityrd.receng.web.webshop.jsondto.facet.RangeFacetRequest;
import com.gravityrd.receng.web.webshop.jsondto.facet.TermFacetRequest;
import com.gravityrd.receng.web.webshop.jsondto.facet.TermFacetRequest.Order;
import com.gravityrd.recengclient.webshop.GravityRecommendationContextBuilder.FacetBuilder.FacetRequestBuilder;
import com.gravityrd.recengclient.webshop.GravityRecommendationContextBuilder.FacetBuilder.RangeFacetBuilder;
import com.gravityrd.recengclient.webshop.GravityRecommendationContextBuilder.FacetBuilder.TermFacetBuilder;
public final class GravityRecommendationContextBuilder {
private int recommendationTime = (int)(System.currentTimeMillis() / 1000);
private int numberLimit;
private String scenarioId;
private List nameValues = new ArrayList();
private Set resultNameValues = new HashSet();
private HashMap resultNameValueFilters = new HashMap();
private List> facets = new ArrayList>();
public static final class FacetBuilder {
public static abstract class FacetRequestBuilder, T extends FacetRequest, F> {
protected String field;
protected FilterLogic filterLogic;
protected List filterValues = new ArrayList();
abstract T build();
public FacetRequestBuilder(String field) {
this.field = field;
}
@SuppressWarnings("unchecked")
public B filterLogic(FilterLogic filterLogic) {
this.filterLogic = filterLogic;
return (B) this;
}
@SuppressWarnings("unchecked")
public B filterValue(F value) {
filterValues.add(value);
return (B) this;
}
@SuppressWarnings("unchecked")
public B filterValues(Collection values) {
filterValues.addAll(values);
return (B) this;
}
}
public static final class TermFacetBuilder extends FacetRequestBuilder {
private int count;
private Order order;
public TermFacetBuilder(String field, int count) {
super(field);
this.count = count;
}
public TermFacetBuilder order(Order order) {
this.order = order;
return this;
}
@Override
public TermFacetRequest build() {
Filter filter = null;
if (filterValues.size() > 0) {
filter = new Filter(filterLogic, filterValues);
}
return new TermFacetRequest(field, count, order, filter);
}
}
public static final class RangeFacetBuilder extends FacetRequestBuilder{
private List ranges = new ArrayList();
public RangeFacetBuilder(String field) {
super(field);
}
public RangeFacetBuilder(String field, Collection ranges) {
super(field);
this.ranges.addAll(ranges);
}
public RangeFacetBuilder addRange(Range range) {
this.ranges.add(range);
return this;
}
public RangeFacetBuilder addRanges(Collection ranges) {
this.ranges.addAll(ranges);
return this;
}
public RangeFacetBuilder addRange(Double from, Double to) {
this.ranges.add(new Range(from, to));
return this;
}
public RangeFacetBuilder filterValue(Double from, Double to) {
filterValues.add(new Range(from, to));
return this;
}
@Override
public RangeFacetRequest build() {
if (ranges.size() == 0) throw new IllegalStateException("At least one range should be specified");
Filter filter = null;
if (filterValues.size() > 0) {
filter = new Filter(filterLogic, filterValues);
}
return new RangeFacetRequest(field, ranges, filter);
}
}
public static TermFacetBuilder term(String field, int count) {
return new TermFacetBuilder(field, count);
}
public static RangeFacetBuilder range(String field) {
return new RangeFacetBuilder(field);
}
public static RangeFacetBuilder range(String field, List ranges) {
return new RangeFacetBuilder(field, ranges);
}
public static DynamicRangeFacetBuilder dynamicRange(String field, int maxRangeCount, int quantum) {
return new DynamicRangeFacetBuilder(field, maxRangeCount, quantum);
}
}
public static final class DynamicRangeFacetBuilder extends FacetRequestBuilder {
private List ranges = new ArrayList();
private int maxRangeCount;
private int quantum;
public DynamicRangeFacetBuilder(String field, int maxRangeCount, int quantum) {
super(field);
this.maxRangeCount = maxRangeCount;
this.quantum = quantum;
}
public DynamicRangeFacetBuilder filterValue(Double from, Double to) {
filterValues.add(new Range(from, to));
return this;
}
@Override
public DynamicRangeFacetRequest build() {
Filter filter = null;
if (filterValues.size() > 0) {
filter = new Filter(filterLogic, filterValues);
}
return new DynamicRangeFacetRequest(field, maxRangeCount, quantum, filter);
}
}
public static TermFacetBuilder term(String field, int count) {
return new TermFacetBuilder(field, count);
}
public static RangeFacetBuilder range(String field) {
return new RangeFacetBuilder(field);
}
public static RangeFacetBuilder range(String field, List ranges) {
return new RangeFacetBuilder(field, ranges);
}
public GravityRecommendationContextBuilder(String scenarioId, int numberLimit) {
this.numberLimit = numberLimit;
this.scenarioId = scenarioId;
}
public GravityRecommendationContextBuilder setRecommendationTime(int recommendationTime) {
this.recommendationTime = recommendationTime;
return this;
}
public GravityRecommendationContextBuilder addNameValue(String name, String value) {
this.nameValues.add(new GravityNameValue(name, value));
return this;
}
public GravityRecommendationContextBuilder addNameValue(List names, List values) {
if (names.size() != values.size()) {
throw new IllegalArgumentException("Size of names and values should be equal!");
}
for (int i=0; i < names.size(); i++) {
this.nameValues.add(new GravityNameValue(names.get(i), values.get(i)));
}
return this;
}
public GravityRecommendationContextBuilder addNameValue(Collection nameValues) {
this.nameValues.addAll(nameValues);
return this;
}
public GravityRecommendationContextBuilder addResultNameValue(String name) {
this.resultNameValues.add(name);
return this;
}
public GravityRecommendationContextBuilder addResultNameValues(Collection names) {
this.resultNameValues.addAll(names);
return this;
}
public GravityRecommendationContextBuilder addResultNameValueFilter(String name, Collection values) {
this.resultNameValueFilters.put(name, values.toArray(new String[values.size()]));
return this;
}
public GravityRecommendationContextBuilder addFacet(FacetRequest> facet) {
this.facets.add(facet);
return this;
}
public GravityRecommendationContextBuilder addFacet(FacetRequestBuilder,?,?> facetBuilder) {
this.facets.add(facetBuilder.build());
return this;
}
public GravityRecommendationContext build() {
GravityRecommendationContext context = new GravityRecommendationContext();
context.scenarioId = scenarioId;
context.numberLimit = numberLimit;
context.recommendationTime = recommendationTime;
context.nameValues = nameValues.toArray(new GravityNameValue[nameValues.size()]);
context.resultNameValues = resultNameValues.toArray(new String[resultNameValues.size()]);
context.resultNameValueFilters = resultNameValueFilters.size() > 0 ? resultNameValueFilters : null;
context.facets = facets.size() > 0 ? facets : null;
return context;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy