添加注解选择器

This commit is contained in:
huangchengxing 2022-07-05 13:39:07 +08:00
parent 2ecda1d245
commit 0659440cad

View File

@ -0,0 +1,81 @@
package cn.hutool.core.annotation;
/**
* 注解选择器指定两个注解选择其中一个返回<br>
* 该接口用于在{@link SyntheticAnnotation}中用于从一批相同的注解对象中筛选最终用于合成注解对象
*
* @author huangchengxing
*/
@FunctionalInterface
public interface SynthesizedAnnotationSelector {
/**
* 返回距离根对象更近的注解当距离一样时优先返回旧注解
*/
SynthesizedAnnotationSelector NEAREST_AND_OLDEST_PRIORITY = new NearestAndOldestPrioritySelector();
/**
* 返回距离根对象更近的注解当距离一样时优先返回新注解
*/
SynthesizedAnnotationSelector NEAREST_AND_NEWEST_PRIORITY = new NearestAndNewestPrioritySelector();
/**
* 返回距离根对象更远的注解当距离一样时优先返回旧注解
*/
SynthesizedAnnotationSelector FARTHEST_AND_OLDEST_PRIORITY = new FarthestAndOldestPrioritySelector();
/**
* 返回距离根对象更远的注解当距离一样时优先返回新注解
*/
SynthesizedAnnotationSelector FARTHEST_AND_NEWEST_PRIORITY = new FarthestAndNewestPrioritySelector();
/**
* 比较两个被合成的注解选择其中的一个并返回
*
* @param oldAnnotation 已存在的注解该参数不允许为空
* @param newAnnotation 新获取的注解该参数不允许为空
* @return 被合成的注解
*/
<T, A extends SynthesizedAnnotation<T>> A choose(A oldAnnotation, A newAnnotation);
/**
* 返回距离根对象更近的注解当距离一样时优先返回旧注解
*/
class NearestAndOldestPrioritySelector implements SynthesizedAnnotationSelector {
@Override
public <T, A extends SynthesizedAnnotation<T>> A choose(A oldAnnotation, A newAnnotation) {
return newAnnotation.getVerticalDistance() < oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation;
}
}
/**
* 返回距离根对象更近的注解当距离一样时优先返回新注解
*/
class NearestAndNewestPrioritySelector implements SynthesizedAnnotationSelector {
@Override
public <T, A extends SynthesizedAnnotation<T>> A choose(A oldAnnotation, A newAnnotation) {
return newAnnotation.getVerticalDistance() <= oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation;
}
}
/**
* 返回距离根对象更远的注解当距离一样时优先返回旧注解
*/
class FarthestAndOldestPrioritySelector implements SynthesizedAnnotationSelector {
@Override
public <T, A extends SynthesizedAnnotation<T>> A choose(A oldAnnotation, A newAnnotation) {
return newAnnotation.getVerticalDistance() > oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation;
}
}
/**
* 返回距离根对象更远的注解当距离一样时优先返回新注解
*/
class FarthestAndNewestPrioritySelector implements SynthesizedAnnotationSelector {
@Override
public <T, A extends SynthesizedAnnotation<T>> A choose(A oldAnnotation, A newAnnotation) {
return newAnnotation.getVerticalDistance() >= oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation;
}
}
}