Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.itxiaoer.commons.jpa.Restrictions Maven / Gradle / Ivy
package com.itxiaoer.commons.jpa;
import com.itxiaoer.commons.core.Operator;
import com.itxiaoer.commons.orm.TransformHandler;
import com.itxiaoer.commons.orm.Transformation;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author : liuyk
*/
@SuppressWarnings({"unused", "WeakerAccess", "UnusedReturnValue"})
public class Restrictions {
private Criteria criteria = new Criteria<>();
private Restrictions() {
}
public static Restrictions of() {
return new Restrictions<>();
}
public Restrictions where(E query) {
Map fields = TransformHandler.fields(query);
fields.forEach((k, v) -> parse(v));
return this;
}
public Restrictions eq(String name, Object value) {
parse(name, value, true, Operator.EQ);
return this;
}
public Restrictions eq(String name, Object value, boolean ignoreEmpty) {
parse(name, value, ignoreEmpty, Operator.EQ);
return this;
}
public Restrictions hasMember(String name, Object value) {
parse(name, value, true, Operator.IS_MEMBER);
return this;
}
public Restrictions hasMember(String name, Object value, boolean ignoreEmpty) {
parse(name, value, ignoreEmpty, Operator.IS_MEMBER);
return this;
}
public Restrictions ne(String name, Object value) {
parse(name, value, true, Operator.NE);
return this;
}
public Restrictions ne(String name, Object value, boolean ignoreEmpty) {
parse(name, value, ignoreEmpty, Operator.NE);
return this;
}
public Restrictions like(String name, String value) {
parse(name, value, true, Operator.LIKE);
return this;
}
public Restrictions like(String name, String value, boolean ignoreEmpty) {
parse(name, value, ignoreEmpty, Operator.LIKE);
return this;
}
public Restrictions gt(String name, Object value) {
parse(name, value, true, Operator.GT);
return this;
}
public Restrictions gt(String name, Object value, boolean ignoreEmpty) {
parse(name, value, ignoreEmpty, Operator.GT);
return this;
}
public Restrictions lt(String name, Object value) {
parse(name, value, true, Operator.LT);
return this;
}
public Restrictions lt(String name, Object value, boolean ignoreEmpty) {
parse(name, value, ignoreEmpty, Operator.LT);
return this;
}
public Restrictions lte(String name, Object value) {
parse(name, value, true, Operator.GTE);
return this;
}
public Restrictions lte(String name, Object value, boolean ignoreEmpty) {
parse(name, value, ignoreEmpty, Operator.GTE);
return this;
}
public Restrictions gte(String name, Object value) {
parse(name, value, true, Operator.LTE);
return this;
}
public Restrictions gte(String name, Object value, boolean ignoreEmpty) {
parse(name, value, ignoreEmpty, Operator.LTE);
return this;
}
public Restrictions in(String name, Collection value) {
parse(name, value, true, Operator.IN);
return this;
}
public Restrictions in(String name, Collection value, boolean ignoreEmpty) {
parse(name, value, ignoreEmpty, Operator.IN);
return this;
}
public Restrictions notIn(String name, Collection value) {
parse(name, value, true, Operator.NOT_IN);
return this;
}
public Restrictions notIn(String name, Collection value, boolean ignoreEmpty) {
parse(name, value, ignoreEmpty, Operator.NOT_IN);
return this;
}
public Restrictions and(Criterion... criterion) {
criteria.add(new LogicalCriterion(Arrays.asList(criterion), Operator.AND));
return this;
}
public Restrictions or(Criterion... criterion) {
criteria.add(new LogicalCriterion(Arrays.asList(criterion), Operator.OR));
return this;
}
public Criteria get() {
return this.criteria;
}
private static boolean ignore(Object value, boolean ignoreEmpty) {
if (Objects.isNull(value) && ignoreEmpty) {
return true;
}
if (value instanceof String) {
String v = (String) value;
return StringUtils.isBlank(v) && ignoreEmpty;
}
if (value instanceof Collection) {
Collection v = (Collection) value;
return v.isEmpty() && ignoreEmpty;
}
return false;
}
private void parse(String name, Object value, boolean ignoreEmpty, Operator operator) {
if (ignore(value, ignoreEmpty)) {
return;
}
criteria.add(new SimpleCriterion(name, value, operator));
}
private void parse(Transformation transformation) {
if (ignore(transformation.getValue(), transformation.isIgnoreEmpty())) {
return;
}
String[] fields = transformation.getField();
if (Array.getLength(fields) == 1) {
criteria.add(new SimpleCriterion(fields[0], transformation.getValue(), transformation.getOperator()));
} else {
List criterion = Stream.of(fields).map(k -> new SimpleCriterion(k, transformation.getValue(), transformation.getOperator())).collect(Collectors.toList());
Criterion[] criteria = criterion.toArray(new Criterion[]{});
if (Operator.OR == transformation.getRelation()) {
or(criteria);
} else {
and(criteria);
}
}
}
}