From 707712b2c0fb8f2ea0784efb52feac438c351779 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 28 Aug 2024 17:20:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A1=E7=AE=97=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/time/Quarter.java | 21 ++++++++++ .../plusone/commons/time/YearQuarter.java | 41 +++++++++++++++++++ .../plusone/commons/time/QuarterTests.java | 2 +- 3 files changed, 63 insertions(+), 1 deletion(-) 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 7e9fb14..5f3c6ff 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/time/Quarter.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/time/Quarter.java @@ -5,6 +5,7 @@ import java.time.MonthDay; import com.google.common.base.Preconditions; +import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.util.Numbers; /** @@ -45,6 +46,8 @@ public enum Quarter { this.firstMonth = this.lastMonth - 2; } + // StaticFactoryMethods + /** * 根据给定的月份值返回对应的季度 * @@ -52,6 +55,7 @@ public enum Quarter { * @return 对应的季度 * @throws IllegalArgumentException 如果月份值不在有效范围内(1到12),将抛出异常 */ + @StaticFactoryMethod(Quarter.class) public static Quarter fromMonth(int monthValue) { Preconditions.checkArgument(Numbers.between(monthValue, 1, 13), "Invalid value for MonthOfYear: " + monthValue); return of(computeQuarterValueInternal(monthValue)); @@ -63,6 +67,7 @@ public enum Quarter { * @param month 月份 * @return 对应的季度 */ + @StaticFactoryMethod(Quarter.class) public static Quarter fromMonth(Month month) { final int monthValue = month.getValue(); return of(computeQuarterValueInternal(monthValue)); @@ -86,6 +91,7 @@ public enum Quarter { * @return 对应的季度 * @throws IllegalArgumentException 如果季度值不在有效范围内(1到4),将抛出异常 */ + @StaticFactoryMethod(Quarter.class) public static Quarter of(int value) { if (value < 1 || value > 4) { throw new IllegalArgumentException("Invalid value for Quarter: " + value); @@ -93,6 +99,21 @@ public enum Quarter { return ENUMS[value - 1]; } + // StaticFactoryMethods end + + // computs + + public Quarter plus(long quarters) { // TODO 单元测试 + final int amount = (int) ((quarters % 4) + 4); + return ENUMS[(ordinal() + amount) % 4]; + } + + public Quarter minus(long quarters) { // TODO 单元测试 + return plus(-(quarters % 4)); + } + + // computs end + // Getters public int getValue() { 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 ac3946d..de30339 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java @@ -1,5 +1,7 @@ package xyz.zhouxy.plusone.commons.time; +import static java.time.temporal.ChronoField.YEAR; + import java.io.Serializable; import java.time.LocalDate; import java.time.Month; @@ -29,6 +31,14 @@ public final class YearQuarter implements Comparable, Serializable /** 季度结束日期 */ private final LocalDate lastDate; + private YearQuarter(int year, int quarter) { + Preconditions.checkNotNull(quarter, "Quarter can not be null."); + this.year = year; + this.quarter = Quarter.of(quarter); + this.firstDate = this.quarter.firstMonthDay().atYear(year); + this.lastDate = this.quarter.lastMonthDay().atYear(year); + } + private YearQuarter(int year, @Nonnull Quarter quarter) { Preconditions.checkNotNull(quarter, "Quarter can not be null."); this.year = year; @@ -136,6 +146,37 @@ public final class YearQuarter implements Comparable, Serializable // Getters end + // computes + + public YearQuarter plusQuarters(long quartersToAdd) { // TODO 单元测试 + if (quartersToAdd == 0) { + return this; + } + long quarterCount = this.year * 4L + (this.quarter.getValue() - 1); + long calcQuarters = quarterCount + quartersToAdd; // safe overflow + int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcQuarters, 4)); + int newQuarter = (int) Math.floorMod(calcQuarters, 4) + 1; + return of(newYear, Quarter.of(newQuarter)); + } + + public YearQuarter minusQuarters(long quartersToAdd) { // TODO 单元测试 + return plusQuarters(-quartersToAdd); + } + + public YearQuarter plusYears(long yearsToAdd) { // TODO 单元测试 + if (yearsToAdd == 0) { + return this; + } + int newYear = YEAR.checkValidIntValue(this.year + yearsToAdd); // safe overflow + return of(newYear, this.quarter); + } + + public YearQuarter minusYears(long yearsToAdd) { + return plusYears(-yearsToAdd); + } + + // computes end + // hashCode & equals @Override diff --git a/src/test/java/xyz/zhouxy/plusone/commons/time/QuarterTests.java b/src/test/java/xyz/zhouxy/plusone/commons/time/QuarterTests.java index 8620776..7ac0c5a 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/time/QuarterTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/time/QuarterTests.java @@ -203,7 +203,7 @@ class QuarterTests { @Test void testFirstDayOfYear() { int firstDayOfYear; - + firstDayOfYear = Quarter.Q1.firstDayOfYear(true); assertEquals(1, firstDayOfYear); firstDayOfYear = Quarter.Q1.firstDayOfYear(false);