重构部分代码;修改 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 {
/** 第一季度 */
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 14
*/
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

View File

@ -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;
}
}

View File

@ -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));
}

View File

@ -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());
}
}
}