From 85939e4fc4b47d23f8158aba0be8148b8f811a3e Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 21 Oct 2024 00:26:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20ImmutableObject=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20com.google.errorprone.annotations.Immutabl?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/annotation/ImmutableObject.java | 48 ------------------- .../zhouxy/plusone/commons/math/Interval.java | 14 +++--- .../plusone/commons/time/YearQuarter.java | 18 +++---- 3 files changed, 18 insertions(+), 62 deletions(-) delete mode 100644 src/main/java/xyz/zhouxy/plusone/commons/annotation/ImmutableObject.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/annotation/ImmutableObject.java b/src/main/java/xyz/zhouxy/plusone/commons/annotation/ImmutableObject.java deleted file mode 100644 index 5a8c51c..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/annotation/ImmutableObject.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package xyz.zhouxy.plusone.commons.annotation; - -import java.lang.annotation.Target; - -import com.google.common.annotations.Beta; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -/** - * 标记不可变对象。根据约定,该对象的字段只能是不可变对象或基础数据类型,且都是只读的。如: - *
@ImmutableObject
- * class Foo {
- *     private final @Getter int intVal;
- *     private final @Getter String stringVal;
- *     private final @Getter LocalDate dateVal;
- *     private final @Getter BigDecimal decimalVal;
- * }
- * 
- *

- * 如 guava 的 ImmutableList、ImmutableMap 等不可变集合,不属于此,因为其不要求包含的元素本身是不可变的。 - *

- * - * @author ZhouXY - * @since 0.1.0 - */ -@Beta -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -public @interface ImmutableObject { -} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/math/Interval.java b/src/main/java/xyz/zhouxy/plusone/commons/math/Interval.java index dbd3192..aed22e9 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/math/Interval.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/math/Interval.java @@ -21,9 +21,11 @@ import java.util.Optional; import javax.annotation.Nonnull; import com.google.common.base.Preconditions; +import com.google.errorprone.annotations.Immutable; import xyz.zhouxy.plusone.commons.util.Numbers; +@Immutable public class Interval> { @Nonnull private final IntervalType intervalType; @@ -50,29 +52,29 @@ public class Interval> { } @Nonnull - public IntervalType getIntervalType() { + public final IntervalType getIntervalType() { return intervalType; } @Nonnull - public Optional getLowerBound() { + public final Optional getLowerBound() { return Optional.ofNullable(lowerBound); } @Nonnull - public Optional getUpperBound() { + public final Optional getUpperBound() { return Optional.ofNullable(upperBound); } - public boolean isLeftClosed() { + public final boolean isLeftClosed() { return this.intervalType.isLeftClosed(); } - public boolean isRightClosed() { + public final boolean isRightClosed() { return this.intervalType.isRightClosed(); } - public boolean validValue(@Nonnull T value) { + public final boolean validValue(@Nonnull T value) { return Numbers.between(value, this.lowerBound, this.upperBound, this.intervalType); } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java b/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java index c4c2a8d..7fc15a3 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java @@ -29,12 +29,14 @@ import java.util.Objects; import javax.annotation.Nonnull; import com.google.common.base.Preconditions; +import com.google.errorprone.annotations.Immutable; /** * 表示年份与季度 - * + * * @author zhouxy */ +@Immutable public final class YearQuarter implements Comparable, Serializable { private static final long serialVersionUID = 3804145964419489753L; @@ -57,7 +59,7 @@ public final class YearQuarter implements Comparable, Serializable /** * 根据指定年份与季度,创建 {@link YearQuarter} 实例 - * + * * @param year 年份 * @param quarter 季度 * @return {@link YearQuarter} 实例 @@ -68,7 +70,7 @@ public final class YearQuarter implements Comparable, Serializable /** * 根据指定年份与季度,创建 {@link YearQuarter} 实例 - * + * * @param year 年份 * @param quarter 季度 * @return {@link YearQuarter} 实例 @@ -79,7 +81,7 @@ public final class YearQuarter implements Comparable, Serializable /** * 根据指定日期,判断日期所在的年份与季度,创建 {@link YearQuarter} 实例 - * + * * @param date 日期 * @return {@link YearQuarter} 实例 */ @@ -89,7 +91,7 @@ public final class YearQuarter implements Comparable, Serializable /** * 根据指定日期,判断日期所在的年份与季度,创建 {@link YearQuarter} 实例 - * + * * @param date 日期 * @return {@link YearQuarter} 实例 */ @@ -103,7 +105,7 @@ public final class YearQuarter implements Comparable, Serializable /** * 根据指定日期,判断日期所在的年份与季度,创建 {@link YearQuarter} 实例 - * + * * @param date 日期 * @return {@link YearQuarter} 实例 */ @@ -113,7 +115,7 @@ public final class YearQuarter implements Comparable, Serializable /** * 根据指定年月,判断其所在的年份与季度,创建 {@link YearQuarter} 实例 - * + * * @param yearMonth 年月 * @return {@link YearQuarter} 实例 */ @@ -238,7 +240,7 @@ public final class YearQuarter implements Comparable, Serializable /** * 返回 {@link YearQuarter} 的字符串表示形式,如 "2024 Q3" - * + * * @return {@link YearQuarter} 的字符串表示形式 */ @Override