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

com.yuehuanghun.mybatis.milu.criteria.LambdaPredicateImpl Maven / Gradle / Ivy

There is a newer version: 1.18.0
Show newest version
/*
 * Copyright 2020-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yuehuanghun.mybatis.milu.criteria;

import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;

import com.yuehuanghun.mybatis.milu.annotation.Mode;
import com.yuehuanghun.mybatis.milu.criteria.lambda.LambdaReflections;
import com.yuehuanghun.mybatis.milu.criteria.lambda.SerializableFunction;
import com.yuehuanghun.mybatis.milu.generic.GenericProviderContext;
import com.yuehuanghun.mybatis.milu.tool.Assert;

import lombok.Getter;

public class LambdaPredicateImpl implements LambdaPredicate {
	protected T entity;
	
	@Getter
	private PredicateImpl delegate = new PredicateImpl();

	@Override
	public int renderSqlTemplate(GenericProviderContext context, StringBuilder expressionBuilder, Set columns,
			int paramIndex) {
		return getDelegate().renderSqlTemplate(context, expressionBuilder, columns, paramIndex);
	}

	@Override
	public int renderParams(GenericProviderContext context, Map params, int paramIndex) {
		return getDelegate().renderParams(context, params, paramIndex);
	}

	@Override
	public LambdaPredicate apply(T entity) {
		Assert.notNull(entity, "实体对象不能为空");
		this.entity = entity;
		return this;
	}
	
	protected void assertEntityNotNull() {
		if(entity == null) {
			throw new IllegalStateException("参数实体对象为null,需要先使用apply方法设置参数实体对象");
		}
	}

	@Override
	public LambdaPredicate conditionMode(Mode conditionMode) {
		getDelegate().conditionMode(conditionMode);
		return this;
	}

	@Override
	public LambdaPredicate and(Condition... conditions) {
		getDelegate().and(conditions);
		return this;
	}

	@Override
	public LambdaPredicate and(Consumer> predicate) {
		LambdaPredicateImpl p = new LambdaPredicateImpl<>();
		if(entity != null) {
			p.apply(entity);
		}
		predicate.accept(p);
		if(!p.isEmpty()) {
			p.getDelegate().setDepth(this.getDelegate().getDepth() + 1);
			getDelegate().and(p);
		}
		return this;
	}

	@Override
	public LambdaPredicate andP(Consumer predicate) {
		PredicateImpl p = new PredicateImpl();
		predicate.accept(p);
		if(!p.isEmpty()) {
			p.setDepth(this.getDelegate().getDepth() + 1);
			getDelegate().and(p);
		}
		return this;
	}

	@Override
	public LambdaPredicate or(Condition... conditions) {
		getDelegate().or(conditions);
		return this;
	}

	@Override
	public LambdaPredicate or(Consumer> predicate) {
		LambdaPredicateImpl p = new LambdaPredicateImpl<>();
		if(entity != null) {
			p.apply(entity);
		}
		predicate.accept(p);
		if(!p.isEmpty()) {
			p.getDelegate().setDepth(this.getDelegate().getDepth() + 1);
			getDelegate().or(p);
		}
		return this;
	}

	@Override
	public LambdaPredicate orP(Consumer predicate) {
		PredicateImpl p = new PredicateImpl();
		predicate.accept(p);
		if(!p.isEmpty()) {
			p.setDepth(this.getDelegate().getDepth() + 1);
			getDelegate().or(p);
		}
		return this;
	}
	
	@Override
	public LambdaPredicate not(Condition... conditions) {
		getDelegate().not(conditions);
		return this;
	}

	@Override
	public LambdaPredicate not(Consumer> predicate) {
		LambdaPredicateImpl p = new LambdaPredicateImpl<>();
		if(entity != null) {
			p.apply(entity);
		}
		predicate.accept(p);
		if(!p.isEmpty()) {
			p.getDelegate().setDepth(this.getDelegate().getDepth() + 1);
			getDelegate().not(p);
		}
		return this;
	}

	@Override
	public LambdaPredicate notP(Consumer predicate) {
		PredicateImpl p = new PredicateImpl();
		predicate.accept(p);
		if(!p.isEmpty()) {
			p.setDepth(this.getDelegate().getDepth() + 1);
			getDelegate().not(p);
		}
		return this;
	}

	@Override
	public LambdaPredicate eq(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().eq(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate eq(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().eq(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate neq(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().neq(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate neq(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().neq(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate lt(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().lt(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate lt(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().lt(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate lte(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().lte(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate lte(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().lte(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate gt(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().gt(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate gt(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().gt(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate gte(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().gte(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate gte(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().gte(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate like(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().like(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate like(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().like(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate notLike(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().notLike(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate notLike(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().notLike(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate contain(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().contain(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate contain(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().contain(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate notContain(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().notContain(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate notContain(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().notContain(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate startWith(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().startWith(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate startWith(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().startWith(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate endWith(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().endWith(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate endWith(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().endWith(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate between(SerializableFunction getterFn, Object startValue, Object endValue) {
		getDelegate().between(LambdaReflections.fnToFieldName(getterFn), startValue, endValue);
		return this;
	}
	
	@Override
	public LambdaPredicate between(boolean accept, SerializableFunction getterFn, Object startValue, Object endValue) {
		getDelegate().between(accept, LambdaReflections.fnToFieldName(getterFn), startValue, endValue);
		return this;
	}

	@Override
	public LambdaPredicate isNull(SerializableFunction getterFn) {
		getDelegate().isNull(LambdaReflections.fnToFieldName(getterFn));
		return this;
	}

	@Override
	public LambdaPredicate isNull(boolean accept, SerializableFunction getterFn) {
		getDelegate().isNull(accept, LambdaReflections.fnToFieldName(getterFn));
		return this;
	}

	@Override
	public LambdaPredicate notNull(SerializableFunction getterFn) {
		getDelegate().notNull(LambdaReflections.fnToFieldName(getterFn));
		return this;
	}

	@Override
	public LambdaPredicate notNull(boolean accept, SerializableFunction getterFn) {
		getDelegate().notNull(accept, LambdaReflections.fnToFieldName(getterFn));
		return this;
	}

	@Override
	public LambdaPredicate in(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().in(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate in(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().in(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate notIn(SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().notIn(LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate notIn(boolean accept, SerializableFunction getterFn) {
		assertEntityNotNull();
		getDelegate().notIn(accept, LambdaReflections.fnToFieldName(getterFn), getterFn.apply(entity));
		return this;
	}

	@Override
	public LambdaPredicate eq(SerializableFunction getterFn, Object value) {
		getDelegate().eq(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate eq(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().eq(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate neq(SerializableFunction getterFn, Object value) {
		getDelegate().neq(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate neq(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().neq(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate lt(SerializableFunction getterFn, Object value) {
		getDelegate().lt(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate lt(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().lt(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate lte(SerializableFunction getterFn, Object value) {
		getDelegate().lte(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate lte(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().lte(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate gt(SerializableFunction getterFn, Object value) {
		getDelegate().gt(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate gt(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().gt(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate gte(SerializableFunction getterFn, Object value) {
		getDelegate().gte(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate gte(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().gte(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate like(SerializableFunction getterFn, Object value) {
		getDelegate().like(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate like(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().like(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate notLike(SerializableFunction getterFn, Object value) {
		getDelegate().notLike(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate notLike(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().notLike(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate contain(SerializableFunction getterFn, Object value) {
		getDelegate().contain(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate contain(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().contain(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate notContain(SerializableFunction getterFn, Object value) {
		getDelegate().notContain(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate notContain(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().notContain(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate startWith(SerializableFunction getterFn, Object value) {
		getDelegate().startWith(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate startWith(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().startWith(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate endWith(SerializableFunction getterFn, Object value) {
		getDelegate().endWith(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate endWith(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().endWith(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate in(SerializableFunction getterFn, Object value) {
		getDelegate().in(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate in(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().in(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate notIn(SerializableFunction getterFn, Object value) {
		getDelegate().notIn(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate notIn(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().notIn(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate regex(SerializableFunction getterFn, Object value) {
		getDelegate().regex(LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate regex(boolean accept, SerializableFunction getterFn, Object value) {
		getDelegate().regex(accept, LambdaReflections.fnToFieldName(getterFn), value);
		return this;
	}

	@Override
	public LambdaPredicate undeleted() {
		getDelegate().undeleted();
		return this;
	}

	@Override
	public LambdaPredicate deleted() {
		getDelegate().deleted();
		return this;
	}

	@Override
	public int hashCode() {
		return this.getClass().hashCode() + this.getDelegate().hashCode() * 31;
	}

	@Override
	public boolean equals(Object that) {
		if(!this.getClass().isInstance(that)) {
			return false;
		}
		return Objects.equals(this.getDelegate(), ((LambdaPredicateImpl)that).getDelegate());
	}

	@Override
	public boolean isEmpty() {
		return getDelegate().isEmpty();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy