From 0659440cad3e6daf04f5289431149dc2851e283b Mon Sep 17 00:00:00 2001 From: huangchengxing <841396397@qq.com> Date: Tue, 5 Jul 2022 13:39:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E8=A7=A3=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SynthesizedAnnotationSelector.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationSelector.java diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationSelector.java b/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationSelector.java new file mode 100644 index 000000000..65b76cf61 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/SynthesizedAnnotationSelector.java @@ -0,0 +1,81 @@ +package cn.hutool.core.annotation; + +/** + * 注解选择器,指定两个注解,选择其中一个返回。
+ * 该接口用于在{@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 被合成的注解 + */ + > A choose(A oldAnnotation, A newAnnotation); + + /** + * 返回距离根对象更近的注解,当距离一样时优先返回旧注解 + */ + class NearestAndOldestPrioritySelector implements SynthesizedAnnotationSelector { + @Override + public > A choose(A oldAnnotation, A newAnnotation) { + return newAnnotation.getVerticalDistance() < oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation; + } + } + + /** + * 返回距离根对象更近的注解,当距离一样时优先返回新注解 + */ + class NearestAndNewestPrioritySelector implements SynthesizedAnnotationSelector { + @Override + public > A choose(A oldAnnotation, A newAnnotation) { + return newAnnotation.getVerticalDistance() <= oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation; + } + } + + /** + * 返回距离根对象更远的注解,当距离一样时优先返回旧注解 + */ + class FarthestAndOldestPrioritySelector implements SynthesizedAnnotationSelector { + @Override + public > A choose(A oldAnnotation, A newAnnotation) { + return newAnnotation.getVerticalDistance() > oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation; + } + } + + /** + * 返回距离根对象更远的注解,当距离一样时优先返回新注解 + */ + class FarthestAndNewestPrioritySelector implements SynthesizedAnnotationSelector { + @Override + public > A choose(A oldAnnotation, A newAnnotation) { + return newAnnotation.getVerticalDistance() >= oldAnnotation.getVerticalDistance() ? newAnnotation : oldAnnotation; + } + } + +}