feat: 优化链式调用,支持reduce操作和存在性判定

This commit is contained in:
wangyu 2024-07-16 16:17:14 +08:00
parent dce7f7a63f
commit 7ac9fdd8e3
10 changed files with 232 additions and 10 deletions

View File

@ -53,6 +53,15 @@ public interface BeanPropertyAnnotations {
*/ */
<A extends Annotation> BeanPropertyAnnotationChain<A, MergedAnnotation<A>> as(Class<A> annotationType); <A extends Annotation> BeanPropertyAnnotationChain<A, MergedAnnotation<A>> as(Class<A> annotationType);
/**
* 判断某个注解是否存在存在则交给consumer进行处理仅取得找到的第一个注解
*
* @param annotationType 注解类型
* @param <A> 泛型
* @return 结果
*/
<A extends Annotation> BeanPropertyAnnotationChain<A, MergedAnnotation<A>> as(String annotationType);
/** /**
* 判断某个注解是否存在存在则交给consumer进行处理取得所有注解并自动判断复数形式 * 判断某个注解是否存在存在则交给consumer进行处理取得所有注解并自动判断复数形式
* *
@ -80,6 +89,22 @@ public interface BeanPropertyAnnotations {
*/ */
boolean last(); boolean last();
/**
* 设置缓存的值
*
* @param value
* @param <T> 值泛型
*/
<T> BeanPropertyAnnotations value(T value);
/**
* 返回上一步的值
*
* @param <T> 泛型
* @return 结果
*/
<T> T value();
/** /**
* 返回空的运行链 * 返回空的运行链
* *

View File

@ -31,6 +31,7 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
private final EmptyAnnotationChain<? extends Annotation, ?> emptyChain = new EmptyAnnotationChain<>(this); private final EmptyAnnotationChain<? extends Annotation, ?> emptyChain = new EmptyAnnotationChain<>(this);
private final EmptyAnnotationBatchChain<? extends Annotation, ?> emptyBatchChain = new EmptyAnnotationBatchChain<>(this); private final EmptyAnnotationBatchChain<? extends Annotation, ?> emptyBatchChain = new EmptyAnnotationBatchChain<>(this);
private boolean last; private boolean last;
private Object value;
SimpleBeanPropertyAnnotations(AnnotatedElement field) { SimpleBeanPropertyAnnotations(AnnotatedElement field) {
this.field = field; this.field = field;
@ -68,6 +69,7 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
* @return 结果 * @return 结果
*/ */
@Override @Override
@SuppressWarnings("Duplicates")
public <A extends Annotation> BeanPropertyAnnotationChain<A, MergedAnnotation<A>> as(Class<A> annotationType) { public <A extends Annotation> BeanPropertyAnnotationChain<A, MergedAnnotation<A>> as(Class<A> annotationType) {
if (null != field && !this.last) { if (null != field && !this.last) {
MergedAnnotation<A> annotation = this.annotations.get(annotationType); MergedAnnotation<A> annotation = this.annotations.get(annotationType);
@ -78,6 +80,24 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
return empty(); return empty();
} }
/**
* 判断某个注解是否存在存在则交给consumer进行处理仅取得找到的第一个注解
*
* @param annotationType 注解类型
* @return 结果
*/
@Override
@SuppressWarnings("Duplicates")
public <A extends Annotation> BeanPropertyAnnotationChain<A, MergedAnnotation<A>> as(String annotationType) {
if (null != field && !this.last) {
MergedAnnotation<A> annotation = this.annotations.get(annotationType);
if (annotation.isPresent()) {
return new SimpleAnnotationChain<>(this.last(true), annotation);
}
}
return empty();
}
/** /**
* 判断某个注解是否存在存在则交给consumer进行处理取得所有注解并自动判断复数形式 * 判断某个注解是否存在存在则交给consumer进行处理取得所有注解并自动判断复数形式
* *
@ -129,6 +149,27 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
return this.last; return this.last;
} }
/**
* 设置缓存的值
*
* @param value
*/
@Override
public <T> BeanPropertyAnnotations value(T value) {
this.value = value;
return this;
}
/**
* 返回上一步的值
*
* @return 结果
*/
@Override
public <T> T value() {
return cast(value);
}
/** /**
* 返回空的运行链 * 返回空的运行链
* *
@ -145,7 +186,6 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
* @return 结果 * @return 结果
*/ */
@Override @Override
public <A extends Annotation, E> BeanPropertyAnnotationBatchChain<A, E> batchEmpty() { public <A extends Annotation, E> BeanPropertyAnnotationBatchChain<A, E> batchEmpty() {
return cast(emptyBatchChain); return cast(emptyBatchChain);
} }

View File

@ -24,7 +24,16 @@ public interface AnnotationChainSupport {
BeanPropertyAnnotations or(); BeanPropertyAnnotations or();
/** /**
* 主动停止操作会清空前面的状态 * 当前值是否存在
*
* @return 结果
*/ */
void end(); boolean isPresent();
/**
* 主动停止操作会清空前面的状态
*
* @return 结束状态true则是以值结束false以空结束
*/
boolean end();
} }

View File

@ -64,12 +64,19 @@ public interface BeanPropertyAnnotationBatchChain<A extends Annotation, T> exten
*/ */
BeanPropertyAnnotationBatchChain<A, T> empty(Runnable fallback); BeanPropertyAnnotationBatchChain<A, T> empty(Runnable fallback);
/**
* 将多值合并为单值
*
* @param reducer 合并器
* @param <R> 返回泛型
* @return 结果
*/
<R> BeanPropertyAnnotationChain<A, R> reduce(Function<List<T>, R> reducer);
/** /**
* 获取所有结果为list * 获取所有结果为list
* *
* @return 结果 * @return 结果
*/ */
List<A> synthesize(); List<A> synthesize();
} }

