feat: added feature

This commit is contained in:
Zihlu Wang
2024-05-25 00:37:47 +08:00
parent 75e858c667
commit ac125d189d
26 changed files with 201 additions and 149 deletions
@@ -1,40 +0,0 @@
/*
* Copyright (C) 2024-2024 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.onixbyte.icalendar;
import com.onixbyte.icalendar.property.parameter.AlternateRepresentation;
import com.onixbyte.icalendar.property.parameter.Language;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
/**
* CalendarUtil
*
* @author Zihlu WANG
*/
public final class CalendarUtil {
private CalendarUtil() {
}
}
@@ -51,7 +51,7 @@ public class Event extends CalendarComponent {
/**
*
*/
private DateTimeStamp dtStart;
// private DateTimeStamp dtStart;
/*
* The following properties are OPTIONAL, but MUST NOT occur more than once.
@@ -31,7 +31,7 @@ public final class Categories implements ComponentProperty {
}
public static class Builder {
private List<String> categories;
private final List<String> categories;
private Builder() {
categories = new ArrayList<>();
@@ -17,13 +17,9 @@
package com.onixbyte.icalendar.component.property;
import com.onixbyte.icalendar.CalendarUtil;
import com.onixbyte.icalendar.property.parameter.AlternateRepresentation;
import com.onixbyte.icalendar.property.parameter.Language;
import java.util.Objects;
import java.util.Optional;
public record Comment(AlternateRepresentation altRep,
Language language,
String value) implements TextProperty, ComponentProperty {
@@ -32,6 +28,6 @@ public record Comment(AlternateRepresentation altRep,
@Override
public String resolve() {
return composeResolution(PROPERTY_NAME, altRep, language, value);
return TextProperty.composeResolution(PROPERTY_NAME, altRep, language, value);
}
}
@@ -17,7 +17,7 @@
package com.onixbyte.icalendar.component.property;
import com.onixbyte.icalendar.CalendarUtil;
import com.onixbyte.icalendar.core.DateTimeFormatters;
import java.time.LocalDateTime;
import java.time.ZoneId;
@@ -31,6 +31,6 @@ public record DateTimeCompleted(LocalDateTime value) implements DateTimeProperty
return PROPERTY_NAME + ":" + value
.atZone(ZoneId.systemDefault())
.withZoneSameInstant(ZoneId.of("UTC"))
.format(DateTimeProperty.utcDateTimeFormatter());
.format(DateTimeFormatters.utcDateTimeFormatter()) + "\n";
}
}
@@ -17,10 +17,25 @@
package com.onixbyte.icalendar.component.property;
import com.onixbyte.icalendar.core.DateTimeFormatters;
import java.time.LocalDateTime;
import java.time.ZoneId;
/**
* DateTimeCreated
*
* @author Zihlu WANG
*/
public final class DateTimeCreated {
public record DateTimeCreated(LocalDateTime created) implements ComponentProperty {
private static final String PROPERTY_NAME = "CREATED";
@Override
public String resolve() {
return PROPERTY_NAME + ":" + created
.atZone(ZoneId.systemDefault())
.withZoneSameInstant(ZoneId.of("UTC"))
.format(DateTimeFormatters.utcDateTimeFormatter()) + "\n";
}
}
@@ -17,13 +17,10 @@
package com.onixbyte.icalendar.component.property;
import com.onixbyte.icalendar.CalendarUtil;
import com.onixbyte.icalendar.property.parameter.ValueDataType;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public record DateTimeDue(ValueDataType valueDataType,
LocalDateTime localDateTime) implements DateTimeProperty, ComponentProperty {
@@ -17,13 +17,13 @@
package com.onixbyte.icalendar.component.property;
import com.onixbyte.icalendar.CalendarUtil;
import com.onixbyte.icalendar.core.DateTimeFormatters;
import com.onixbyte.icalendar.property.parameter.ValueDataType;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public interface DateTimeProperty {
@@ -33,18 +33,6 @@ public interface DateTimeProperty {
ValueDataType.DATE_TIME
);
DateTimeFormatter utcDateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'");
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
static DateTimeFormatter utcDateTimeFormatter() {
return utcDateTimeFormatter;
}
static DateTimeFormatter dateFormatter() {
return dateFormatter;
}
static String composeResolution(String propertyName,
ValueDataType valueDataType,
LocalDateTime localDateTime) {
@@ -57,12 +45,16 @@ public interface DateTimeProperty {
var dateTimeFormatter = switch (Optional.ofNullable(valueDataType)
.filter(SUPPORTED_VALUES::contains)
.orElse(ValueDataType.DATE_TIME)) {
case DATE -> dateFormatter;
case DATE -> DateTimeFormatters.dateFormatter();
case BINARY, UTC_OFFSET, BOOLEAN, CAL_ADDRESS, DURATION, FLOAT, INTEGER, PERIOD,
RECURRENCE_RULE, TEXT, URI, DATE_TIME -> utcDateTimeFormatter;
RECURRENCE_RULE, TEXT, URI, DATE_TIME -> DateTimeFormatters.utcDateTimeFormatter();
};
dateTimeEndBuilder.append(":").append(localDateTime.format(dateTimeFormatter)).append("\n");
dateTimeEndBuilder.append(":")
.append(localDateTime.atZone(ZoneId.systemDefault())
.withZoneSameInstant(ZoneId.of("UTC"))
.format(dateTimeFormatter))
.append("\n");
return dateTimeEndBuilder.toString();
}
@@ -18,9 +18,10 @@
package com.onixbyte.icalendar.component.property;
import com.onixbyte.icalendar.calendar.property.Method;
import com.onixbyte.icalendar.config.Formatters;
import com.onixbyte.icalendar.core.DateTimeFormatters;
import java.time.LocalDateTime;
import java.time.ZoneId;
/**
* In the case of an {@link com.onixbyte.icalendar.component.Calendar iCalendar} object that
@@ -50,7 +51,15 @@ import java.time.LocalDateTime;
*
* @author Zihlu WANG
*/
// public final class DateTimeStamp implements DateTimeProperty, ComponentProperty {
//
//
// }
public record DateTimeStamp(LocalDateTime dateTimeStamp) implements DateTimeProperty, ComponentProperty {
private static final String PROPERTY_NAME = "DTSTAMP";
@Override
public String resolve() {
return PROPERTY_NAME + ":" + dateTimeStamp
.atZone(ZoneId.systemDefault())
.withZoneSameInstant(ZoneId.of("UTC"))
.format(DateTimeFormatters.utcDateTimeFormatter()) + "\n";
}
}
@@ -17,7 +17,6 @@
package com.onixbyte.icalendar.component.property;
import com.onixbyte.icalendar.CalendarUtil;
import com.onixbyte.icalendar.property.parameter.AlternateRepresentation;
import com.onixbyte.icalendar.property.parameter.Language;
@@ -29,7 +28,7 @@ public record Description(AlternateRepresentation altRep,
@Override
public String resolve() {
return composeResolution(PROPERTY_NAME, altRep, language, value);
return TextProperty.composeResolution(PROPERTY_NAME, altRep, language, value);
}
}
@@ -49,7 +49,7 @@ public final class FreeBusyTime implements ComponentProperty {
private FreeBusyTimeType freeBusyType;
private List<Period> freeBusyValue;
private final List<Period> freeBusyValue;
private Builder() {
this.freeBusyValue = new ArrayList<>();
@@ -70,7 +70,8 @@ public final class FreeBusyTime implements ComponentProperty {
freeBusyBuilder.append(":").append(String.join(",", freeBusyValue
.stream()
.map(Period::resolve)
.toList()));
.toList()))
.append("\n");
return freeBusyBuilder.toString();
}
@@ -17,10 +17,24 @@
package com.onixbyte.icalendar.component.property;
import com.onixbyte.icalendar.core.DateTimeFormatters;
import java.time.LocalDateTime;
import java.time.ZoneId;
/**
* LastModified
*
* @author Zihlu WANG
*/
public final class LastModified {
public record LastModified(LocalDateTime value) implements ComponentProperty {
private static final String PROPERTY_NAME = "LAST-MODIFIED";
@Override
public String resolve() {
return PROPERTY_NAME + ":" + value.atZone(ZoneId.systemDefault())
.withZoneSameInstant(ZoneId.of("UTC"))
.format(DateTimeFormatters.utcDateTimeFormatter()) + "\n";
}
}
@@ -17,7 +17,6 @@
package com.onixbyte.icalendar.component.property;
import com.onixbyte.icalendar.CalendarUtil;
import com.onixbyte.icalendar.property.parameter.AlternateRepresentation;
import com.onixbyte.icalendar.property.parameter.Language;
@@ -29,6 +28,6 @@ public record Location(AlternateRepresentation altRep,
@Override
public String resolve() {
return composeResolution(PROPERTY_NAME, altRep, language, value);
return TextProperty.composeResolution(PROPERTY_NAME, altRep, language, value);
}
}
@@ -40,7 +40,7 @@ public final class Resources implements TextProperty, ComponentProperty {
}
public static class Builder {
private List<String> value;
private final List<String> value;
private AlternateRepresentation altRep;
@@ -81,6 +81,6 @@ public final class Resources implements TextProperty, ComponentProperty {
@Override
public String resolve() {
return composeResolution(PROPERTY_NAME, altRep, language, () -> String.join(",", value));
return TextProperty.composeResolution(PROPERTY_NAME, altRep, language, () -> String.join(",", value));
}
}
@@ -0,0 +1,11 @@
package com.onixbyte.icalendar.component.property;
public record SequenceNumber(Integer sequence) implements ComponentProperty {
private static final String PROPERTY_NAME = "SEQUENCE";
@Override
public String resolve() {
return PROPERTY_NAME + ":" + sequence + "\n";
}
}
@@ -28,6 +28,6 @@ public record Summary(AlternateRepresentation altRep,
@Override
public String resolve() {
return composeResolution(PROPERTY_NAME, altRep, language, value);
return TextProperty.composeResolution(PROPERTY_NAME, altRep, language, value);
}
}
@@ -26,7 +26,7 @@ import java.util.function.Supplier;
public interface TextProperty {
default String composeResolution(String propertyName,
static String composeResolution(String propertyName,
AlternateRepresentation altRep,
Language language,
String value) {
@@ -49,7 +49,7 @@ public interface TextProperty {
return resolutionBuilder.toString();
}
default String composeResolution(String propertyName,
static String composeResolution(String propertyName,
AlternateRepresentation altRep,
Language language,
Supplier<String> valueSupplier) {
@@ -0,0 +1,15 @@
package com.onixbyte.icalendar.component.property;
public enum TimeTransparency implements ComponentProperty {
OPAQUE,
TRANSPARENT,
;
private static final String PROPERTY_NAME = "TRANSP";
@Override
public String resolve() {
return PROPERTY_NAME + ":" + name() + "\n";
}
}
@@ -0,0 +1,19 @@
package com.onixbyte.icalendar.component.property;
import java.time.ZoneId;
public final class TimeZoneIdentifier implements ComponentProperty {
private static final String PROPERTY_NAME = "TZID";
private final ZoneId timeZoneIdentifier;
public TimeZoneIdentifier(String timeZoneIdentifier) {
this.timeZoneIdentifier = ZoneId.of(timeZoneIdentifier);
}
@Override
public String resolve() {
return PROPERTY_NAME + ":" + timeZoneIdentifier + "\n";
}
}
@@ -0,0 +1,24 @@
package com.onixbyte.icalendar.component.property;
import com.onixbyte.icalendar.property.parameter.Language;
import java.util.Optional;
public record TimeZoneName(Language language, String value) implements ComponentProperty {
private static final String PROPERTY_NAME = "TZNAME";
@Override
public String resolve() {
var builder = new StringBuilder(PROPERTY_NAME);
Optional.ofNullable(language)
.ifPresent((lang) -> builder.append(";").append(lang.resolve()));
builder.append(":")
.append(value)
.append("\n");
return builder.toString();
}
}
@@ -0,0 +1,4 @@
package com.onixbyte.icalendar.component.property;
public record TimeZoneOffsetFrom() {
}
@@ -1,54 +0,0 @@
/*
* Copyright (C) 2024-2024 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.onixbyte.icalendar.config;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
/**
* A formatter to format {@link java.time.LocalDateTime}.
*
* @author Zihlu Wang
*/
public final class Formatters {
/**
* 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);
}
return utcDateTimeFormatter;
}
private static DateTimeFormatter utcDateTimeFormatter;
/**
* Private constructor to prevent this class being initialised.
*/
private Formatters() {
}
}
@@ -15,9 +15,18 @@
* limitations under the License.
*/
package com.onixbyte.icalendar.core;
/**
* CalendarUtil
*
*
* @since 1.0.0
* @author Zihlu WANG
*/
package com.onixbyte.icalendar.config;
public final class CalendarUtil {
private CalendarUtil() {
}
}
@@ -0,0 +1,42 @@
package com.onixbyte.icalendar.core;
import com.onixbyte.icalendar.property.parameter.ValueDataType;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.Optional;
public final class DateTimeFormatters {
private DateTimeFormatters() {
}
private static DateTimeFormatter utcDateTimeFormatter;
private static DateTimeFormatter dateFormatter;
private static DateTimeFormatter localDateTimeFormatter;
public static DateTimeFormatter utcDateTimeFormatter() {
if (Objects.isNull(utcDateTimeFormatter)) {
utcDateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'");
}
return utcDateTimeFormatter;
}
public static DateTimeFormatter dateFormatter() {
if (Objects.isNull(dateFormatter)) {
dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
}
return dateFormatter;
}
public static DateTimeFormatter localDateTimeFormatter() {
if (Objects.isNull(localDateTimeFormatter)) {
localDateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss");
}
return localDateTimeFormatter;
}
}
@@ -17,8 +17,7 @@
package com.onixbyte.icalendar.datatype;
import com.onixbyte.icalendar.component.property.DateTimeProperty;
import com.onixbyte.icalendar.property.Resolvable;
import com.onixbyte.icalendar.core.DateTimeFormatters;
import java.time.LocalDateTime;
@@ -38,7 +37,7 @@ public final class PeriodExplicit implements Period {
@Override
public String resolve() {
return startTime.format(DateTimeProperty.utcDateTimeFormatter()) + "/" +
endTime.format(DateTimeProperty.utcDateTimeFormatter());
return startTime.format(DateTimeFormatters.utcDateTimeFormatter()) + "/" +
endTime.format(DateTimeFormatters.utcDateTimeFormatter());
}
}
@@ -18,6 +18,7 @@
package com.onixbyte.icalendar.datatype;
import com.onixbyte.icalendar.component.property.DateTimeProperty;
import com.onixbyte.icalendar.core.DateTimeFormatters;
import java.time.Duration;
import java.time.LocalDateTime;
@@ -30,6 +31,6 @@ public final class PeriodStart implements Period {
@Override
public String resolve() {
return start.format(DateTimeProperty.utcDateTimeFormatter()) + "/" + duration;
return start.format(DateTimeFormatters.utcDateTimeFormatter()) + "/" + duration;
}
}