Skip to content

Commit

Permalink
Changed signature of DateList to support lazy date parse/format based…
Browse files Browse the repository at this point in the history
… on TzId
  • Loading branch information
benfortuna committed Dec 30, 2020
1 parent b1df6f6 commit 78986f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 46 deletions.
69 changes: 25 additions & 44 deletions src/main/java/net/fortuna/ical4j/model/DateList.java
Expand Up @@ -35,6 +35,7 @@

import java.io.Serializable;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.time.temporal.Temporal;
import java.util.*;
Expand All @@ -52,78 +53,59 @@ public class DateList<T extends Temporal> implements Serializable {

private static final long serialVersionUID = -3700862452550012357L;

private final List<T> dates;

private final CalendarDateFormat dateFormat;
private final List<TemporalAdapter<T>> dates;

/**
* Default constructor.
*/
public DateList() {
this(CalendarDateFormat.FLOATING_DATE_TIME_FORMAT);
}

public DateList(CalendarDateFormat dateFormat) {
this(Collections.emptyList(), dateFormat);
this(Collections.emptyList());
}

/**
* Constructs a new date list of the specified type containing
* the dates in the specified list.
* @param list a list of dates to include in the new list
*/
public DateList(final List<T> list) {
this(list, CalendarDateFormat.from(list));
}

public DateList(final List<T> list, CalendarDateFormat dateFormat) {
Objects.requireNonNull(dateFormat, "dateFormat");
this.dates = Collections.unmodifiableList(list);
this.dateFormat = dateFormat;
public DateList(final List<TemporalAdapter<T>> list) {
this.dates = list;
}

/**
* Parse a string representation of a date/time list.
*
* @param value
* @param <T>
* @return
* @throws DateTimeParseException
*/
public static <T extends Temporal> DateList<T> parse(String value) {
return parse(value, null);
}

public static <T extends Temporal> DateList<T> parse(String value, CalendarDateFormat calendarDateFormat) {
List<Temporal> dates = Arrays.stream(value.split(",")).map(TemporalAdapter::parse)
.map(TemporalAdapter::getTemporal).collect(Collectors.toList());
public static DateList<? extends Temporal> parse(String value) {
List<TemporalAdapter<Temporal>> dates = Arrays.stream(value.split(","))
.map(TemporalAdapter::parse)
.collect(Collectors.toList());

if (calendarDateFormat != null) {
return new DateList<>((List<T>) dates, calendarDateFormat);
} else {
return new DateList<>((List<T>) dates);
}
return new DateList<>(dates);
}

public static <T extends Temporal> DateList<T> parse(String value, TzId tzId, TimeZoneRegistry timeZoneRegistry) {
List<Temporal> dates = Arrays.stream(value.split(",")).map(s -> TemporalAdapter.parse(s, tzId, timeZoneRegistry))
.map(TemporalAdapter::getTemporal).collect(Collectors.toList());
return new DateList<>((List<T>) dates);
public static DateList<ZonedDateTime> parse(String value, TzId tzId, TimeZoneRegistry timeZoneRegistry) {
List<TemporalAdapter<ZonedDateTime>> dates = Arrays.stream(value.split(","))
.map(s -> TemporalAdapter.parse(s, tzId, timeZoneRegistry))
.collect(Collectors.toList());
return new DateList<>(dates);
}

@Override
public String toString() {
if (dates.isEmpty()) {
return "";
}
return dates.stream().map(dateFormat::format).collect(Collectors.joining(","));
return dates.stream().map(TemporalAdapter::toString).collect(Collectors.joining(","));
}

public String toString(ZoneId zoneId) {
if (dates.isEmpty()) {
return "";
}
return dates.stream().map(date -> dateFormat.format(date, zoneId)).collect(Collectors.joining(","));
return dates.stream().map(date -> date.toString(zoneId)).collect(Collectors.joining(","));
}

/**
Expand All @@ -135,32 +117,31 @@ public String toString(ZoneId zoneId) {
* @see List#add(java.lang.Object)
*/
public final DateList<T> add(final T date) {
List<T> copy = new ArrayList<>(dates);
copy.add(date);
return new DateList<>(copy, dateFormat);
List<TemporalAdapter<T>> copy = new ArrayList<>(dates);
copy.add(new TemporalAdapter<T>(date));
return new DateList<>(copy);
}

public final DateList<T> addAll(Collection<? extends T> arg0) {
List<T> copy = new ArrayList<>(dates);
copy.addAll(arg0);
List<TemporalAdapter<T>> copy = new ArrayList<>(dates);
copy.addAll(arg0.stream().map(TemporalAdapter<T>::new).collect(Collectors.toList()));
return new DateList<>(copy);
}

public List<T> getDates() {
return dates;
return dates.stream().map(TemporalAdapter::getTemporal).collect(Collectors.toList());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DateList<?> dateList = (DateList<?>) o;
return Objects.equals(dates, dateList.dates) &&
dateFormat.equals(dateList.dateFormat);
return Objects.equals(dates, dateList.dates);
}

@Override
public int hashCode() {
return Objects.hash(dates, dateFormat);
return Objects.hash(dates);
}
}
Expand Up @@ -133,9 +133,9 @@ public final List<T> getDates() {
public void setValue(final String aValue) {
Optional<TzId> tzId = getParameters().getFirst(Parameter.TZID);
if (tzId.isPresent()) {
dates = DateList.parse(aValue, tzId.get(), timeZoneRegistry);
dates = (DateList<T>) DateList.parse(aValue, tzId.get(), timeZoneRegistry);
} else {
dates = DateList.parse(aValue);
dates = (DateList<T>) DateList.parse(aValue);
}
}

Expand Down

0 comments on commit 78986f3

Please sign in to comment.