View File

@ -70,4 +70,10 @@ public interface BeanPropertyAnnotationChain<A extends Annotation, T> extends An
*/ */
A synthesize(); A synthesize();
/**
* 获取缓存的值
*
* @return 结果
*/
T get();
} }

View File

@ -10,7 +10,7 @@ import lombok.RequiredArgsConstructor;
* @author wangyu * @author wangyu
*/ */
@RequiredArgsConstructor @RequiredArgsConstructor
public class BasicAnnotationChainSupport implements AnnotationChainSupport { abstract class BasicAnnotationChainSupport implements AnnotationChainSupport {
// 父级 // 父级
protected final BeanPropertyAnnotations parent; protected final BeanPropertyAnnotations parent;
@ -22,7 +22,7 @@ public class BasicAnnotationChainSupport implements AnnotationChainSupport {
*/ */
@Override @Override
public BeanPropertyAnnotations and() { public BeanPropertyAnnotations and() {
return parent.last(false); return cache().last(false);
} }
/** /**
@ -32,14 +32,23 @@ public class BasicAnnotationChainSupport implements AnnotationChainSupport {
*/ */
@Override @Override
public BeanPropertyAnnotations or() { public BeanPropertyAnnotations or() {
return parent; return cache();
} }
/** /**
* 主动停止操作会清空前面的状态 * 主动停止操作会清空前面的状态
*/ */
@Override @Override
public void end() { public boolean end() {
parent.last(false); boolean result = isPresent();
parent.last(false).value(null);
return result;
} }
/**
* 缓存值逻辑
*
* @return 父亲
*/
protected abstract BeanPropertyAnnotations cache();
} }

View File

@ -2,6 +2,7 @@ package dev.flyfish.framework.beans.meta.parser.chain.impl;
import dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations; import dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationBatchChain; import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationBatchChain;
import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationChain;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Collections; import java.util.Collections;
@ -94,6 +95,27 @@ public class EmptyAnnotationBatchChain<A extends Annotation, T> extends BasicAnn
return this; return this;
} }
/**
* 将多值合并为单值
*
* @param reducer 合并器
* @return 结果
*/
@Override
public <R> BeanPropertyAnnotationChain<A, R> reduce(Function<List<T>, R> reducer) {
return parent.empty();
}
/**
* 是否存在值
*
* @return 结果
*/
@Override
public boolean isPresent() {
return false;
}
/** /**
* 获取所有结果为list * 获取所有结果为list
* *
@ -104,4 +126,14 @@ public class EmptyAnnotationBatchChain<A extends Annotation, T> extends BasicAnn
parent.last(false); parent.last(false);
return Collections.emptyList(); return Collections.emptyList();
} }
/**
* 缓存值逻辑
*
* @return 父亲
*/
@Override
protected BeanPropertyAnnotations cache() {
return parent;
}
} }

