增加LambdaFunction和LambdaSupplier功能
This commit is contained in:
@@ -8,6 +8,7 @@ package org.redkale.source;
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import static org.redkale.source.ColumnExpress.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* ColumnValue主要用于多个字段更新的表达式。
|
||||
@@ -30,6 +31,22 @@ public class ColumnValue {
|
||||
public ColumnValue() {
|
||||
}
|
||||
|
||||
public <T extends Serializable> ColumnValue(LambdaSupplier<T> func) {
|
||||
this(LambdaSupplier.readColumn(func), ColumnExpress.MOV, func.get());
|
||||
}
|
||||
|
||||
public <T extends Serializable> ColumnValue(LambdaSupplier<T> func, ColumnExpress express) {
|
||||
this(LambdaSupplier.readColumn(func), express, func.get());
|
||||
}
|
||||
|
||||
public <T> ColumnValue(LambdaFunction<T, ?> func, Serializable value) {
|
||||
this(LambdaFunction.readColumn(func), ColumnExpress.MOV, value);
|
||||
}
|
||||
|
||||
public <T> ColumnValue(LambdaFunction<T, ?> func, ColumnExpress express, Serializable value) {
|
||||
this(LambdaFunction.readColumn(func), express, value);
|
||||
}
|
||||
|
||||
public ColumnValue(String column, Serializable value) {
|
||||
this(column, ColumnExpress.MOV, value);
|
||||
}
|
||||
@@ -175,6 +192,256 @@ public class ColumnValue {
|
||||
return new ColumnValue(column, ORR, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同 mov 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
*
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T extends Serializable> ColumnValue create(LambdaSupplier<T> func) {
|
||||
return new ColumnValue(func);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param <T> 值的泛型
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T extends Serializable> ColumnValue mov(LambdaSupplier<T> func) {
|
||||
return new ColumnValue(func, MOV);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} + {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param <T> 值的泛型
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T extends Serializable> ColumnValue inc(LambdaSupplier<T> func) {
|
||||
return new ColumnValue(func, INC);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} - {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param <T> 值的泛型
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T extends Serializable> ColumnValue dec(LambdaSupplier<T> func) {
|
||||
return new ColumnValue(func, DEC);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} * {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param <T> 值的泛型
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T extends Serializable> ColumnValue mul(LambdaSupplier<T> func) {
|
||||
return new ColumnValue(func, MUL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} / {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param <T> 值的泛型
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T extends Serializable> ColumnValue div(LambdaSupplier<T> func) {
|
||||
return new ColumnValue(func, DIV);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} & {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param <T> 值的泛型
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T extends Serializable> ColumnValue and(LambdaSupplier<T> func) {
|
||||
return new ColumnValue(func, AND);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} | {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param <T> 值的泛型
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T extends Serializable> ColumnValue orr(LambdaSupplier<T> func) {
|
||||
return new ColumnValue(func, ORR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同 mov 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param value 字段值
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T> ColumnValue create(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return new ColumnValue(func, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param value 字段值
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T> ColumnValue mov(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return new ColumnValue(func, MOV, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} + {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param value 字段值
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T> ColumnValue inc(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return new ColumnValue(func, INC, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} + 1 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T> ColumnValue inc(LambdaFunction<T, ?> func) {
|
||||
return new ColumnValue(func, INC, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} - {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param value 字段值
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T> ColumnValue dec(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return new ColumnValue(func, DEC, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} - 1 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T> ColumnValue dec(LambdaFunction<T, ?> func) {
|
||||
return new ColumnValue(func, DEC, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} * {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param value 字段值
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T> ColumnValue mul(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return new ColumnValue(func, MUL, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} / {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param value 字段值
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T> ColumnValue div(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return new ColumnValue(func, DIV, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} & {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param value 字段值
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T> ColumnValue and(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return new ColumnValue(func, AND, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} | {value} 操作
|
||||
*
|
||||
* @param func 字段名Lambda
|
||||
* @param value 字段值
|
||||
*
|
||||
* @return ColumnValue
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T> ColumnValue orr(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return new ColumnValue(func, ORR, value);
|
||||
}
|
||||
|
||||
public String getColumn() {
|
||||
return column;
|
||||
}
|
||||
|
||||
@@ -574,6 +574,23 @@ public interface DataSource extends Resourcable {
|
||||
*/
|
||||
public <T> int updateColumn(final Class<T> clazz, final Serializable pk, final String column, final Serializable value);
|
||||
|
||||
/**
|
||||
* 更新单个记录的单个字段 <br>
|
||||
* <b>注意</b>:即使字段标记为@Column(updatable=false)也会被更新 <br>
|
||||
* 等价SQL: UPDATE {table} SET {column} = {value} WHERE {primary} = {id} <br>
|
||||
*
|
||||
* @param <T> Entity泛型
|
||||
* @param <V> 更新值泛型
|
||||
* @param clazz Entity类
|
||||
* @param pk 主键
|
||||
* @param func 更新值Lambda
|
||||
*
|
||||
* @return 影响的记录条数
|
||||
*/
|
||||
default <T, V extends Serializable> int updateColumn(final Class<T> clazz, final Serializable pk, final LambdaSupplier<V> func) {
|
||||
return updateColumn(clazz, pk, LambdaSupplier.readColumn(func), func.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新单个记录的单个字段 <br>
|
||||
* <b>注意</b>:即使字段标记为@Column(updatable=false)也会被更新 <br>
|
||||
@@ -589,6 +606,23 @@ public interface DataSource extends Resourcable {
|
||||
*/
|
||||
public <T> CompletableFuture<Integer> updateColumnAsync(final Class<T> clazz, final Serializable pk, final String column, final Serializable value);
|
||||
|
||||
/**
|
||||
* 更新单个记录的单个字段 <br>
|
||||
* <b>注意</b>:即使字段标记为@Column(updatable=false)也会被更新 <br>
|
||||
* 等价SQL: UPDATE {table} SET {column} = {value} WHERE {primary} = {id} <br>
|
||||
*
|
||||
* @param <T> Entity泛型
|
||||
* @param <V> 更新值泛型
|
||||
* @param clazz Entity类
|
||||
* @param pk 主键
|
||||
* @param func 更新值Lambda
|
||||
*
|
||||
* @return 影响的记录条数
|
||||
*/
|
||||
default <T, V extends Serializable> CompletableFuture<Integer> updateColumnAsync(final Class<T> clazz, final Serializable pk, final LambdaSupplier<V> func) {
|
||||
return updateColumnAsync(clazz, pk, LambdaSupplier.readColumn(func), func.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新符合过滤条件记录的单个字段 <br>
|
||||
* <b>注意</b>:即使字段标记为@Column(updatable=false)也会被更新 <br>
|
||||
@@ -604,6 +638,23 @@ public interface DataSource extends Resourcable {
|
||||
*/
|
||||
public <T> int updateColumn(final Class<T> clazz, final String column, final Serializable value, final FilterNode node);
|
||||
|
||||
/**
|
||||
* 更新符合过滤条件记录的单个字段 <br>
|
||||
* <b>注意</b>:即使字段标记为@Column(updatable=false)也会被更新 <br>
|
||||
* 等价SQL: UPDATE {table} SET {column} = {value} WHERE {filter node} <br>
|
||||
*
|
||||
* @param <T> Entity泛型
|
||||
* @param <V> 更新值泛型
|
||||
* @param clazz Entity类
|
||||
* @param func 更新值Lambda
|
||||
* @param node 过滤条件
|
||||
*
|
||||
* @return 影响的记录条数
|
||||
*/
|
||||
default <T, V extends Serializable> int updateColumn(final Class<T> clazz, final LambdaSupplier<V> func, final FilterNode node) {
|
||||
return updateColumn(clazz, LambdaSupplier.readColumn(func), func.get(), node);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新符合过滤条件记录的单个字段 <br>
|
||||
* <b>注意</b>:即使字段标记为@Column(updatable=false)也会被更新 <br>
|
||||
@@ -619,6 +670,23 @@ public interface DataSource extends Resourcable {
|
||||
*/
|
||||
public <T> CompletableFuture<Integer> updateColumnAsync(final Class<T> clazz, final String column, final Serializable value, final FilterNode node);
|
||||
|
||||
/**
|
||||
* 更新符合过滤条件记录的单个字段 <br>
|
||||
* <b>注意</b>:即使字段标记为@Column(updatable=false)也会被更新 <br>
|
||||
* 等价SQL: UPDATE {table} SET {column} = {value} WHERE {filter node} <br>
|
||||
*
|
||||
* @param <T> Entity泛型
|
||||
* @param <V> 更新值泛型
|
||||
* @param clazz Entity类
|
||||
* @param func 更新值Lambda
|
||||
* @param node 过滤条件
|
||||
*
|
||||
* @return 影响的记录条数
|
||||
*/
|
||||
default <T, V extends Serializable> CompletableFuture<Integer> updateColumnAsync(final Class<T> clazz, final LambdaSupplier<V> func, final FilterNode node) {
|
||||
return updateColumnAsync(clazz, LambdaSupplier.readColumn(func), func.get(), node);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新指定主键值记录的部分字段 <br>
|
||||
* 字段赋值操作选项见 ColumnExpress <br>
|
||||
@@ -728,6 +796,21 @@ public interface DataSource extends Resourcable {
|
||||
return updateColumn(entity, (FilterNode) null, columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新单个记录的指定字段 <br>
|
||||
* <b>注意</b>:Entity类中标记为@Column(updatable=false)不会被更新 <br>
|
||||
* 等价SQL: UPDATE {table} SET {column1} = {value1}, {column2} = {value2}, {column3} = {value3}, ··· WHERE {primary} = {bean.id} <br>
|
||||
*
|
||||
* @param <T> Entity泛型
|
||||
* @param entity 待更新的Entity对象
|
||||
* @param funcs 需更新的字段名Lambda集合
|
||||
*
|
||||
* @return 影响的记录条数
|
||||
*/
|
||||
default <T> int updateColumn(final T entity, final LambdaFunction<T, ?>... funcs) {
|
||||
return updateColumn(entity, (FilterNode) null, LambdaFunction.readColumns(funcs));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新单个记录的指定字段 <br>
|
||||
* <b>注意</b>:Entity类中标记为@Column(updatable=false)不会被更新 <br>
|
||||
@@ -743,6 +826,21 @@ public interface DataSource extends Resourcable {
|
||||
return updateColumnAsync(entity, (FilterNode) null, columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新单个记录的指定字段 <br>
|
||||
* <b>注意</b>:Entity类中标记为@Column(updatable=false)不会被更新 <br>
|
||||
* 等价SQL: UPDATE {table} SET {column1} = {value1}, {column2} = {value2}, {column3} = {value3}, ··· WHERE {primary} = {bean.id} <br>
|
||||
*
|
||||
* @param <T> Entity泛型
|
||||
* @param entity 待更新的Entity对象
|
||||
* @param funcs 需更新的字段名Lambda集合
|
||||
*
|
||||
* @return 影响的记录条数CompletableFuture
|
||||
*/
|
||||
default <T> CompletableFuture<Integer> updateColumnAsync(final T entity, final LambdaFunction<T, ?>... funcs) {
|
||||
return updateColumnAsync(entity, (FilterNode) null, LambdaFunction.readColumns(funcs));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新符合过滤条件记录的指定字段 <br>
|
||||
* <b>注意</b>:Entity类中标记为@Column(updatable=false)不会被更新 <br>
|
||||
@@ -757,6 +855,22 @@ public interface DataSource extends Resourcable {
|
||||
*/
|
||||
public <T> int updateColumn(final T entity, final FilterNode node, final String... columns);
|
||||
|
||||
/**
|
||||
* 更新符合过滤条件记录的指定字段 <br>
|
||||
* <b>注意</b>:Entity类中标记为@Column(updatable=false)不会被更新 <br>
|
||||
* 等价SQL: UPDATE {table} SET {column1} = {value1}, {column2} = {value2}, {column3} = {value3}, ··· WHERE {filter node} <br>
|
||||
*
|
||||
* @param <T> Entity泛型
|
||||
* @param entity 待更新的Entity对象
|
||||
* @param node 过滤条件
|
||||
* @param funcs 需更新的字段名Lambda集合
|
||||
*
|
||||
* @return 影响的记录条数
|
||||
*/
|
||||
default <T> int updateColumn(final T entity, final FilterNode node, final LambdaFunction<T, ?>... funcs) {
|
||||
return updateColumn(entity, node, LambdaFunction.readColumns(funcs));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新符合过滤条件记录的指定字段 <br>
|
||||
* <b>注意</b>:Entity类中标记为@Column(updatable=false)不会被更新 <br>
|
||||
@@ -771,6 +885,22 @@ public interface DataSource extends Resourcable {
|
||||
*/
|
||||
public <T> CompletableFuture<Integer> updateColumnAsync(final T entity, final FilterNode node, final String... columns);
|
||||
|
||||
/**
|
||||
* 更新符合过滤条件记录的指定字段 <br>
|
||||
* <b>注意</b>:Entity类中标记为@Column(updatable=false)不会被更新 <br>
|
||||
* 等价SQL: UPDATE {table} SET {column1} = {value1}, {column2} = {value2}, {column3} = {value3}, ··· WHERE {filter node} <br>
|
||||
*
|
||||
* @param <T> Entity泛型
|
||||
* @param entity 待更新的Entity对象
|
||||
* @param node 过滤条件
|
||||
* @param funcs 需更新的字段名Lambda集合
|
||||
*
|
||||
* @return 影响的记录条数
|
||||
*/
|
||||
default <T> CompletableFuture<Integer> updateColumnAsync(final T entity, final FilterNode node, final LambdaFunction<T, ?>... funcs) {
|
||||
return updateColumnAsync(entity, node, LambdaFunction.readColumns(funcs));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新单个记录的指定字段 <br>
|
||||
* <b>注意</b>:Entity类中标记为@Column(updatable=false)不会被更新 <br>
|
||||
|
||||
@@ -161,6 +161,30 @@ public class FilterNode { //FilterNode 不能实现Serializable接口, 否则
|
||||
return and(new FilterNode(column, express, itemand, value));
|
||||
}
|
||||
|
||||
public final <T extends Serializable> FilterNode and(LambdaSupplier<T> func) {
|
||||
return and(func, null);
|
||||
}
|
||||
|
||||
public final <T extends Serializable> FilterNode and(LambdaSupplier<T> func, FilterExpress express) {
|
||||
return and(func, express, true);
|
||||
}
|
||||
|
||||
public final <T extends Serializable> FilterNode and(LambdaSupplier<T> func, FilterExpress express, boolean itemand) {
|
||||
return and(new FilterNode(LambdaSupplier.readColumn(func), express, itemand, func.get()));
|
||||
}
|
||||
|
||||
public final <T> FilterNode and(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return and(func, null, value);
|
||||
}
|
||||
|
||||
public final <T> FilterNode and(LambdaFunction<T, ?> func, FilterExpress express, Serializable value) {
|
||||
return and(func, express, true, value);
|
||||
}
|
||||
|
||||
public final <T> FilterNode and(LambdaFunction<T, ?> func, FilterExpress express, boolean itemand, Serializable value) {
|
||||
return and(new FilterNode(LambdaFunction.readColumn(func), express, itemand, value));
|
||||
}
|
||||
|
||||
public final FilterNode or(FilterNode node) {
|
||||
return any(node, true);
|
||||
}
|
||||
@@ -177,6 +201,30 @@ public class FilterNode { //FilterNode 不能实现Serializable接口, 否则
|
||||
return or(new FilterNode(column, express, itemand, value));
|
||||
}
|
||||
|
||||
public final <T extends Serializable> FilterNode or(LambdaSupplier<T> func) {
|
||||
return or(func, null);
|
||||
}
|
||||
|
||||
public final <T extends Serializable> FilterNode or(LambdaSupplier<T> func, FilterExpress express) {
|
||||
return or(func, express, true);
|
||||
}
|
||||
|
||||
public final <T extends Serializable> FilterNode or(LambdaSupplier<T> func, FilterExpress express, boolean itemand) {
|
||||
return or(new FilterNode(LambdaSupplier.readColumn(func), express, itemand, func.get()));
|
||||
}
|
||||
|
||||
public final <T> FilterNode or(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return or(func, null, value);
|
||||
}
|
||||
|
||||
public final <T> FilterNode or(LambdaFunction<T, ?> func, FilterExpress express, Serializable value) {
|
||||
return or(func, express, true, value);
|
||||
}
|
||||
|
||||
public final <T> FilterNode or(LambdaFunction<T, ?> func, FilterExpress express, boolean itemand, Serializable value) {
|
||||
return or(new FilterNode(LambdaFunction.readColumn(func), express, itemand, value));
|
||||
}
|
||||
|
||||
protected FilterNode any(FilterNode node, boolean signor) {
|
||||
if (this.readOnly) {
|
||||
throw new SourceException("FilterNode(" + this + ") is ReadOnly");
|
||||
@@ -347,6 +395,30 @@ public class FilterNode { //FilterNode 不能实现Serializable接口, 否则
|
||||
return new FilterNode(column, express, itemand, value);
|
||||
}
|
||||
|
||||
public static <T extends Serializable> FilterNode create(LambdaSupplier<T> func) {
|
||||
return create(func, null);
|
||||
}
|
||||
|
||||
public static <T extends Serializable> FilterNode create(LambdaSupplier<T> func, FilterExpress express) {
|
||||
return create(func, express, true);
|
||||
}
|
||||
|
||||
public static <T extends Serializable> FilterNode create(LambdaSupplier<T> func, FilterExpress express, boolean itemand) {
|
||||
return new FilterNode(LambdaSupplier.readColumn(func), express, itemand, func.get());
|
||||
}
|
||||
|
||||
public static <T> FilterNode create(LambdaFunction<T, ?> func, Serializable value) {
|
||||
return create(func, null, value);
|
||||
}
|
||||
|
||||
public static <T> FilterNode create(LambdaFunction<T, ?> func, FilterExpress express, Serializable value) {
|
||||
return create(func, express, true, value);
|
||||
}
|
||||
|
||||
public static <T> FilterNode create(LambdaFunction<T, ?> func, FilterExpress express, boolean itemand, Serializable value) {
|
||||
return new FilterNode(LambdaFunction.readColumn(func), express, itemand, value);
|
||||
}
|
||||
|
||||
@Deprecated(since = "2.8.0")
|
||||
public static FilterNode filter(String column, Serializable value) {
|
||||
return create(column, null, value);
|
||||
|
||||
36
src/main/java/org/redkale/util/LambdaFunction.java
Normal file
36
src/main/java/org/redkale/util/LambdaFunction.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Lambda的Function自定义类
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.8.0
|
||||
*
|
||||
* @param <T> 泛型
|
||||
* @param <R> 泛型
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface LambdaFunction<T, R> extends Function<T, R>, Serializable {
|
||||
|
||||
public static <T> String[] readColumns(LambdaFunction<T, ?>... funcs) {
|
||||
String[] columns = new String[funcs.length];
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
columns[i] = readColumn(funcs[i]);
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
|
||||
public static <T> String readColumn(LambdaFunction<T, ?> func) {
|
||||
return SerializedLambda.readColumn(func);
|
||||
}
|
||||
|
||||
}
|
||||
26
src/main/java/org/redkale/util/LambdaSupplier.java
Normal file
26
src/main/java/org/redkale/util/LambdaSupplier.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Lambda的Supplier自定义类
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.8.0
|
||||
*
|
||||
* @param <T> 泛型
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface LambdaSupplier<T extends Serializable> extends Supplier<T>, Serializable {
|
||||
|
||||
public static <T extends Serializable> String readColumn(LambdaSupplier<T> func) {
|
||||
return SerializedLambda.readColumn(func);
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,19 @@ public class SelectColumn implements Predicate<String> {
|
||||
// public static SelectColumn createIncludes(String... columns) {
|
||||
// return new SelectColumn(columns, false);
|
||||
// }
|
||||
//
|
||||
//
|
||||
/**
|
||||
* class中的字段名
|
||||
*
|
||||
* @param funcs 包含的字段名Lambda集合
|
||||
*
|
||||
* @return SelectColumn
|
||||
*/
|
||||
public static <T> SelectColumn includes(LambdaFunction<T, ?>... funcs) {
|
||||
return includes(LambdaFunction.readColumns(funcs));
|
||||
}
|
||||
|
||||
/**
|
||||
* class中的字段名
|
||||
*
|
||||
@@ -109,6 +122,8 @@ public class SelectColumn implements Predicate<String> {
|
||||
// public static SelectColumn createIncludes(String[] cols, String... columns) {
|
||||
// return new SelectColumn(Utility.append(cols, columns), false);
|
||||
// }
|
||||
//
|
||||
//
|
||||
/**
|
||||
* class中的字段名
|
||||
*
|
||||
@@ -133,6 +148,19 @@ public class SelectColumn implements Predicate<String> {
|
||||
// public static SelectColumn createExcludes(String... columns) {
|
||||
// return new SelectColumn(columns, true);
|
||||
// }
|
||||
//
|
||||
//
|
||||
/**
|
||||
* class中的字段名
|
||||
*
|
||||
* @param funcs 包含的字段名Lambda集合
|
||||
*
|
||||
* @return SelectColumn
|
||||
*/
|
||||
public static <T> SelectColumn excludes(LambdaFunction<T, ?>... funcs) {
|
||||
return excludes(LambdaFunction.readColumns(funcs));
|
||||
}
|
||||
|
||||
/**
|
||||
* class中的字段名
|
||||
*
|
||||
@@ -156,6 +184,8 @@ public class SelectColumn implements Predicate<String> {
|
||||
// public static SelectColumn createExcludes(String[] cols, String... columns) {
|
||||
// return new SelectColumn(Utility.append(cols, columns), true);
|
||||
// }
|
||||
//
|
||||
//
|
||||
/**
|
||||
*
|
||||
* class中的字段名
|
||||
|
||||
298
src/main/java/org/redkale/util/SerializedLambda.java
Normal file
298
src/main/java/org/redkale/util/SerializedLambda.java
Normal file
@@ -0,0 +1,298 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.invoke.MethodHandleInfo;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 完全复制java.lang.invoke.SerializedLambda类源码,必须保持字段信息一样
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.8.0
|
||||
*
|
||||
*/
|
||||
public class SerializedLambda implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 8025925345765570181L;
|
||||
|
||||
/**
|
||||
* The capturing class.
|
||||
*/
|
||||
private final Class<?> capturingClass;
|
||||
|
||||
/**
|
||||
* The functional interface class.
|
||||
*/
|
||||
private final String functionalInterfaceClass;
|
||||
|
||||
/**
|
||||
* The functional interface method name.
|
||||
*/
|
||||
private final String functionalInterfaceMethodName;
|
||||
|
||||
/**
|
||||
* The functional interface method signature.
|
||||
*/
|
||||
private final String functionalInterfaceMethodSignature;
|
||||
|
||||
/**
|
||||
* The implementation class.
|
||||
*/
|
||||
private final String implClass;
|
||||
|
||||
/**
|
||||
* The implementation method name.
|
||||
*/
|
||||
private final String implMethodName;
|
||||
|
||||
/**
|
||||
* The implementation method signature.
|
||||
*/
|
||||
private final String implMethodSignature;
|
||||
|
||||
/**
|
||||
* The implementation method kind.
|
||||
*/
|
||||
private final int implMethodKind;
|
||||
|
||||
/**
|
||||
* The instantiated method type.
|
||||
*/
|
||||
private final String instantiatedMethodType;
|
||||
|
||||
/**
|
||||
* The captured arguments.
|
||||
*/
|
||||
@SuppressWarnings("serial") // Not statically typed as Serializable
|
||||
private final Object[] capturedArgs;
|
||||
|
||||
/**
|
||||
* Create a {@code SerializedLambda} from the low-level information present
|
||||
* at the lambda factory site.
|
||||
*
|
||||
* @param capturingClass The class in which the lambda expression appears
|
||||
* @param functionalInterfaceClass Name, in slash-delimited form, of static
|
||||
* type of the returned lambda object
|
||||
* @param functionalInterfaceMethodName Name of the functional interface
|
||||
* method for the present at the
|
||||
* lambda factory site
|
||||
* @param functionalInterfaceMethodSignature Signature of the functional
|
||||
* interface method present at
|
||||
* the lambda factory site
|
||||
* @param implMethodKind Method handle kind for the implementation method
|
||||
* @param implClass Name, in slash-delimited form, for the class holding
|
||||
* the implementation method
|
||||
* @param implMethodName Name of the implementation method
|
||||
* @param implMethodSignature Signature of the implementation method
|
||||
* @param instantiatedMethodType The signature of the primary functional
|
||||
* interface method after type variables
|
||||
* are substituted with their instantiation
|
||||
* from the capture site
|
||||
* @param capturedArgs The dynamic arguments to the lambda factory site,
|
||||
* which represent variables captured by
|
||||
* the lambda
|
||||
*/
|
||||
public SerializedLambda(Class<?> capturingClass,
|
||||
String functionalInterfaceClass,
|
||||
String functionalInterfaceMethodName,
|
||||
String functionalInterfaceMethodSignature,
|
||||
int implMethodKind,
|
||||
String implClass,
|
||||
String implMethodName,
|
||||
String implMethodSignature,
|
||||
String instantiatedMethodType,
|
||||
Object[] capturedArgs) {
|
||||
this.capturingClass = capturingClass;
|
||||
this.functionalInterfaceClass = functionalInterfaceClass;
|
||||
this.functionalInterfaceMethodName = functionalInterfaceMethodName;
|
||||
this.functionalInterfaceMethodSignature = functionalInterfaceMethodSignature;
|
||||
this.implMethodKind = implMethodKind;
|
||||
this.implClass = implClass;
|
||||
this.implMethodName = implMethodName;
|
||||
this.implMethodSignature = implMethodSignature;
|
||||
this.instantiatedMethodType = instantiatedMethodType;
|
||||
this.capturedArgs = Objects.requireNonNull(capturedArgs).clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the class that captured this lambda.
|
||||
*
|
||||
* @return the name of the class that captured this lambda
|
||||
*/
|
||||
public String getCapturingClass() {
|
||||
return capturingClass.getName().replace('.', '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the invoked type to which this
|
||||
* lambda has been converted
|
||||
*
|
||||
* @return the name of the functional interface class to which
|
||||
* this lambda has been converted
|
||||
*/
|
||||
public String getFunctionalInterfaceClass() {
|
||||
return functionalInterfaceClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the primary method for the functional interface
|
||||
* to which this lambda has been converted.
|
||||
*
|
||||
* @return the name of the primary methods of the functional interface
|
||||
*/
|
||||
public String getFunctionalInterfaceMethodName() {
|
||||
return functionalInterfaceMethodName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the signature of the primary method for the functional
|
||||
* interface to which this lambda has been converted.
|
||||
*
|
||||
* @return the signature of the primary method of the functional
|
||||
* interface
|
||||
*/
|
||||
public String getFunctionalInterfaceMethodSignature() {
|
||||
return functionalInterfaceMethodSignature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the class containing the implementation
|
||||
* method.
|
||||
*
|
||||
* @return the name of the class containing the implementation
|
||||
* method
|
||||
*/
|
||||
public String getImplClass() {
|
||||
return implClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the implementation method.
|
||||
*
|
||||
* @return the name of the implementation method
|
||||
*/
|
||||
public String getImplMethodName() {
|
||||
return implMethodName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the signature of the implementation method.
|
||||
*
|
||||
* @return the signature of the implementation method
|
||||
*/
|
||||
public String getImplMethodSignature() {
|
||||
return implMethodSignature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the method handle kind (see {@link MethodHandleInfo}) of
|
||||
* the implementation method.
|
||||
*
|
||||
* @return the method handle kind of the implementation method
|
||||
*/
|
||||
public int getImplMethodKind() {
|
||||
return implMethodKind;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the signature of the primary functional interface method
|
||||
* after type variables are substituted with their instantiation
|
||||
* from the capture site.
|
||||
*
|
||||
* @return the signature of the primary functional interface method
|
||||
* after type variable processing
|
||||
*/
|
||||
public final String getInstantiatedMethodType() {
|
||||
return instantiatedMethodType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the count of dynamic arguments to the lambda capture site.
|
||||
*
|
||||
* @return the count of dynamic arguments to the lambda capture site
|
||||
*/
|
||||
public int getCapturedArgCount() {
|
||||
return capturedArgs.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a dynamic argument to the lambda capture site.
|
||||
*
|
||||
* @param i the argument to capture
|
||||
*
|
||||
* @return a dynamic argument to the lambda capture site
|
||||
*/
|
||||
public Object getCapturedArg(int i) {
|
||||
return capturedArgs[i];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String implKind = MethodHandleInfo.referenceKindToString(implMethodKind);
|
||||
return String.format("SerializedLambda[%s=%s, %s=%s.%s:%s, "
|
||||
+ "%s=%s %s.%s:%s, %s=%s, %s=%d]",
|
||||
"capturingClass", capturingClass,
|
||||
"functionalInterfaceMethod", functionalInterfaceClass,
|
||||
functionalInterfaceMethodName,
|
||||
functionalInterfaceMethodSignature,
|
||||
"implementation",
|
||||
implKind,
|
||||
implClass, implMethodName, implMethodSignature,
|
||||
"instantiatedMethodType", instantiatedMethodType,
|
||||
"numCaptured", capturedArgs.length);
|
||||
}
|
||||
|
||||
private static final ConcurrentHashMap<Class, SerializedLambda> cache = new ConcurrentHashMap();
|
||||
|
||||
public static <T> String readColumn(Serializable func) {
|
||||
return readFieldName(readLambda(func).getImplMethodName());
|
||||
}
|
||||
|
||||
public static SerializedLambda readLambda(Serializable func) {
|
||||
if (!func.getClass().isSynthetic()) { //必须是Lambda表达式的合成类
|
||||
throw new RedkaleException("Not a synthetic lambda class");
|
||||
}
|
||||
return cache.computeIfAbsent(func.getClass(), clazz -> {
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(out);
|
||||
oos.writeObject(func);
|
||||
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())) {
|
||||
@Override
|
||||
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
|
||||
Class<?> clazz = super.resolveClass(desc);
|
||||
return clazz == java.lang.invoke.SerializedLambda.class ? SerializedLambda.class : clazz;
|
||||
}
|
||||
};
|
||||
return (SerializedLambda) in.readObject();
|
||||
} catch (Exception e) {
|
||||
throw new RedkaleException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static String readFieldName(String methodName) {
|
||||
String name;
|
||||
if (methodName.startsWith("is")) {
|
||||
name = methodName.substring(2);
|
||||
} else if (methodName.startsWith("get") || methodName.startsWith("set")) {
|
||||
name = methodName.substring(3);
|
||||
} else {
|
||||
name = methodName;
|
||||
}
|
||||
if (name.length() < 2) {
|
||||
return name.toLowerCase(Locale.ENGLISH);
|
||||
} else if (Character.isUpperCase(name.charAt(1))) {
|
||||
return name;
|
||||
} else {
|
||||
return name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@ import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.BiFunction;
|
||||
import org.redkale.persistence.Id;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.persistence.*;
|
||||
import org.redkale.persistence.VirtualEntity;
|
||||
import org.redkale.source.*;
|
||||
|
||||
@@ -45,6 +45,9 @@ public class CacheTestBean {
|
||||
System.out.println(cache.find(null, FilterNode.create("name", FilterExpress.EQUAL, "BB")));
|
||||
System.out.println(cache.find(null, FilterNode.create("name", FilterExpress.IGNORECASEEQUAL, "BB")));
|
||||
System.out.println(cache.querySheet(null, null, FilterNode.create("name", FilterExpress.IGNORECASENOTLIKE, "B")));
|
||||
System.out.println(cache.find(null, FilterNode.create(CacheTestBean::getName, FilterExpress.EQUAL, "BB")));
|
||||
System.out.println(cache.find(null, FilterNode.create(CacheTestBean::getName, FilterExpress.IGNORECASEEQUAL, "BB")));
|
||||
System.out.println(cache.querySheet(null, null, FilterNode.create(CacheTestBean::getName, FilterExpress.IGNORECASENOTLIKE, "B")));
|
||||
}
|
||||
|
||||
public CacheTestBean() {
|
||||
|
||||
@@ -7,23 +7,14 @@ package org.redkale.test.source;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
import org.redkale.persistence.Cacheable;
|
||||
import org.redkale.persistence.Id;
|
||||
import org.redkale.persistence.VirtualEntity;
|
||||
import org.redkale.source.FilterNodeBean;
|
||||
import org.redkale.source.FilterExpress;
|
||||
import org.redkale.source.FilterColumn;
|
||||
import org.redkale.util.Sheet;
|
||||
import org.redkale.source.FilterBean;
|
||||
import org.redkale.source.Flipper;
|
||||
import org.redkale.source.EntityInfo;
|
||||
import org.redkale.source.FilterNode;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import org.redkale.convert.json.*;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.persistence.*;
|
||||
import org.redkale.persistence.VirtualEntity;
|
||||
import org.redkale.source.*;
|
||||
import org.redkale.util.Sheet;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -71,6 +62,8 @@ public class TestSourceCache {
|
||||
flipper.setSort("userid DESC, createtime DESC");
|
||||
final FilterNode node = FilterNode.create("userid", FilterExpress.GREATERTHAN, 1000).and("username", FilterExpress.LIKE, "用户");
|
||||
System.out.println("node = " + node);
|
||||
final FilterNode node2 = FilterNode.create(TestEntity::getUserid, FilterExpress.GREATERTHAN, 1000).and("username", FilterExpress.LIKE, "用户");
|
||||
Assertions.assertEquals(node.toString(), node2.toString());
|
||||
Sheet<TestEntity> sheet = info.getCache().querySheet(null, flipper, node);
|
||||
System.out.println(sheet);
|
||||
System.out.println(info.getCache().querySheet(null, flipper, FilterNodeBean.createFilterNode(new TestEntityBean(1000, "用户"))));
|
||||
|
||||
Reference in New Issue
Block a user