重构部分代码;修改 API

dev
ZhouXY108 2024-08-26 10:24:07 +08:00
parent c92f4fbd5c
commit fd1126be9b
4 changed files with 101 additions and 123 deletions

View File

@ -14,45 +14,45 @@ import xyz.zhouxy.plusone.commons.util.Numbers;
*/ */
public enum Quarter { 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) */ /** 季度值 (1/2/3/4) */
private final int value; 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 final MonthDay lastMonthDay;
/** 常量值 */
private static final Quarter[] ENUMS = Quarter.values();
/** /**
* @param value (1/2/3/4) * @param value (1/2/3/4)
* @param str
*/ */
Quarter(int value, String str) { Quarter(int value) {
this.value = value; this.value = value;
this.displayName = str;
final int lastMonth = value * 3; final int lastMonthValue = value * 3;
final int startMonth = lastMonth - 2; final int firstMonthValue = lastMonthValue - 2;
this.startMonthValue = startMonth; this.firstMonth = Month.of(firstMonthValue);
this.startMonthDay = MonthDay.of(startMonth, 1); this.firstMonthDay = MonthDay.of(this.firstMonth, 1);
this.lastMonthValue = lastMonth; this.lastMonth = Month.of(lastMonthValue);
this.lastMonthDay = MonthDay.of(lastMonth, (value == 1 || value == 4) ? 31 : 30); // 季度的最后一个月不可能是 2 月
this.lastMonthDay = MonthDay.of(this.lastMonth, this.lastMonth.maxLength());
} }
/** /**
@ -97,40 +97,10 @@ public enum Quarter {
* @throws IllegalArgumentException 14 * @throws IllegalArgumentException 14
*/ */
public static Quarter of(int value) { public static Quarter of(int value) {
switch (value) { if (value < 1 || value > 4) {
case 1: throw new IllegalArgumentException("Invalid value for Quarter: " + value);
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);
} }
return ENUMS[value - 1];
} }
// Getters // Getters
@ -139,34 +109,34 @@ public enum Quarter {
return value; return value;
} }
public String getDisplayName() { public Month firstMonth() {
return displayName; return firstMonth;
} }
public Month getStartMonth() { public int firstMonthValue() {
return Month.of(startMonthValue); return firstMonth.getValue();
} }
public int getStartMonthValue() { public Month lastMonth() {
return startMonthValue; return lastMonth;
} }
public Month getLastMonth() { public int lastMonthValue() {
return Month.of(lastMonthValue); return lastMonth.getValue();
} }
public int getLastMonthValue() { public MonthDay firstMonthDay() {
return lastMonthValue; return firstMonthDay;
} }
public MonthDay getStartMonthDay() { public MonthDay lastMonthDay() {
return startMonthDay;
}
public MonthDay getLastMonthDay() {
return lastMonthDay; return lastMonthDay;
} }
public int firstDayOfYear(boolean leapYear) {
return this.firstMonth.firstDayOfYear(leapYear);
}
// Getters end // Getters end
// Internal // Internal

View File

@ -23,7 +23,7 @@ public final class YearQuarter {
/** 季度 */ /** 季度 */
private final Quarter quarter; private final Quarter quarter;
/** 季度开始日期 */ /** 季度开始日期 */
private final LocalDate startDate; private final LocalDate firstDate;
/** 季度结束日期 */ /** 季度结束日期 */
private final LocalDate lastDate; private final LocalDate lastDate;
@ -31,8 +31,8 @@ public final class YearQuarter {
Preconditions.checkNotNull(quarter, "Quarter can not be null."); Preconditions.checkNotNull(quarter, "Quarter can not be null.");
this.year = year; this.year = year;
this.quarter = quarter; this.quarter = quarter;
this.startDate = quarter.getStartMonthDay().atYear(year); this.firstDate = quarter.firstMonthDay().atYear(year);
this.lastDate = quarter.getLastMonthDay().atYear(year); this.lastDate = quarter.lastMonthDay().atYear(year);
} }
/** /**
@ -100,27 +100,35 @@ public final class YearQuarter {
return quarter; return quarter;
} }
public Month getStartMonth() { public YearMonth firstYearMonth() {
return this.quarter.getStartMonth(); return YearMonth.of(this.year, this.quarter.firstMonth());
} }
public int getStartMonthValue() { public Month firstMonth() {
return this.quarter.getStartMonthValue(); return this.quarter.firstMonth();
} }
public Month getLastMonth() { public int firstMonthValue() {
return this.quarter.getLastMonth(); return this.quarter.firstMonthValue();
} }
public int getLastMonthValue() { public YearMonth lastYearMonth() {
return this.quarter.getLastMonthValue(); return YearMonth.of(this.year, this.quarter.lastMonth());
} }
public LocalDate getStartDate() { public Month lastMonth() {
return startDate; return this.quarter.lastMonth();
} }
public LocalDate getLastDate() { public int lastMonthValue() {
return this.quarter.lastMonthValue();
}
public LocalDate firstDate() {
return firstDate;
}
public LocalDate lastDate() {
return lastDate; return lastDate;
} }
@ -154,6 +162,6 @@ public final class YearQuarter {
*/ */
@Override @Override
public String toString() { public String toString() {
return this.quarter.getDisplayName() + " " + this.year; return this.quarter.name() + " " + this.year;
} }
} }

