feat: 优化链式调用,支持reduce操作和存在性判定
This commit is contained in:
parent
dce7f7a63f
commit
7ac9fdd8e3
@ -53,6 +53,15 @@ public interface BeanPropertyAnnotations {
|
||||
*/
|
||||
<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进行处理,取得所有注解并自动判断复数形式
|
||||
*
|
||||
@ -80,6 +89,22 @@ public interface BeanPropertyAnnotations {
|
||||
*/
|
||||
boolean last();
|
||||
|
||||
/**
|
||||
* 设置缓存的值
|
||||
*
|
||||
* @param value 值
|
||||
* @param <T> 值泛型
|
||||
*/
|
||||
<T> BeanPropertyAnnotations value(T value);
|
||||
|
||||
/**
|
||||
* 返回上一步的值
|
||||
*
|
||||
* @param <T> 泛型
|
||||
* @return 结果
|
||||
*/
|
||||
<T> T value();
|
||||
|
||||
/**
|
||||
* 返回空的运行链
|
||||
*
|
||||
|
@ -31,6 +31,7 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
|
||||
private final EmptyAnnotationChain<? extends Annotation, ?> emptyChain = new EmptyAnnotationChain<>(this);
|
||||
private final EmptyAnnotationBatchChain<? extends Annotation, ?> emptyBatchChain = new EmptyAnnotationBatchChain<>(this);
|
||||
private boolean last;
|
||||
private Object value;
|
||||
|
||||
SimpleBeanPropertyAnnotations(AnnotatedElement field) {
|
||||
this.field = field;
|
||||
@ -68,6 +69,7 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("Duplicates")
|
||||
public <A extends Annotation> BeanPropertyAnnotationChain<A, MergedAnnotation<A>> as(Class<A> annotationType) {
|
||||
if (null != field && !this.last) {
|
||||
MergedAnnotation<A> annotation = this.annotations.get(annotationType);
|
||||
@ -78,6 +80,24 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
|
||||
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进行处理,取得所有注解并自动判断复数形式
|
||||
*
|
||||
@ -129,6 +149,27 @@ public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
|
||||
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 结果
|
||||
*/
|
||||
@Override
|
||||
|
||||
public <A extends Annotation, E> BeanPropertyAnnotationBatchChain<A, E> batchEmpty() {
|
||||
return cast(emptyBatchChain);
|
||||
}
|
||||
|
@ -24,7 +24,16 @@ public interface AnnotationChainSupport {
|
||||
BeanPropertyAnnotations or();
|
||||
|
||||
/**
|
||||
* 主动停止操作,会清空前面的状态
|
||||
* 当前值是否存在
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
void end();
|
||||
boolean isPresent();
|
||||
|
||||
/**
|
||||
* 主动停止操作,会清空前面的状态
|
||||
*
|
||||
* @return 结束状态,true则是以值结束,false以空结束
|
||||
*/
|
||||
boolean end();
|
||||
}
|
||||
|
@ -64,12 +64,19 @@ public interface BeanPropertyAnnotationBatchChain<A extends Annotation, T> exten
|
||||
*/
|
||||
BeanPropertyAnnotationBatchChain<A, T> empty(Runnable fallback);
|
||||
|
||||
/**
|
||||
* 将多值合并为单值
|
||||
*
|
||||
* @param reducer 合并器
|
||||
* @param <R> 返回泛型
|
||||
* @return 结果
|
||||
*/
|
||||
<R> BeanPropertyAnnotationChain<A, R> reduce(Function<List<T>, R> reducer);
|
||||
|
||||
/**
|
||||
* 获取所有结果为list
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
List<A> synthesize();
|
||||
|
||||
|
||||
}
|
||||
|
@ -70,4 +70,10 @@ public interface BeanPropertyAnnotationChain<A extends Annotation, T> extends An
|
||||
*/
|
||||
A synthesize();
|
||||
|
||||
/**
|
||||
* 获取缓存的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
T get();
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import lombok.RequiredArgsConstructor;
|
||||
* @author wangyu
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class BasicAnnotationChainSupport implements AnnotationChainSupport {
|
||||
abstract class BasicAnnotationChainSupport implements AnnotationChainSupport {
|
||||
|
||||
// 父级
|
||||
protected final BeanPropertyAnnotations parent;
|
||||
@ -22,7 +22,7 @@ public class BasicAnnotationChainSupport implements AnnotationChainSupport {
|
||||
*/
|
||||
@Override
|
||||
public BeanPropertyAnnotations and() {
|
||||
return parent.last(false);
|
||||
return cache().last(false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -32,14 +32,23 @@ public class BasicAnnotationChainSupport implements AnnotationChainSupport {
|
||||
*/
|
||||
@Override
|
||||
public BeanPropertyAnnotations or() {
|
||||
return parent;
|
||||
return cache();
|
||||
}
|
||||
|
||||
/**
|
||||
* 主动停止操作,会清空前面的状态
|
||||
*/
|
||||
@Override
|
||||
public void end() {
|
||||
parent.last(false);
|
||||
public boolean end() {
|
||||
boolean result = isPresent();
|
||||
parent.last(false).value(null);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存值逻辑
|
||||
*
|
||||
* @return 父亲
|
||||
*/
|
||||
protected abstract BeanPropertyAnnotations cache();
|
||||
}
|
||||
|
@ -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.chain.BeanPropertyAnnotationBatchChain;
|
||||
import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationChain;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collections;
|
||||
@ -94,6 +95,27 @@ public class EmptyAnnotationBatchChain<A extends Annotation, T> extends BasicAnn
|
||||
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
|
||||
*
|
||||
@ -104,4 +126,14 @@ public class EmptyAnnotationBatchChain<A extends Annotation, T> extends BasicAnn
|
||||
parent.last(false);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存值逻辑
|
||||
*
|
||||
* @return 父亲
|
||||
*/
|
||||
@Override
|
||||
protected BeanPropertyAnnotations cache() {
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
|
@ -93,6 +93,16 @@ public class EmptyAnnotationChain<A extends Annotation, T> extends BasicAnnotati
|
||||
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() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前数据
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public T get() {
|
||||
// 从空的占位访问,访问上次的结果
|
||||
return parent.value();
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存值逻辑
|
||||
*
|
||||
* @return 父亲
|
||||
*/
|
||||
@Override
|
||||
protected BeanPropertyAnnotations cache() {
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
|
@ -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.chain.BeanPropertyAnnotationBatchChain;
|
||||
import dev.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationChain;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
@ -121,6 +122,28 @@ public class SimpleAnnotationBatchChain<A extends Annotation, T> extends BasicAn
|
||||
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
|
||||
*
|
||||
@ -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)
|
||||
.map(MergedAnnotation::synthesize).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存值逻辑
|
||||
*
|
||||
* @return 父亲
|
||||
*/
|
||||
@Override
|
||||
protected BeanPropertyAnnotations cache() {
|
||||
return parent.value(values);
|
||||
}
|
||||
}
|
||||
|
@ -111,6 +111,16 @@ public class SimpleAnnotationChain<A extends Annotation, T> extends BasicAnnotat
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user