Skip to content

Commit

Permalink
Default date time parser
Browse files Browse the repository at this point in the history
  • Loading branch information
w3stling committed Oct 14, 2024
1 parent db84e2c commit 9c2651b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/apptasticsoftware/rssreader/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.apptasticsoftware.rssreader;

import com.apptasticsoftware.rssreader.util.Default;
import com.apptasticsoftware.rssreader.util.Util;

import java.time.ZonedDateTime;
Expand Down Expand Up @@ -60,7 +61,7 @@ public class Channel {
@SuppressWarnings("java:S1133")
@Deprecated(since="3.5.0", forRemoval=true)
public Channel() {
dateTimeParser = new DateTime();
dateTimeParser = Default.getDateTimeParser();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.apptasticsoftware.rssreader;

import java.time.Instant;
import java.time.ZonedDateTime;

/**
Expand All @@ -36,4 +37,11 @@ public interface DateTimeParser {
* @return ZonedDateTime
*/
ZonedDateTime parse(String timestamp);

/**
* Converts a timestamp in String format to an Instant
* @param dateTime timestamp
* @return Instant
*/
Instant toInstant(String dateTime);
}
3 changes: 2 additions & 1 deletion src/main/java/com/apptasticsoftware/rssreader/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.apptasticsoftware.rssreader;


import com.apptasticsoftware.rssreader.util.Default;
import com.apptasticsoftware.rssreader.util.ItemComparator;

import java.time.ZonedDateTime;
Expand Down Expand Up @@ -60,7 +61,7 @@ public class Item implements Comparable<Item> {
@SuppressWarnings("java:S1133")
@Deprecated(since="3.5.0", forRemoval=true)
public Item() {
dateTimeParser = new DateTime();
dateTimeParser = Default.getDateTimeParser();
defaultComparator = ItemComparator.newestItemFirst();
}

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/apptasticsoftware/rssreader/util/Default.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.apptasticsoftware.rssreader.util;

import com.apptasticsoftware.rssreader.DateTime;
import com.apptasticsoftware.rssreader.DateTimeParser;

/**
* Provides default implementations for various components.
*/
public class Default {

private Default() {
// Utility class
}

/**
* Get the default date time parser.
* @return date time parser
*/
public static DateTimeParser getDateTimeParser() {
return new DateTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package com.apptasticsoftware.rssreader.util;

import com.apptasticsoftware.rssreader.DateTime;
import com.apptasticsoftware.rssreader.DateTimeParser;
import com.apptasticsoftware.rssreader.Item;

Expand All @@ -47,7 +46,7 @@ private ItemComparator() {
* @return comparator
*/
public static <I extends Item> Comparator<I> oldestItemFirst() {
var dateTime = new DateTime();
var dateTime = Default.getDateTimeParser();
return Comparator.comparing((I i) -> i.getPubDate().map(dateTime::toInstant).orElse(Instant.EPOCH));
}

Expand All @@ -68,7 +67,7 @@ public static <I extends Item> Comparator<I> oldestItemFirst(DateTimeParser date
* @return comparator
*/
public static <I extends Item> Comparator<I> newestItemFirst() {
var dateTime = new DateTime();
var dateTime = Default.getDateTimeParser();
return Comparator.comparing((I i) -> i.getPubDate().map(dateTime::toInstant).orElse(Instant.EPOCH)).reversed();
}

Expand Down

0 comments on commit 9c2651b

Please sign in to comment.