refactor: Optimsed codes.

Move private and protected methods or fields to the last of java files.
This commit is contained in:
Zihlu Wang
2024-03-31 18:34:01 +08:00
parent cb537a6844
commit 5895557368
16 changed files with 200 additions and 152 deletions
@@ -17,6 +17,8 @@
package cn.org.codecrafters.webcal;
import cn.org.codecrafters.webcal.impl.WebCalendarEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -50,55 +52,6 @@ import java.util.Objects;
*/
public final class WebCalendar {
/**
* The {@code VCALENDAR} tag for iCalendar format
*/
private final static String TAG = "VCALENDAR";
/**
* The name of this calendar.
*/
private String name;
/**
* The company who produces this calendar.
* <p>
* This property will be used in {@code PRODID}.
*/
private String companyName;
/**
* The product name.
* <p>
* This property will be used in {@code PRODID}
*/
private String productName;
/**
* The producer's domain name.
*/
private String domainName;
/**
* Scale of this calendar.
*/
private final String scale = "GREGORIAN";
/**
* The method of this calendar.
*/
private String method;
/**
* The version of this calendar.
*/
private final String version = "2.0";
/**
* List of calendar components and events
*/
private final List<WebCalendarNode> nodes;
/**
* Constructor for WebCalendar class, initializes the list of calendar
* components and events.
@@ -209,5 +162,54 @@ public final class WebCalendar {
"END:" + TAG;
}
/**
* The {@code VCALENDAR} tag for iCalendar format
*/
private final static String TAG = "VCALENDAR";
/**
* The name of this calendar.
*/
private String name;
/**
* The company who produces this calendar.
* <p>
* This property will be used in {@code PRODID}.
*/
private String companyName;
/**
* The product name.
* <p>
* This property will be used in {@code PRODID}
*/
private String productName;
/**
* The producer's domain name.
*/
private String domainName;
/**
* Scale of this calendar.
*/
private final String scale = "GREGORIAN";
/**
* The method of this calendar.
*/
private String method;
/**
* The version of this calendar.
*/
private final String version = "2.0";
/**
* List of calendar components and events
*/
private final List<WebCalendarNode> nodes;
}
@@ -18,6 +18,7 @@
package cn.org.codecrafters.webcal;
import cn.org.codecrafters.webcal.config.Classification;
import cn.org.codecrafters.webcal.impl.WebCalendarEvent;
import java.time.Duration;
import java.time.LocalDateTime;
@@ -37,8 +38,7 @@ import java.util.List;
* @version 1.1.0
* @since 1.0.0
*/
public abstract sealed class WebCalendarNode
permits WebCalendarEvent {
public abstract class WebCalendarNode {
// Common properties for all calendar components and events
protected List<String> categories;
@@ -22,13 +22,19 @@ import java.time.format.DateTimeFormatter;
import java.util.Objects;
/**
* DatetimeFormatters
* A formatter to format {@link java.time.LocalDateTime}.
*
* @author Zihlu Wang
* @since 21 Sept, 2023
*/
public final class Formatter {
public final class DateAndTimeFormatter {
/**
* Get the {@link java.time.format.DateTimeFormatter datetime formatter}
* with UTC pattern and timezone.
*
* @return the {@link java.time.format.DateTimeFormatter datetime formatter}
* with UTC pattern and timezone
*/
public static DateTimeFormatter getUtcDatetimeFormatter() {
if (Objects.isNull(utcDateTimeFormatter)) {
utcDateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
@@ -37,16 +43,6 @@ public final class Formatter {
return utcDateTimeFormatter;
}
// public static DateTimeFormatter getLocalDatetimeFormatter() {
// if (Objects.isNull(localDatetimeFormatter)) {
// localDatetimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss");
// }
//
// return localDatetimeFormatter;
// }
private static DateTimeFormatter utcDateTimeFormatter;
// private static DateTimeFormatter localDatetimeFormatter;
}
@@ -15,10 +15,11 @@
* limitations under the License.
*/
package cn.org.codecrafters.webcal;
package cn.org.codecrafters.webcal.impl;
import cn.org.codecrafters.webcal.WebCalendarNode;
import cn.org.codecrafters.webcal.config.Classification;
import cn.org.codecrafters.webcal.config.Formatter;
import cn.org.codecrafters.webcal.config.DateAndTimeFormatter;
import java.text.MessageFormat;
import java.time.Duration;
@@ -47,8 +48,6 @@ import java.util.UUID;
*/
public final class WebCalendarEvent extends WebCalendarNode {
private final static String TAG = "VEVENT";
/**
* Add categories to the event.
*
@@ -270,8 +269,8 @@ public final class WebCalendarEvent extends WebCalendarNode {
END:{0}""",
TAG, // 0 - tag
Optional.ofNullable(uid).orElse(UUID.randomUUID().toString()) + "@" + domainName, // 1 - uid
now.format(Formatter.getUtcDatetimeFormatter()), // 2 - dtstamp
start.atZone(ZoneId.systemDefault()).format(Formatter.getUtcDatetimeFormatter()), // 3 - start time
now.format(DateAndTimeFormatter.getUtcDatetimeFormatter()), // 2 - dtstamp
start.atZone(ZoneId.systemDefault()).format(DateAndTimeFormatter.getUtcDatetimeFormatter()), // 3 - start time
Optional.ofNullable(summary).map((item) -> "\nSUMMARY:" + item).orElse(""), // 4 - summary
Optional.ofNullable(categories)
.map((item) -> !item.isEmpty() ? "\nCATEGORIES:" + resolveCategories() : null).orElse(""), // 5 - categories
@@ -289,4 +288,6 @@ public final class WebCalendarEvent extends WebCalendarNode {
);
}
private final static String TAG = "VEVENT";
}
@@ -27,7 +27,7 @@
* generating web calendars with customisable settings and events.
* </li>
* <li>
* {@link cn.org.codecrafters.webcal.WebCalendarEvent}: A class
* {@link cn.org.codecrafters.webcal.impl.WebCalendarEvent}: A class
* representing a single event in a web calendar with various
* attributes and options.
* </li>
@@ -18,7 +18,7 @@
package cn.org.codecrafters.webcal.test;
import cn.org.codecrafters.webcal.WebCalendar;
import cn.org.codecrafters.webcal.WebCalendarEvent;
import cn.org.codecrafters.webcal.impl.WebCalendarEvent;
import cn.org.codecrafters.webcal.config.Classification;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;