From fd1126be9b9dcbfde9ea6894911fc9979c7617b9 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 26 Aug 2024 10:24:07 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=9B=E4=BF=AE=E6=94=B9=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/base/Quarter.java | 102 +++++++----------- .../plusone/commons/base/YearQuarter.java | 38 ++++--- .../plusone/commons/base/QuarterTests.java | 76 ++++++------- .../commons/base/YearQuarterTests.java | 8 +- 4 files changed, 101 insertions(+), 123 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/Quarter.java b/src/main/java/xyz/zhouxy/plusone/commons/base/Quarter.java index 72bea90..4f7c2ed 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/Quarter.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/Quarter.java @@ -14,45 +14,45 @@ import xyz.zhouxy.plusone.commons.util.Numbers; */ public enum Quarter { /** 第一季度 */ - Q1(1, "Q1"), + Q1(1), /** 第二季度 */ - Q2(2, "Q2"), + Q2(2), /** 第三季度 */ - Q3(3, "Q3"), + Q3(3), /** 第四季度 */ - Q4(4, "Q4"), + Q4(4), ; /** 季度值 (1/2/3/4) */ private final int value; - /** 季度名称 */ - private final String displayName; /** 季度开始月份 */ - private final int startMonthValue; + private final Month firstMonth; /** 季度开始日期 */ - private final MonthDay startMonthDay; + private final MonthDay firstMonthDay; /** 季度结束月份 */ - private final int lastMonthValue; + private final Month lastMonth; /** 季度结束日期 */ private final MonthDay lastMonthDay; + /** 常量值 */ + private static final Quarter[] ENUMS = Quarter.values(); + /** * @param value 季度值 (1/2/3/4) - * @param str 季度名称 */ - Quarter(int value, String str) { + Quarter(int value) { this.value = value; - this.displayName = str; - final int lastMonth = value * 3; - final int startMonth = lastMonth - 2; + final int lastMonthValue = value * 3; + final int firstMonthValue = lastMonthValue - 2; - this.startMonthValue = startMonth; - this.startMonthDay = MonthDay.of(startMonth, 1); - this.lastMonthValue = lastMonth; - this.lastMonthDay = MonthDay.of(lastMonth, (value == 1 || value == 4) ? 31 : 30); + this.firstMonth = Month.of(firstMonthValue); + this.firstMonthDay = MonthDay.of(this.firstMonth, 1); + this.lastMonth = Month.of(lastMonthValue); + // 季度的最后一个月不可能是 2 月 + this.lastMonthDay = MonthDay.of(this.lastMonth, this.lastMonth.maxLength()); } /** @@ -97,40 +97,10 @@ public enum Quarter { * @throws IllegalArgumentException 如果季度值不在有效范围内(1到4),将抛出异常 */ public static Quarter of(int value) { - switch (value) { - case 1: - return Q1; - case 2: - return Q2; - case 3: - return Q3; - case 4: - return Q4; - default: - throw new EnumConstantNotPresentException(Quarter.class, Integer.toString(value)); - } - } - - /** - * 根据给定的季度名称返回对应的季度 - * - * @param str 季度名称 - * @return 对应的季度 - * @throws IllegalArgumentException 如果季度名称不在有效范围内(Q1/Q2/Q3/Q4),将抛出异常 - */ - public static Quarter of(String str) { - switch (str) { - case "Q1": - return Q1; - case "Q2": - return Q2; - case "Q3": - return Q3; - case "Q4": - return Q4; - default: - throw new EnumConstantNotPresentException(Quarter.class, str); + if (value < 1 || value > 4) { + throw new IllegalArgumentException("Invalid value for Quarter: " + value); } + return ENUMS[value - 1]; } // Getters @@ -139,34 +109,34 @@ public enum Quarter { return value; } - public String getDisplayName() { - return displayName; + public Month firstMonth() { + return firstMonth; } - public Month getStartMonth() { - return Month.of(startMonthValue); + public int firstMonthValue() { + return firstMonth.getValue(); } - public int getStartMonthValue() { - return startMonthValue; + public Month lastMonth() { + return lastMonth; } - public Month getLastMonth() { - return Month.of(lastMonthValue); + public int lastMonthValue() { + return lastMonth.getValue(); } - public int getLastMonthValue() { - return lastMonthValue; + public MonthDay firstMonthDay() { + return firstMonthDay; } - public MonthDay getStartMonthDay() { - return startMonthDay; - } - - public MonthDay getLastMonthDay() { + public MonthDay lastMonthDay() { return lastMonthDay; } + public int firstDayOfYear(boolean leapYear) { + return this.firstMonth.firstDayOfYear(leapYear); + } + // Getters end // Internal diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/YearQuarter.java b/src/main/java/xyz/zhouxy/plusone/commons/base/YearQuarter.java index b17d7bd..12149ea 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/YearQuarter.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/YearQuarter.java @@ -23,7 +23,7 @@ public final class YearQuarter { /** 季度 */ private final Quarter quarter; /** 季度开始日期 */ - private final LocalDate startDate; + private final LocalDate firstDate; /** 季度结束日期 */ private final LocalDate lastDate; @@ -31,8 +31,8 @@ public final class YearQuarter { Preconditions.checkNotNull(quarter, "Quarter can not be null."); this.year = year; this.quarter = quarter; - this.startDate = quarter.getStartMonthDay().atYear(year); - this.lastDate = quarter.getLastMonthDay().atYear(year); + this.firstDate = quarter.firstMonthDay().atYear(year); + this.lastDate = quarter.lastMonthDay().atYear(year); } /** @@ -100,27 +100,35 @@ public final class YearQuarter { return quarter; } - public Month getStartMonth() { - return this.quarter.getStartMonth(); + public YearMonth firstYearMonth() { + return YearMonth.of(this.year, this.quarter.firstMonth()); } - public int getStartMonthValue() { - return this.quarter.getStartMonthValue(); + public Month firstMonth() { + return this.quarter.firstMonth(); } - public Month getLastMonth() { - return this.quarter.getLastMonth(); + public int firstMonthValue() { + return this.quarter.firstMonthValue(); } - public int getLastMonthValue() { - return this.quarter.getLastMonthValue(); + public YearMonth lastYearMonth() { + return YearMonth.of(this.year, this.quarter.lastMonth()); } - public LocalDate getStartDate() { - return startDate; + public Month lastMonth() { + return this.quarter.lastMonth(); } - public LocalDate getLastDate() { + public int lastMonthValue() { + return this.quarter.lastMonthValue(); + } + + public LocalDate firstDate() { + return firstDate; + } + + public LocalDate lastDate() { return lastDate; } @@ -154,6 +162,6 @@ public final class YearQuarter { */ @Override public String toString() { - return this.quarter.getDisplayName() + " " + this.year; + return this.quarter.name() + " " + this.year; } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/base/QuarterTests.java b/src/test/java/xyz/zhouxy/plusone/commons/base/QuarterTests.java index f9895a4..35b2d67 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/base/QuarterTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/base/QuarterTests.java @@ -17,21 +17,21 @@ class QuarterTests { void testQ1() { Quarter quarter = Quarter.of(1); assertSame(Quarter.Q1, quarter); - assertSame(quarter, Quarter.of("Q1")); + assertSame(quarter, Quarter.valueOf("Q1")); assertEquals(1, quarter.getValue()); - assertEquals("Q1", quarter.getDisplayName()); + assertEquals("Q1", quarter.name()); - assertThrows(EnumConstantNotPresentException.class, () -> { + assertThrows(IllegalArgumentException.class, () -> { Quarter.of(0); }); // ========== - int startMonthValue = quarter.getStartMonthValue(); + int startMonthValue = quarter.firstMonthValue(); log.info("startMonthValue: {}", startMonthValue); assertEquals(1, startMonthValue); - Month startMonth = quarter.getStartMonth(); + Month startMonth = quarter.firstMonth(); log.info("startMonth: {}", startMonth); assertEquals(Month.JANUARY, startMonth); @@ -39,11 +39,11 @@ class QuarterTests { // ========== - int lastMonthValue = quarter.getLastMonthValue(); + int lastMonthValue = quarter.lastMonthValue(); log.info("lastMonthValue: {}", lastMonthValue); assertEquals(3, lastMonthValue); - Month lastMonth = quarter.getLastMonth(); + Month lastMonth = quarter.lastMonth(); log.info("lastMonth: {}", lastMonth); assertEquals(Month.MARCH, lastMonth); @@ -51,11 +51,11 @@ class QuarterTests { // ========== - MonthDay startMonthDay = quarter.getStartMonthDay(); + MonthDay startMonthDay = quarter.firstMonthDay(); log.info("startMonthDay: {}", startMonthDay); assertEquals(startMonthDay, MonthDay.of(1, 1)); - MonthDay lastMonthDay = quarter.getLastMonthDay(); + MonthDay lastMonthDay = quarter.lastMonthDay(); log.info("lastMonthDay: {}", lastMonthDay); assertEquals(lastMonthDay, MonthDay.of(3, 31)); } @@ -64,21 +64,21 @@ class QuarterTests { void testQ2() { Quarter quarter = Quarter.of(2); assertSame(Quarter.Q2, quarter); - assertSame(quarter, Quarter.of("Q2")); + assertSame(quarter, Quarter.valueOf("Q2")); assertEquals(2, quarter.getValue()); - assertEquals("Q2", quarter.getDisplayName()); + assertEquals("Q2", quarter.name()); - assertThrows(EnumConstantNotPresentException.class, () -> { + assertThrows(IllegalArgumentException.class, () -> { Quarter.of(5); }); // ========== - int startMonthValue = quarter.getStartMonthValue(); + int startMonthValue = quarter.firstMonthValue(); log.info("startMonthValue: {}", startMonthValue); assertEquals(4, startMonthValue); - Month startMonth = quarter.getStartMonth(); + Month startMonth = quarter.firstMonth(); log.info("startMonth: {}", startMonth); assertEquals(Month.APRIL, startMonth); @@ -86,11 +86,11 @@ class QuarterTests { // ========== - int lastMonthValue = quarter.getLastMonthValue(); + int lastMonthValue = quarter.lastMonthValue(); log.info("lastMonthValue: {}", lastMonthValue); assertEquals(6, lastMonthValue); - Month lastMonth = quarter.getLastMonth(); + Month lastMonth = quarter.lastMonth(); log.info("lastMonth: {}", lastMonth); assertEquals(Month.JUNE, lastMonth); @@ -98,11 +98,11 @@ class QuarterTests { // ========== - MonthDay startMonthDay = quarter.getStartMonthDay(); + MonthDay startMonthDay = quarter.firstMonthDay(); log.info("startMonthDay: {}", startMonthDay); assertEquals(startMonthDay, MonthDay.of(4, 1)); - MonthDay lastMonthDay = quarter.getLastMonthDay(); + MonthDay lastMonthDay = quarter.lastMonthDay(); log.info("lastMonthDay: {}", lastMonthDay); assertEquals(lastMonthDay, MonthDay.of(6, 30)); } @@ -111,21 +111,21 @@ class QuarterTests { void testQ3() { Quarter quarter = Quarter.of(3); assertSame(Quarter.Q3, quarter); - assertSame(quarter, Quarter.of("Q3")); + assertSame(quarter, Quarter.valueOf("Q3")); assertEquals(3, quarter.getValue()); - assertEquals("Q3", quarter.getDisplayName()); + assertEquals("Q3", quarter.name()); - assertThrows(EnumConstantNotPresentException.class, () -> { - Quarter.of("Abc"); + assertThrows(IllegalArgumentException.class, () -> { + Quarter.valueOf("Abc"); }); // ========== - int startMonthValue = quarter.getStartMonthValue(); + int startMonthValue = quarter.firstMonthValue(); log.info("startMonthValue: {}", startMonthValue); assertEquals(7, startMonthValue); - Month startMonth = quarter.getStartMonth(); + Month startMonth = quarter.firstMonth(); log.info("startMonth: {}", startMonth); assertEquals(Month.JULY, startMonth); @@ -133,11 +133,11 @@ class QuarterTests { // ========== - int lastMonthValue = quarter.getLastMonthValue(); + int lastMonthValue = quarter.lastMonthValue(); log.info("lastMonthValue: {}", lastMonthValue); assertEquals(9, lastMonthValue); - Month lastMonth = quarter.getLastMonth(); + Month lastMonth = quarter.lastMonth(); log.info("lastMonth: {}", lastMonth); assertEquals(Month.SEPTEMBER, lastMonth); @@ -145,11 +145,11 @@ class QuarterTests { // ========== - MonthDay startMonthDay = quarter.getStartMonthDay(); + MonthDay startMonthDay = quarter.firstMonthDay(); log.info("startMonthDay: {}", startMonthDay); assertEquals(startMonthDay, MonthDay.of(7, 1)); - MonthDay lastMonthDay = quarter.getLastMonthDay(); + MonthDay lastMonthDay = quarter.lastMonthDay(); log.info("lastMonthDay: {}", lastMonthDay); assertEquals(lastMonthDay, MonthDay.of(9, 30)); } @@ -158,21 +158,21 @@ class QuarterTests { void testQ4() { Quarter quarter = Quarter.of(4); assertSame(Quarter.Q4, quarter); - assertSame(quarter, Quarter.of("Q4")); + assertSame(quarter, Quarter.valueOf("Q4")); assertEquals(4, quarter.getValue()); - assertEquals("Q4", quarter.getDisplayName()); + assertEquals("Q4", quarter.name()); - assertThrows(EnumConstantNotPresentException.class, () -> { - Quarter.of("Q5"); + assertThrows(IllegalArgumentException.class, () -> { + Quarter.valueOf("Q5"); }); // ========== - int startMonthValue = quarter.getStartMonthValue(); + int startMonthValue = quarter.firstMonthValue(); log.info("startMonthValue: {}", startMonthValue); assertEquals(10, startMonthValue); - Month startMonth = quarter.getStartMonth(); + Month startMonth = quarter.firstMonth(); log.info("startMonth: {}", startMonth); assertEquals(Month.OCTOBER, startMonth); @@ -180,10 +180,10 @@ class QuarterTests { // ========== - int lastMonthValue = quarter.getLastMonthValue(); + int lastMonthValue = quarter.lastMonthValue(); log.info("lastMonthValue: {}", lastMonthValue); assertEquals(12, lastMonthValue); - Month lastMonth = quarter.getLastMonth(); + Month lastMonth = quarter.lastMonth(); log.info("lastMonth: {}", lastMonth); assertEquals(Month.DECEMBER, lastMonth); @@ -191,11 +191,11 @@ class QuarterTests { // ========== - MonthDay startMonthDay = quarter.getStartMonthDay(); + MonthDay startMonthDay = quarter.firstMonthDay(); log.info("startMonthDay: {}", startMonthDay); assertEquals(startMonthDay, MonthDay.of(10, 1)); - MonthDay lastMonthDay = quarter.getLastMonthDay(); + MonthDay lastMonthDay = quarter.lastMonthDay(); log.info("lastMonthDay: {}", lastMonthDay); assertEquals(lastMonthDay, MonthDay.of(12, 31)); } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/base/YearQuarterTests.java b/src/test/java/xyz/zhouxy/plusone/commons/base/YearQuarterTests.java index 6f8b441..c6d66c3 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/base/YearQuarterTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/base/YearQuarterTests.java @@ -42,13 +42,13 @@ public class YearQuarterTests { Quarter quarter = Quarter.of(qrtr); YearQuarter yearQuarter = YearQuarter.of(year, quarter); - LocalDate expectedStartDate = quarter.getStartMonthDay().atYear(year); + LocalDate expectedStartDate = quarter.firstMonthDay().atYear(year); log.info("{} - expectedStartDate: {}", yearQuarter, expectedStartDate); - LocalDate expectedEndDate = quarter.getLastMonthDay().atYear(year); + LocalDate expectedEndDate = quarter.lastMonthDay().atYear(year); log.info("{} - expectedEndDate: {}", yearQuarter, expectedEndDate); - assertEquals(expectedStartDate, yearQuarter.getStartDate()); - assertEquals(expectedEndDate, yearQuarter.getLastDate()); + assertEquals(expectedStartDate, yearQuarter.firstDate()); + assertEquals(expectedEndDate, yearQuarter.lastDate()); } } }