Skip to content

Commit

Permalink
Improved factory generics
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jun 13, 2020
1 parent 42af23d commit 73dd147
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/main/java/net/fortuna/ical4j/model/TemporalAdapter.java
Expand Up @@ -35,14 +35,14 @@ public class TemporalAdapter<T extends Temporal> implements Serializable {
/**
* A formatter capable of parsing to multiple temporal types based on the input string.
*/
private static CalendarDateFormat PARSE_FORMAT = new CalendarDateFormat(
private static final CalendarDateFormat PARSE_FORMAT = new CalendarDateFormat(
"yyyyMMdd['T'HHmmss[X]]", Instant::from, LocalDateTime::from, LocalDate::from);

private final String valueString;

private final TzId tzId;

private transient TimeZoneRegistry timeZoneRegistry;
private transient final TimeZoneRegistry timeZoneRegistry;

private transient T temporal;

Expand Down Expand Up @@ -96,6 +96,7 @@ private TemporalAdapter(String value, TzId tzId, TimeZoneRegistry timeZoneRegist
this.timeZoneRegistry = timeZoneRegistry;
}

@SuppressWarnings("unchecked")
public T getTemporal() {
if (temporal == null) {
synchronized (valueString) {
Expand Down Expand Up @@ -180,6 +181,7 @@ public ZonedDateTime toLocalTime(ZoneId zoneId) {
* @return an adapter containing the parsed temporal value and format type
* @throws DateTimeParseException if the string cannot be parsed
*/
@SuppressWarnings("unchecked")
public static <T extends Temporal> TemporalAdapter<T> parse(String value) throws DateTimeParseException {
return new TemporalAdapter<>((T) PARSE_FORMAT.parse(value));
}
Expand Down
Expand Up @@ -78,14 +78,14 @@ public abstract class DateProperty<T extends Temporal> extends Property {
* @param name the property name
* @param parameters a list of initial parameters
*/
public DateProperty(final String name, final List<Parameter> parameters, PropertyFactory factory) {
public DateProperty(final String name, final List<Parameter> parameters, PropertyFactory<? extends DateProperty<T>> factory) {
super(name, parameters, factory);
}

/**
* @param name the property name
*/
public DateProperty(final String name, PropertyFactory factory) {
public DateProperty(final String name, PropertyFactory<? extends DateProperty<T>> factory) {
super(name, factory);
}

Expand All @@ -97,6 +97,7 @@ public DateProperty(final String name, PropertyFactory factory) {
*
* @return Returns the date.
*/
@SuppressWarnings("unchecked")
public T getDate() {
if (date != null) {
Optional<TzId> tzId = getParameter(Parameter.TZID);
Expand Down Expand Up @@ -133,6 +134,7 @@ public void setDate(T date) {
*
* @param value a string representation of a DATE or DATE-TIME value
*/
@SuppressWarnings("unchecked")
public void setValue(final String value) throws DateTimeParseException {
// value can be either a date-time or a date..
if (value != null && !value.isEmpty()) {
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/net/fortuna/ical4j/model/property/RecurrenceId.java
Expand Up @@ -38,8 +38,6 @@
import net.fortuna.ical4j.validate.ParameterValidator;
import net.fortuna.ical4j.validate.ValidationException;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.Temporal;
import java.util.List;

Expand Down Expand Up @@ -133,13 +131,17 @@ public class RecurrenceId<T extends Temporal> extends DateProperty<T> {

private static final long serialVersionUID = 4456883817126011006L;

public RecurrenceId() {
super(RECURRENCE_ID, new Factory<T>());
}

/**
* Creates a new instance initialised with the parsed value.
*
* @param value the RECURRENCE_ID value string to parse
*/
public RecurrenceId(final String value) {
super(RECURRENCE_ID, new Factory());
super(RECURRENCE_ID, new Factory<T>());
setValue(value);
}

Expand All @@ -148,7 +150,7 @@ public RecurrenceId(final String value) {
* @param aValue a value string for this component
*/
public RecurrenceId(final List<Parameter> aList, final String aValue) {
super(RECURRENCE_ID, aList, new Factory());
super(RECURRENCE_ID, aList, new Factory<T>());
setValue(aValue);
}

Expand All @@ -158,7 +160,7 @@ public RecurrenceId(final List<Parameter> aList, final String aValue) {
* @param aDate a date representation of a date or date-time
*/
public RecurrenceId(final T aDate) {
super(RECURRENCE_ID, new Factory());
super(RECURRENCE_ID, new Factory<T>());
setDate(aDate);
}

Expand All @@ -169,7 +171,7 @@ public RecurrenceId(final T aDate) {
* @param aDate a date representation of a date or date-time
*/
public RecurrenceId(final List<Parameter> aList, final T aDate) {
super(RECURRENCE_ID, aList, new Factory());
super(RECURRENCE_ID, aList, new Factory<T>());
setDate(aDate);
}

Expand All @@ -194,22 +196,22 @@ public final void validate() throws ValidationException {

@Override
public Property copy() {
return new Factory().createProperty(getParameters(), getValue());
return new Factory<T>().createProperty(getParameters(), getValue());
}

public static class Factory extends Content.Factory implements PropertyFactory<RecurrenceId> {
public static class Factory<T extends Temporal> extends Content.Factory implements PropertyFactory<RecurrenceId<T>> {
private static final long serialVersionUID = 1L;

public Factory() {
super(RECURRENCE_ID);
}

public RecurrenceId createProperty(final List<Parameter> parameters, final String value) {
return new RecurrenceId(parameters, value);
public RecurrenceId<T> createProperty(final List<Parameter> parameters, final String value) {
return new RecurrenceId<>(parameters, value);
}

public RecurrenceId createProperty() {
return new RecurrenceId<>(LocalDateTime.now(ZoneOffset.UTC));
public RecurrenceId<T> createProperty() {
return new RecurrenceId<>();
}
}

Expand Down

0 comments on commit 73dd147

Please sign in to comment.