View File

@ -17,21 +17,21 @@ class QuarterTests {
void testQ1() { void testQ1() {
Quarter quarter = Quarter.of(1); Quarter quarter = Quarter.of(1);
assertSame(Quarter.Q1, quarter); assertSame(Quarter.Q1, quarter);
assertSame(quarter, Quarter.of("Q1")); assertSame(quarter, Quarter.valueOf("Q1"));
assertEquals(1, quarter.getValue()); assertEquals(1, quarter.getValue());
assertEquals("Q1", quarter.getDisplayName()); assertEquals("Q1", quarter.name());
assertThrows(EnumConstantNotPresentException.class, () -> { assertThrows(IllegalArgumentException.class, () -> {
Quarter.of(0); Quarter.of(0);
}); });
// ========== // ==========
int startMonthValue = quarter.getStartMonthValue(); int startMonthValue = quarter.firstMonthValue();
log.info("startMonthValue: {}", startMonthValue); log.info("startMonthValue: {}", startMonthValue);
assertEquals(1, startMonthValue); assertEquals(1, startMonthValue);
Month startMonth = quarter.getStartMonth(); Month startMonth = quarter.firstMonth();
log.info("startMonth: {}", startMonth); log.info("startMonth: {}", startMonth);
assertEquals(Month.JANUARY, startMonth); assertEquals(Month.JANUARY, startMonth);
@ -39,11 +39,11 @@ class QuarterTests {
// ========== // ==========
int lastMonthValue = quarter.getLastMonthValue(); int lastMonthValue = quarter.lastMonthValue();
log.info("lastMonthValue: {}", lastMonthValue); log.info("lastMonthValue: {}", lastMonthValue);
assertEquals(3, lastMonthValue); assertEquals(3, lastMonthValue);
Month lastMonth = quarter.getLastMonth(); Month lastMonth = quarter.lastMonth();
log.info("lastMonth: {}", lastMonth); log.info("lastMonth: {}", lastMonth);
assertEquals(Month.MARCH, lastMonth); assertEquals(Month.MARCH, lastMonth);
@ -51,11 +51,11 @@ class QuarterTests {
// ========== // ==========
MonthDay startMonthDay = quarter.getStartMonthDay(); MonthDay startMonthDay = quarter.firstMonthDay();
log.info("startMonthDay: {}", startMonthDay); log.info("startMonthDay: {}", startMonthDay);
assertEquals(startMonthDay, MonthDay.of(1, 1)); assertEquals(startMonthDay, MonthDay.of(1, 1));
MonthDay lastMonthDay = quarter.getLastMonthDay(); MonthDay lastMonthDay = quarter.lastMonthDay();
log.info("lastMonthDay: {}", lastMonthDay); log.info("lastMonthDay: {}", lastMonthDay);
assertEquals(lastMonthDay, MonthDay.of(3, 31)); assertEquals(lastMonthDay, MonthDay.of(3, 31));
} }
@ -64,21 +64,21 @@ class QuarterTests {
void testQ2() { void testQ2() {
Quarter quarter = Quarter.of(2); Quarter quarter = Quarter.of(2);
assertSame(Quarter.Q2, quarter); assertSame(Quarter.Q2, quarter);
assertSame(quarter, Quarter.of("Q2")); assertSame(quarter, Quarter.valueOf("Q2"));
assertEquals(2, quarter.getValue()); assertEquals(2, quarter.getValue());
assertEquals("Q2", quarter.getDisplayName()); assertEquals("Q2", quarter.name());
assertThrows(EnumConstantNotPresentException.class, () -> { assertThrows(IllegalArgumentException.class, () -> {
Quarter.of(5); Quarter.of(5);
}); });
// ========== // ==========
int startMonthValue = quarter.getStartMonthValue(); int startMonthValue = quarter.firstMonthValue();
log.info("startMonthValue: {}", startMonthValue); log.info("startMonthValue: {}", startMonthValue);
assertEquals(4, startMonthValue); assertEquals(4, startMonthValue);
Month startMonth = quarter.getStartMonth(); Month startMonth = quarter.firstMonth();
log.info("startMonth: {}", startMonth); log.info("startMonth: {}", startMonth);
assertEquals(Month.APRIL, startMonth); assertEquals(Month.APRIL, startMonth);
@ -86,11 +86,11 @@ class QuarterTests {
// ========== // ==========
int lastMonthValue = quarter.getLastMonthValue(); int lastMonthValue = quarter.lastMonthValue();
log.info("lastMonthValue: {}", lastMonthValue); log.info("lastMonthValue: {}", lastMonthValue);
assertEquals(6, lastMonthValue); assertEquals(6, lastMonthValue);
Month lastMonth = quarter.getLastMonth(); Month lastMonth = quarter.lastMonth();
log.info("lastMonth: {}", lastMonth); log.info("lastMonth: {}", lastMonth);
assertEquals(Month.JUNE, lastMonth); assertEquals(Month.JUNE, lastMonth);
@ -98,11 +98,11 @@ class QuarterTests {
// ========== // ==========
MonthDay startMonthDay = quarter.getStartMonthDay(); MonthDay startMonthDay = quarter.firstMonthDay();
log.info("startMonthDay: {}", startMonthDay); log.info("startMonthDay: {}", startMonthDay);
assertEquals(startMonthDay, MonthDay.of(4, 1)); assertEquals(startMonthDay, MonthDay.of(4, 1));
MonthDay lastMonthDay = quarter.getLastMonthDay(); MonthDay lastMonthDay = quarter.lastMonthDay();
log.info("lastMonthDay: {}", lastMonthDay); log.info("lastMonthDay: {}", lastMonthDay);
assertEquals(lastMonthDay, MonthDay.of(6, 30)); assertEquals(lastMonthDay, MonthDay.of(6, 30));
} }
@ -111,21 +111,21 @@ class QuarterTests {
void testQ3() { void testQ3() {
Quarter quarter = Quarter.of(3); Quarter quarter = Quarter.of(3);
assertSame(Quarter.Q3, quarter); assertSame(Quarter.Q3, quarter);
assertSame(quarter, Quarter.of("Q3")); assertSame(quarter, Quarter.valueOf("Q3"));
assertEquals(3, quarter.getValue()); assertEquals(3, quarter.getValue());
assertEquals("Q3", quarter.getDisplayName()); assertEquals("Q3", quarter.name());
assertThrows(EnumConstantNotPresentException.class, () -> { assertThrows(IllegalArgumentException.class, () -> {
Quarter.of("Abc"); Quarter.valueOf("Abc");
}); });
// ========== // ==========
int startMonthValue = quarter.getStartMonthValue(); int startMonthValue = quarter.firstMonthValue();
log.info("startMonthValue: {}", startMonthValue); log.info("startMonthValue: {}", startMonthValue);
assertEquals(7, startMonthValue); assertEquals(7, startMonthValue);
Month startMonth = quarter.getStartMonth(); Month startMonth = quarter.firstMonth();
log.info("startMonth: {}", startMonth); log.info("startMonth: {}", startMonth);
assertEquals(Month.JULY, startMonth); assertEquals(Month.JULY, startMonth);
@ -133,11 +133,11 @@ class QuarterTests {
// ========== // ==========
int lastMonthValue = quarter.getLastMonthValue(); int lastMonthValue = quarter.lastMonthValue();
log.info("lastMonthValue: {}", lastMonthValue); log.info("lastMonthValue: {}", lastMonthValue);
assertEquals(9, lastMonthValue); assertEquals(9, lastMonthValue);
Month lastMonth = quarter.getLastMonth(); Month lastMonth = quarter.lastMonth();
log.info("lastMonth: {}", lastMonth); log.info("lastMonth: {}", lastMonth);
assertEquals(Month.SEPTEMBER, lastMonth); assertEquals(Month.SEPTEMBER, lastMonth);
@ -145,11 +145,11 @@ class QuarterTests {
// ========== // ==========
MonthDay startMonthDay = quarter.getStartMonthDay(); MonthDay startMonthDay = quarter.firstMonthDay();
log.info("startMonthDay: {}", startMonthDay); log.info("startMonthDay: {}", startMonthDay);
assertEquals(startMonthDay, MonthDay.of(7, 1)); assertEquals(startMonthDay, MonthDay.of(7, 1));
MonthDay lastMonthDay = quarter.getLastMonthDay(); MonthDay lastMonthDay = quarter.lastMonthDay();
log.info("lastMonthDay: {}", lastMonthDay); log.info("lastMonthDay: {}", lastMonthDay);
assertEquals(lastMonthDay, MonthDay.of(9, 30)); assertEquals(lastMonthDay, MonthDay.of(9, 30));
} }
@ -158,21 +158,21 @@ class QuarterTests {
void testQ4() { void testQ4() {
Quarter quarter = Quarter.of(4); Quarter quarter = Quarter.of(4);
assertSame(Quarter.Q4, quarter); assertSame(Quarter.Q4, quarter);
assertSame(quarter, Quarter.of("Q4")); assertSame(quarter, Quarter.valueOf("Q4"));
assertEquals(4, quarter.getValue()); assertEquals(4, quarter.getValue());
assertEquals("Q4", quarter.getDisplayName()); assertEquals("Q4", quarter.name());
assertThrows(EnumConstantNotPresentException.class, () -> { assertThrows(IllegalArgumentException.class, () -> {
Quarter.of("Q5"); Quarter.valueOf("Q5");
}); });
// ========== // ==========
int startMonthValue = quarter.getStartMonthValue(); int startMonthValue = quarter.firstMonthValue();
log.info("startMonthValue: {}", startMonthValue); log.info("startMonthValue: {}", startMonthValue);
assertEquals(10, startMonthValue); assertEquals(10, startMonthValue);
Month startMonth = quarter.getStartMonth(); Month startMonth = quarter.firstMonth();
log.info("startMonth: {}", startMonth); log.info("startMonth: {}", startMonth);
assertEquals(Month.OCTOBER, startMonth); assertEquals(Month.OCTOBER, startMonth);
@ -180,10 +180,10 @@ class QuarterTests {
// ========== // ==========
int lastMonthValue = quarter.getLastMonthValue(); int lastMonthValue = quarter.lastMonthValue();
log.info("lastMonthValue: {}", lastMonthValue); log.info("lastMonthValue: {}", lastMonthValue);
assertEquals(12, lastMonthValue); assertEquals(12, lastMonthValue);
Month lastMonth = quarter.getLastMonth(); Month lastMonth = quarter.lastMonth();
log.info("lastMonth: {}", lastMonth); log.info("lastMonth: {}", lastMonth);
assertEquals(Month.DECEMBER, lastMonth); assertEquals(Month.DECEMBER, lastMonth);
@ -191,11 +191,11 @@ class QuarterTests {
// ========== // ==========
MonthDay startMonthDay = quarter.getStartMonthDay(); MonthDay startMonthDay = quarter.firstMonthDay();
log.info("startMonthDay: {}", startMonthDay); log.info("startMonthDay: {}", startMonthDay);
assertEquals(startMonthDay, MonthDay.of(10, 1)); assertEquals(startMonthDay, MonthDay.of(10, 1));
MonthDay lastMonthDay = quarter.getLastMonthDay(); MonthDay lastMonthDay = quarter.lastMonthDay();
log.info("lastMonthDay: {}", lastMonthDay); log.info("lastMonthDay: {}", lastMonthDay);
assertEquals(lastMonthDay, MonthDay.of(12, 31)); assertEquals(lastMonthDay, MonthDay.of(12, 31));
} }

View File

@ -42,13 +42,13 @@ public class YearQuarterTests {
Quarter quarter = Quarter.of(qrtr); Quarter quarter = Quarter.of(qrtr);
YearQuarter yearQuarter = YearQuarter.of(year, quarter); YearQuarter yearQuarter = YearQuarter.of(year, quarter);
LocalDate expectedStartDate = quarter.getStartMonthDay().atYear(year); LocalDate expectedStartDate = quarter.firstMonthDay().atYear(year);
log.info("{} - expectedStartDate: {}", yearQuarter, expectedStartDate); log.info("{} - expectedStartDate: {}", yearQuarter, expectedStartDate);
LocalDate expectedEndDate = quarter.getLastMonthDay().atYear(year); LocalDate expectedEndDate = quarter.lastMonthDay().atYear(year);
log.info("{} - expectedEndDate: {}", yearQuarter, expectedEndDate); log.info("{} - expectedEndDate: {}", yearQuarter, expectedEndDate);
assertEquals(expectedStartDate, yearQuarter.getStartDate()); assertEquals(expectedStartDate, yearQuarter.firstDate());
assertEquals(expectedEndDate, yearQuarter.getLastDate()); assertEquals(expectedEndDate, yearQuarter.lastDate());
} }
} }
} }