Skip to content

Commit

Permalink
Applied generics to date-based properties
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed May 17, 2019
1 parent c91bfab commit 1b69aeb
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 124 deletions.
Expand Up @@ -32,13 +32,13 @@
package net.fortuna.ical4j.model.property;

import net.fortuna.ical4j.model.*;
import net.fortuna.ical4j.model.parameter.TzId;
import net.fortuna.ical4j.model.parameter.Value;
import net.fortuna.ical4j.util.Strings;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.time.temporal.Temporal;
import java.util.Optional;

/**
* $Id$
Expand All @@ -47,24 +47,35 @@
* <p/>
* Base class for properties with a list of dates as a value.
*
* Note that generics have been introduced as part of the migration to the new Java Date/Time API.
* Date properties should now indicate the applicable {@link Temporal} type for the property.
*
* For example:
*
* <ul>
* <li>UTC-based properties should use {@link java.time.Instant} to represent UTC time</li>
* <li>Date-only properties should use {@link java.time.LocalDate} to represent a date value</li>
* <li>Date-time properties should use {@link java.time.ZonedDateTime} to represent a date-time value influenced by timezone rules</li>
* </ul>
*
* @author Ben Fortuna
*/
public abstract class DateListProperty extends Property {
public abstract class DateListProperty<T extends Temporal> extends Property {

/**
*
*/
private static final long serialVersionUID = 5233773091972759919L;

private DateList dates;
private DateList<T> dates;

private TimeZone timeZone;

/**
* @param name the property name
*/
public DateListProperty(final String name, PropertyFactory factory) {
this(name, new DateList(Value.DATE_TIME), factory);
this(name, new DateList<>(), factory);
}

/**
Expand All @@ -79,7 +90,7 @@ public DateListProperty(final String name, final ParameterList parameters, Prope
* @param name the property name
* @param dates a list of initial dates for the property
*/
public DateListProperty(final String name, final DateList dates, PropertyFactory factory) {
public DateListProperty(final String name, final DateList<T> dates, PropertyFactory factory) {
this(name, new ParameterList(), dates, factory);
}

Expand All @@ -88,28 +99,24 @@ public DateListProperty(final String name, final DateList dates, PropertyFactory
* @param parameters property parameters
* @param dates a list of initial dates for the property
*/
public DateListProperty(final String name, final ParameterList parameters, final DateList dates,
public DateListProperty(final String name, final ParameterList parameters, final DateList<T> dates,
PropertyFactory factory) {
super(name, parameters, factory);
this.dates = dates;
if (dates != null && !Value.DATE_TIME.equals(dates.getType())) {
getParameters().replace(dates.getType());
}
}

/**
* @return Returns the dates.
*/
public final DateList getDates() {
public final DateList<T> getDates() {
return dates;
}

/**
* {@inheritDoc}
*/
public void setValue(final String aValue) throws ParseException {
dates = new DateList(aValue, getParameter(Parameter.VALUE),
timeZone);
dates = DateList.parse(aValue);
}

/**
Expand All @@ -131,13 +138,7 @@ public void setTimeZone(final TimeZone timezone) {
}
this.timeZone = timezone;
if (timezone != null) {
if (!Value.DATE_TIME.equals(getDates().getType())) {
throw new UnsupportedOperationException(
"TimeZone is not applicable to current value");
}
dates.setTimeZone(timezone);
getParameters().remove(getParameter(Parameter.TZID));
final TzId tzId = new TzId(timezone.getID());
final net.fortuna.ical4j.model.parameter.TzId tzId = new net.fortuna.ical4j.model.parameter.TzId(timezone.getID());
getParameters().replace(tzId);
} else {
// use setUtc() to reset timezone..
Expand All @@ -148,8 +149,8 @@ public void setTimeZone(final TimeZone timezone) {
/**
* @return the timezone
*/
public final TimeZone getTimeZone() {
return timeZone;
public final Optional<TimeZone> getTimeZone() {
return Optional.ofNullable(timeZone);
}

/**
Expand All @@ -160,12 +161,12 @@ public final TimeZone getTimeZone() {
* @param utc the UTC value
*/
public final void setUtc(final boolean utc) {
if (dates == null || !Value.DATE_TIME.equals(dates.getType())) {
throw new UnsupportedOperationException(
"TimeZone is not applicable to current value");
if (dates == null) {
throw new UnsupportedOperationException("TimeZone is not applicable to current value");
}
if (utc) {
getParameters().remove(getParameter(Parameter.TZID));
}
dates.setUtc(utc);
getParameters().remove(getParameter(Parameter.TZID));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/fortuna/ical4j/model/property/DtEnd.java
Expand Up @@ -107,7 +107,7 @@
*
* @author Ben Fortuna
*/
public class DtEnd extends DateProperty {
public class DtEnd<T extends Temporal> extends DateProperty<T> {

private static final long serialVersionUID = 8107416684717228297L;

Expand Down Expand Up @@ -167,7 +167,7 @@ public DtEnd(final ParameterList aList, final String aValue)
*
* @param aDate a date
*/
public DtEnd(final Temporal aDate) {
public DtEnd(final T aDate) {
super(DTEND, new Factory());
setDate(aDate);
}
Expand All @@ -178,7 +178,7 @@ public DtEnd(final Temporal aDate) {
* @param time the time of the DtEnd
* @param utc specifies whether time is UTC
*/
public DtEnd(final Temporal time, final boolean utc) {
public DtEnd(final T time, final boolean utc) {
super(DTEND, new Factory());
setDate(time);
setUtc(utc);
Expand All @@ -190,7 +190,7 @@ public DtEnd(final Temporal time, final boolean utc) {
* @param aList a list of parameters for this component
* @param aDate a date
*/
public DtEnd(final ParameterList aList, final Temporal aDate) {
public DtEnd(final ParameterList aList, final T aDate) {
super(DTEND, aList, new Factory());
setDate(aDate);
}
Expand All @@ -208,7 +208,7 @@ public Property createProperty(final ParameterList parameters, final String valu
}

public Property createProperty() {
return new DtEnd();
return new DtEnd<>();
}
}

Expand Down
23 changes: 11 additions & 12 deletions src/main/java/net/fortuna/ical4j/model/property/DtStart.java
Expand Up @@ -105,7 +105,7 @@
*
* @author Ben Fortuna
*/
public class DtStart extends DateProperty {
public class DtStart<T extends Temporal> extends DateProperty<T> {

private static final long serialVersionUID = -5707097476081111815L;

Expand All @@ -127,9 +127,9 @@ public DtStart(TimeZone timezone) {

/**
* @param aValue a value string for this component
* @throws ParseException where the specified value string is not a valid date-time/date representation
* @throws java.time.format.DateTimeParseException where the specified value string is not a valid date-time/date representation
*/
public DtStart(final String aValue) throws ParseException {
public DtStart(final String aValue) {
super(DTSTART, new Factory());
setValue(aValue);
}
Expand All @@ -139,21 +139,20 @@ public DtStart(final String aValue) throws ParseException {
*
* @param value a string representation of a DTSTART value
* @param timezone initial timezone
* @throws ParseException where the specified value is not a valid string
* @throws java.time.format.DateTimeParseException where the specified value is not a valid string
* representation
*/
public DtStart(String value, TimeZone timezone) throws ParseException {
public DtStart(String value, TimeZone timezone) {
super(DTSTART, timezone, new Factory());
setValue(value);
}

/**
* @param aList a list of parameters for this component
* @param aValue a value string for this component
* @throws ParseException where the specified value string is not a valid date-time/date representation
* @throws java.time.format.DateTimeParseException where the specified value string is not a valid date-time/date representation
*/
public DtStart(final ParameterList aList, final String aValue)
throws ParseException {
public DtStart(final ParameterList aList, final String aValue) {
super(DTSTART, aList, new Factory());
setValue(aValue);
}
Expand All @@ -163,7 +162,7 @@ public DtStart(final ParameterList aList, final String aValue)
*
* @param aDate a date
*/
public DtStart(final Temporal aDate) {
public DtStart(final T aDate) {
super(DTSTART, new Factory());
setDate(aDate);
}
Expand All @@ -174,7 +173,7 @@ public DtStart(final Temporal aDate) {
* @param time the time of the DtStart
* @param utc specifies whether time is UTC
*/
public DtStart(final Temporal time, final boolean utc) {
public DtStart(final T time, final boolean utc) {
super(DTSTART, new Factory());
setDate(time);
setUtc(utc);
Expand All @@ -186,7 +185,7 @@ public DtStart(final Temporal time, final boolean utc) {
* @param aList a list of parameters for this component
* @param aDate a date
*/
public DtStart(final ParameterList aList, final Temporal aDate) {
public DtStart(final ParameterList aList, final T aDate) {
super(DTSTART, aList, new Factory());
setDate(aDate);
}
Expand All @@ -204,7 +203,7 @@ public Property createProperty(final ParameterList parameters, final String valu
}

public Property createProperty() {
return new DtStart();
return new DtStart<>();
}
}

Expand Down
30 changes: 10 additions & 20 deletions src/main/java/net/fortuna/ical4j/model/property/Due.java
Expand Up @@ -93,19 +93,10 @@
*
* @author Ben Fortuna
*/
public class Due extends DateProperty {
public class Due<T extends Temporal> extends DateProperty<T> {

private static final long serialVersionUID = -2965312347832730406L;

/**
* Default constructor. The time value is initialised to the time of instantiation.
*/
public Due() {
super(DUE, new Factory());
// defaults to UTC time..
setDate(LocalDateTime.now(ZoneOffset.UTC));
}

/**
* Creates a new DUE property initialised with the specified timezone.
*
Expand All @@ -119,9 +110,9 @@ public Due(TimeZone timezone) {
* Creates a new instance initialised with the parsed value.
*
* @param value the DUE value string to parse
* @throws ParseException where the specified string is not a valid DUE value representation
* @throws java.time.format.DateTimeParseException where the specified string is not a valid DUE value representation
*/
public Due(final String value) throws ParseException {
public Due(final String value) {
super(DUE, new Factory());
setValue(value);
}
Expand All @@ -131,21 +122,20 @@ public Due(final String value) throws ParseException {
*
* @param value a string representation of a DUE value
* @param timezone initial timezone
* @throws ParseException where the specified value is not a valid string
* @throws java.time.format.DateTimeParseException where the specified value is not a valid string
* representation
*/
public Due(String value, TimeZone timezone) throws ParseException {
public Due(String value, TimeZone timezone) {
super(DUE, timezone, new Factory());
setValue(value);
}

/**
* @param aList a list of parameters for this component
* @param aValue a value string for this component
* @throws ParseException when the specified string is not a valid date/date-time representation
* @throws java.time.format.DateTimeParseException when the specified string is not a valid date/date-time representation
*/
public Due(final ParameterList aList, final String aValue)
throws ParseException {
public Due(final ParameterList aList, final String aValue) {
super(DUE, aList, new Factory());
setValue(aValue);
}
Expand All @@ -155,7 +145,7 @@ public Due(final ParameterList aList, final String aValue)
*
* @param aDate a date
*/
public Due(final Temporal aDate) {
public Due(final T aDate) {
super(DUE, new Factory());
setDate(aDate);
}
Expand All @@ -166,7 +156,7 @@ public Due(final Temporal aDate) {
* @param aList a list of parameters for this component
* @param aDate a date
*/
public Due(final ParameterList aList, final Temporal aDate) {
public Due(final ParameterList aList, final T aDate) {
super(DUE, aList, new Factory());
setDate(aDate);
}
Expand All @@ -184,7 +174,7 @@ public Property createProperty(final ParameterList parameters, final String valu
}

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

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/fortuna/ical4j/model/property/ExDate.java
Expand Up @@ -39,6 +39,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.time.temporal.Temporal;

/**
* $Id$
Expand All @@ -49,7 +50,7 @@
*
* @author benf
*/
public class ExDate extends DateListProperty {
public class ExDate<T extends Temporal> extends DateListProperty<T> {

private static final long serialVersionUID = 2635730172243974463L;

Expand All @@ -74,15 +75,15 @@ public ExDate(final ParameterList aList, final String aValue)
/**
* @param dList a list of dates
*/
public ExDate(final DateList dList) {
public ExDate(final DateList<T> dList) {
super(EXDATE, dList, new Factory());
}

/**
* @param aList a list of parameters for this component
* @param dList a list of dates
*/
public ExDate(final ParameterList aList, final DateList dList) {
public ExDate(final ParameterList aList, final DateList<T> dList) {
super(EXDATE, aList, dList, new Factory());
}

Expand Down

0 comments on commit 1b69aeb

Please sign in to comment.