|
@@ -23,9 +23,10 @@ import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Set;
|
|
|
import java.util.function.BiConsumer;
|
|
|
-import java.util.function.BiFunction;
|
|
|
+import java.util.function.BiPredicate;
|
|
|
import java.util.function.Consumer;
|
|
|
import java.util.function.Function;
|
|
|
+import java.util.function.Predicate;
|
|
|
import java.util.function.Supplier;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.Stream;
|
|
@@ -35,7 +36,6 @@ import com.chelvc.framework.common.function.Setter;
|
|
|
import com.chelvc.framework.common.model.Modification;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.google.common.collect.Maps;
|
|
|
-import com.google.common.collect.Sets;
|
|
|
import io.protostuff.LinkedBuffer;
|
|
|
import io.protostuff.ProtobufIOUtil;
|
|
|
import io.protostuff.Schema;
|
|
@@ -497,11 +497,11 @@ public final class ObjectUtils {
|
|
|
/**
|
|
|
* 查找对象字段
|
|
|
*
|
|
|
- * @param clazz 对象
|
|
|
- * @param function 字段回调函数
|
|
|
+ * @param clazz 对象
|
|
|
+ * @param filter 字段过滤函数
|
|
|
* @return 字段实例
|
|
|
*/
|
|
|
- public static Field findClassField(@NonNull Class<?> clazz, @NonNull Function<Field, Boolean> function) {
|
|
|
+ public static Field findClassField(@NonNull Class<?> clazz, @NonNull Predicate<Field> filter) {
|
|
|
if (!Modifier.isInterface(clazz.getModifiers())) {
|
|
|
do {
|
|
|
Field[] fields = clazz.getDeclaredFields();
|
|
@@ -511,7 +511,7 @@ public final class ObjectUtils {
|
|
|
if (!field.isSynthetic() && ((enumerable && field.isEnumConstant())
|
|
|
|| (!enumerable && !Modifier.isStatic(field.getModifiers())))) {
|
|
|
field.setAccessible(true);
|
|
|
- if (Boolean.TRUE.equals(function.apply(field))) {
|
|
|
+ if (filter.test(field)) {
|
|
|
return field;
|
|
|
}
|
|
|
}
|
|
@@ -1414,13 +1414,25 @@ public final class ObjectUtils {
|
|
|
/**
|
|
|
* 获取两个对象实例不同属性值
|
|
|
*
|
|
|
- * @param <T> 数据类型
|
|
|
- * @param object 对象实例
|
|
|
- * @param other 对象实例
|
|
|
- * @param excludes 需要排除的属性名称集合
|
|
|
+ * @param <T> 数据类型
|
|
|
+ * @param object 对象实例
|
|
|
+ * @param other 对象实例
|
|
|
* @return 不同属性值
|
|
|
*/
|
|
|
- public static <T> List<Modification> compare(T object, T other, @NonNull String... excludes) {
|
|
|
+ public static <T> List<Modification> compare(T object, T other) {
|
|
|
+ return compare(object, other, field -> true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取两个对象实例不同属性值
|
|
|
+ *
|
|
|
+ * @param <T> 数据类型
|
|
|
+ * @param object 对象实例
|
|
|
+ * @param other 对象实例
|
|
|
+ * @param filter 字段过滤函数
|
|
|
+ * @return 不同属性值
|
|
|
+ */
|
|
|
+ public static <T> List<Modification> compare(T object, T other, @NonNull Predicate<Field> filter) {
|
|
|
if (object == null || other == null) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
@@ -1428,34 +1440,29 @@ public final class ObjectUtils {
|
|
|
if (isEmpty(fields)) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
- List<Modification> modifications = Lists.newArrayListWithCapacity(fields.size());
|
|
|
- Set<String> _excludes = excludes.length == 0 ? Collections.emptySet() : Sets.newHashSet(excludes);
|
|
|
- fields.forEach((name, field) -> {
|
|
|
- if (_excludes.isEmpty() || !_excludes.contains(name)) {
|
|
|
- Object before = getObjectValue(object, field);
|
|
|
- Object after = getObjectValue(other, field);
|
|
|
- if (!equals(before, after)) {
|
|
|
- modifications.add(Modification.builder().name(name).before(before).after(after).build());
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- return modifications;
|
|
|
+ return fields.entrySet().stream().filter(entry -> filter.test(entry.getValue())).map(entry -> {
|
|
|
+ String name = entry.getKey();
|
|
|
+ Field field = entry.getValue();
|
|
|
+ Object before = getObjectValue(object, field);
|
|
|
+ Object after = getObjectValue(other, field);
|
|
|
+ return equals(before, after) ? null : Modification.builder().name(name).before(before).after(after).build();
|
|
|
+ }).filter(Objects::nonNull).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 循环获取对象
|
|
|
*
|
|
|
* @param supplier 对象提供函数
|
|
|
- * @param function 对象处理函数(返回false则中断循环)
|
|
|
+ * @param filter 对象过滤函数(返回false则中断循环)
|
|
|
* @param <T> 对象类型
|
|
|
* @return 对象实例
|
|
|
*/
|
|
|
- public static <T> T loop(@NonNull Supplier<T> supplier, @NonNull BiFunction<Integer, T, Boolean> function) {
|
|
|
+ public static <T> T loop(@NonNull Supplier<T> supplier, @NonNull BiPredicate<Integer, T> filter) {
|
|
|
T t;
|
|
|
int count = 1;
|
|
|
do {
|
|
|
t = supplier.get();
|
|
|
- } while (Boolean.TRUE.equals(function.apply(count++, t)));
|
|
|
+ } while (filter.test(count++, t));
|
|
|
return t;
|
|
|
}
|
|
|
|