Skip to content

Commit

Permalink
Add updated field (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
w3stling authored Aug 8, 2024
1 parent 1064ab8 commit 529f86a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ protected void registerItemTags() {
itemTags.putIfAbsent("category", Item::addCategory);
itemTags.putIfAbsent("pubDate", Item::setPubDate);
itemTags.putIfAbsent("published", Item::setPubDate);
itemTags.putIfAbsent("updated", (item, value) -> Mapper.mapIfEmpty(value, item::getPubDate, item::setPubDate));
itemTags.putIfAbsent("updated", (item, value) -> {
item.setUpdated(value);
Mapper.mapIfEmpty(value, item::getPubDate, item::setPubDate);
});
itemTags.putIfAbsent("comments", Item::setComments);
itemTags.putIfAbsent("dc:creator", (item, value) -> Mapper.mapIfEmpty(value, item::getAuthor, item::setAuthor));
itemTags.putIfAbsent("dc:date", (item, value) -> Mapper.mapIfEmpty(value, item::getPubDate, item::setPubDate));
Expand Down
35 changes: 32 additions & 3 deletions src/main/java/com/apptasticsoftware/rssreader/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class Item implements Comparable<Item> {
private String guid;
private Boolean isPermaLink;
private String pubDate;
private String updated;
private String comments;
private Enclosure enclosure;
private final List<Enclosure> enclosures = new ArrayList<>();
Expand Down Expand Up @@ -211,7 +212,7 @@ public void setGuid(String guid) {

/**
* If the guid element has an attribute named "isPermaLink" with a value of true, the reader may assume that
* it is a permalink to the item, that is, a url that can be opened in a Web browser, that points to the full
* it is a permalink to the item, that is, an url that can be opened in a Web browser, that points to the full
* item described by the item element.
*
* @return permanent link
Expand All @@ -222,7 +223,7 @@ public Optional<Boolean> getIsPermaLink() {

/**
* If the guid element has an attribute named "isPermaLink" with a value of true, the reader may assume that
* it is a permalink to the item, that is, a url that can be opened in a Web browser, that points to the full
* it is a permalink to the item, that is, an url that can be opened in a Web browser, that points to the full
* item described by the item element.
*
* @param isPermaLink is perma link
Expand Down Expand Up @@ -258,6 +259,33 @@ public Optional<ZonedDateTime> getPubDateZonedDateTime() {
return getPubDate().map(dateTimeParser::parse);
}

/**
* Get a string that indicates when the item was updated.
*
* @return updated date
*/
public Optional<String> getUpdated() {
return Optional.ofNullable(updated);
}

/**
* Set a string that indicates when the item was updated.
*
* @param updated updated date
*/
public void setUpdated(String updated) {
this.updated = updated;
}

/**
* Get a ZonedDateTime that indicates when the item was updated.
*
* @return publication date
*/
public Optional<ZonedDateTime> getUpdatedZonedDateTime() {
return getUpdated().map(dateTimeParser::parse);
}

/**
* Get comments relating to the item.
* @return comments
Expand Down Expand Up @@ -341,6 +369,7 @@ public boolean equals(Object o) {
Objects.equals(getGuid(), item.getGuid()) &&
Objects.equals(getIsPermaLink(), item.getIsPermaLink()) &&
Objects.equals(getPubDate(), item.getPubDate()) &&
Objects.equals(getUpdated(), item.getUpdated()) &&
Objects.equals(getComments(), item.getComments()) &&
getEnclosures().equals(item.getEnclosures()) &&
Objects.equals(getChannel(), item.getChannel());
Expand All @@ -349,7 +378,7 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
return Objects.hash(getTitle(), getDescription(), getLink(), getAuthor(), getCategories(),
getGuid(), getIsPermaLink(), getPubDate(), getComments(), getEnclosures(), getChannel());
getGuid(), getIsPermaLink(), getPubDate(), getUpdated(), getComments(), getEnclosures(), getChannel());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ void testCloseTwice() throws IOException {
}
}

@SuppressWarnings("java:S5961")
@Test
void testAtomFeed() {
var items = new RssReader().read(fromFile("atom-feed.xml"))
Expand All @@ -618,18 +619,30 @@ void testAtomFeed() {
assertEquals("Example Toolkit", items.get(0).getChannel().getGenerator().orElse(null));
assertEquals("2005-07-31T12:29:29Z", items.get(0).getChannel().getLastBuildDate().orElse(null));

assertEquals("Atom draft-07 snapshot", items.get(0).getTitle().orElse(null));
assertNull(items.get(1).getAuthor().orElse(null));
assertEquals("http://example.org/audio/ph34r_my_podcast.mp3", items.get(0).getLink().orElse(null));
assertEquals("tag:example.org,2003:3.2397", items.get(0).getGuid().orElse(null));
assertEquals("2003-12-13T08:29:29-04:00", items.get(0).getPubDate().orElse(null));
assertEquals("2005-07-31T12:29:29Z", items.get(0).getUpdated().orElse(null));
assertEquals(211, items.get(1).getDescription().orElse("").length());

assertEquals("Atom-Powered Robots Run Amok", items.get(1).getTitle().orElse(null));
assertNull(items.get(1).getAuthor().orElse(null));
assertEquals("http://example.org/2003/12/13/atom03", items.get(1).getLink().orElse(null));
assertEquals("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a", items.get(1).getGuid().orElse(null));
assertEquals("2003-12-13T18:30:02Z", items.get(1).getPubDate().orElse(null));
assertEquals("2003-12-13T18:30:02Z", items.get(1).getUpdated().orElse(null));
assertEquals(211, items.get(1).getDescription().orElse("").length());

assertEquals("Atom-Powered Robots Run Amok 2", items.get(2).getTitle().orElse(null));
assertNull(items.get(2).getAuthor().orElse(null));
assertEquals("http://example.org/2003/12/13/atom04", items.get(2).getLink().orElse(null));
assertEquals("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6b", items.get(2).getGuid().orElse(null));
assertEquals("2003-12-13T18:30:01Z", items.get(2).getPubDate().orElse(null));
assertEquals("2003-12-13T09:28:28-04:00", items.get(2).getPubDate().orElse(null));
assertEquals(1071322108, items.get(2).getPubDateZonedDateTime().map(ZonedDateTime::toEpochSecond).orElse(null));
assertEquals("2003-12-13T18:30:01Z", items.get(2).getUpdated().orElse(null));
assertEquals(1071340201, items.get(2).getUpdatedZonedDateTime().map(ZonedDateTime::toEpochSecond).orElse(null));
assertEquals(47, items.get(2).getDescription().orElse("").length());
}

Expand Down
1 change: 1 addition & 0 deletions src/test/resources/atom-feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<title>Atom-Powered Robots Run Amok 2</title>
<link href="http://example.org/2003/12/13/atom04"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6b</id>
<published>2003-12-13T09:28:28-04:00</published>
<updated>2003-12-13T18:30:01Z</updated>
</entry>
</feed>

0 comments on commit 529f86a

Please sign in to comment.