View File

@ -93,6 +93,16 @@ public class EmptyAnnotationChain<A extends Annotation, T> extends BasicAnnotati
return this; return this;
} }
/**
* 当前值是否存在
*
* @return 结果
*/
@Override
public boolean isPresent() {
return false;
}
/** /**
* 直接获取结果 * 直接获取结果
* *
@ -102,4 +112,25 @@ public class EmptyAnnotationChain<A extends Annotation, T> extends BasicAnnotati
public A synthesize() { public A synthesize() {
return null; return null;
} }
/**
* 获取当前数据
*
* @return 结果
*/
@Override
public T get() {
// 从空的占位访问访问上次的结果
return parent.value();
}
/**
* 缓存值逻辑
*
* @return 父亲
*/
@Override
protected BeanPropertyAnnotations cache() {
return parent;
}
} }

View File

@ -2,6 +2,7 @@ package dev.flyfish.framework.beans.meta.parser.chain.impl;
import dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations; import dev.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationBatchChain; import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationBatchChain;
import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationChain;
import org.springframework.core.annotation.MergedAnnotation; import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -121,6 +122,28 @@ public class SimpleAnnotationBatchChain<A extends Annotation, T> extends BasicAn
return this; return this;
} }
/**
* 将多值合并为单值
*
* @param reducer 合并器
* @return 结果
*/
@Override
public <R> BeanPropertyAnnotationChain<A, R> reduce(Function<List<T>, R> reducer) {
R reduced = reducer.apply(values);
return new SimpleAnnotationChain<>(parent, reduced);
}
/**
* 是否存在值
*
* @return 结果
*/
@Override
public boolean isPresent() {
return !CollectionUtils.isEmpty(values);
}
/** /**
* 获取所有结果为list * 获取所有结果为list
* *
@ -132,4 +155,14 @@ public class SimpleAnnotationBatchChain<A extends Annotation, T> extends BasicAn
return mapped ? Collections.emptyList() : this.values.stream().map(item -> (MergedAnnotation<A>) item) return mapped ? Collections.emptyList() : this.values.stream().map(item -> (MergedAnnotation<A>) item)
.map(MergedAnnotation::synthesize).collect(Collectors.toList()); .map(MergedAnnotation::synthesize).collect(Collectors.toList());
} }
/**
* 缓存值逻辑
*
* @return 父亲
*/
@Override
protected BeanPropertyAnnotations cache() {
return parent.value(values);
}
} }

View File

@ -111,6 +111,16 @@ public class SimpleAnnotationChain<A extends Annotation, T> extends BasicAnnotat
return this; return this;
} }
/**
* 当前值是否存在
*
* @return 结果
*/
@Override
public boolean isPresent() {
return null != value;
}
/** /**
* 直接获取结果 * 直接获取结果
* *
@ -122,4 +132,24 @@ public class SimpleAnnotationChain<A extends Annotation, T> extends BasicAnnotat
parent.last(false); parent.last(false);
return value instanceof MergedAnnotation ? ((MergedAnnotation<A>) value).synthesize() : null; return value instanceof MergedAnnotation ? ((MergedAnnotation<A>) value).synthesize() : null;
} }
/**
* 获取缓存的值
*
* @return 结果
*/
@Override
public T get() {
return value;
}
/**
* 缓存值逻辑
*
* @return 父亲
*/
@Override
protected BeanPropertyAnnotations cache() {
return parent.value(value);
}
} }