From 4b9b38eeb98df7c0a80b015b5f92b30eb5270fef Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 16 Dec 2024 20:02:28 +0800 Subject: [PATCH] =?UTF-8?q?Quarter=20=E5=AE=9E=E7=8E=B0=20IWithIntCode?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/time/Quarter.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/time/Quarter.java b/src/main/java/xyz/zhouxy/plusone/commons/time/Quarter.java index 4af922a..cbc38e1 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/time/Quarter.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/time/Quarter.java @@ -23,13 +23,15 @@ import java.time.temporal.ChronoField; import com.google.common.collect.Range; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; +import xyz.zhouxy.plusone.commons.base.IWithIntCode; +import xyz.zhouxy.plusone.commons.util.AssertTools; /** * 季度 * * @author ZhouXY */ -public enum Quarter { +public enum Quarter implements IWithIntCode { /** 第一季度 */ Q1(1), /** 第二季度 */ @@ -107,10 +109,7 @@ public enum Quarter { */ @StaticFactoryMethod(Quarter.class) public static Quarter of(int value) { - if (value < 1 || value > 4) { - throw new IllegalArgumentException("Invalid value for Quarter: " + value); - } - return ENUMS[value - 1]; + return ENUMS[checkValidIntValue(value) - 1]; } // StaticFactoryMethods end @@ -134,6 +133,11 @@ public enum Quarter { return value; } + @Override + public int getCode() { + return getValue(); + } + public Month firstMonth() { return Month.of(firstMonthValue()); } @@ -166,6 +170,11 @@ public enum Quarter { // Getters end + public static int checkValidIntValue(int value) { + AssertTools.checkArgument(value >= 1 && value <= 4, () -> "Invalid value for Quarter: " + value); + return value; + } + // Internal /**