com.testdroid.api.dto.Context Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testdroid-api Show documentation
Show all versions of testdroid-api Show documentation
The Testdroid API library for Java
package com.testdroid.api.dto;
import com.testdroid.api.APIEntity;
import com.testdroid.api.APISort;
import com.testdroid.api.filter.FilterEntry;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author Damian Sniezek
*/
public class Context {
public static final int DEFAULT_LIMIT = 20;
public static final int DEFAULT_OFFSET = 0;
public static final String FILTER_REQUEST_PARAM = "filter";
public static final String SEARCH_REQUEST_PARAM = "search";
public static final String FILTER_DELIMITER = ";";
public static final String FIELDS_REQUEST_PARAM = "fields";
public static final String GROUP_REQUEST_PARAM = "group";
public static final String LIMIT_REQUEST_PARAM = "limit";
public static final String SORT_REQUEST_PARAM = "sort";
public static final String OFFSET_REQUEST_PARAM = "offset";
private int limit = DEFAULT_LIMIT;
private int offset = DEFAULT_OFFSET;
private String search;
private APISort sort;
private List fields = new ArrayList<>();
private List groups = new ArrayList<>();
private List filters = new ArrayList<>();
private final Class type;
private Boolean cacheable = Boolean.FALSE;
private MultiValuedMap extraParams = new HashSetValuedHashMap<>();
private Long count;
public Context(Class type) {
this.type = type;
}
public Context(Class type, int offset, int limit, String search, String sort) {
this.offset = offset;
this.limit = limit;
this.search = search;
this.type = type;
this.sort = APISort.deserialize(sort);
}
@SuppressWarnings("squid:S107")
public Context(
Class type, int offset, int limit, String search, String sort, List filters,
List groups, List fields) {
this.offset = offset;
this.limit = limit;
this.search = search;
this.type = type;
this.sort = APISort.deserialize(sort);
this.filters = filters;
this.groups = groups;
this.fields = fields;
}
public int getOffset() {
return offset;
}
public Context setOffset(int offset) {
this.offset = offset;
return this;
}
public int getLimit() {
return limit;
}
public Context setLimit(int limit) {
this.limit = limit;
return this;
}
public String getSearch() {
return search;
}
public Context setSearch(String search) {
this.search = search;
return this;
}
public APISort getSort() {
return sort;
}
public Optional findSort(String field) {
return sort.getSorts().stream().filter(s -> Objects.equals(s.getColumn(), field)).findAny();
}
public Context setSort(APISort sort) {
this.sort = sort;
return this;
}
public Class getType() {
return type;
}
public List getFilters() {
return filters;
}
public Optional findFilter(String field, Operand operand) {
return filters.stream().filter(f -> keyAndOperandEqual(f, field, operand)).findAny();
}
public Context setFilters(List filters) {
this.filters = filters;
return this;
}
public Context addFilter(FilterEntry filterEntry) {
this.filters.add(filterEntry);
return this;
}
public List getFields() {
return fields;
}
public Context setFields(List fields) {
this.fields = fields;
return this;
}
public Context addField(String field) {
this.fields.add(field);
return this;
}
public List getGroups() {
return groups;
}
public Boolean getCacheable() {
return cacheable;
}
public Context setCacheable(Boolean cacheable) {
this.cacheable = cacheable;
return this;
}
public MultiValuedMap getExtraParams() {
return extraParams;
}
public void setExtraParams(MultiValuedMap extraParams) {
this.extraParams = extraParams;
}
public Context setCount(Long count) {
this.count = count;
return this;
}
public Optional computeMaxResult() {
Optional maxResult = Optional.empty();
if (limit != 0 && limit != Integer.MAX_VALUE) {
if (Objects.nonNull(count) && offset + limit > count) {
maxResult = Optional.of(count.intValue() % limit);
} else {
maxResult = Optional.of(limit);
}
}
return maxResult;
}
public MultiValuedMap build() {
MultiValuedMap map = new HashSetValuedHashMap<>();
map.put(LIMIT_REQUEST_PARAM, limit);
map.put(OFFSET_REQUEST_PARAM, offset);
map.put(SEARCH_REQUEST_PARAM, search);
map.put(SORT_REQUEST_PARAM, sort != null ? sort.serialize() : null);
map.put(FILTER_REQUEST_PARAM, filters.stream().map(FilterEntry::toString).collect(Collectors.joining(";")));
map.put(GROUP_REQUEST_PARAM, groups);
map.put(FIELDS_REQUEST_PARAM, fields);
map.putAll(extraParams);
return map;
}
public Context as(Class clazz) {
return new Context<>(clazz, this.offset, this.limit, this.search, this.sort
.serialize(), this.filters, this.groups, this.fields);
}
private Boolean keyAndOperandEqual(FilterEntry fe, String field, Operand operand) {
return Objects.equals(fe.getField(), field) && Objects.equals(fe.getOperand(), operand);
}
}