-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a library for sloppy date parsing
Remove support for dangerous Bundesarchiv date range format (1906/08, meaning the years) and add support for e.g. `1933 ca.` and `1939/09 - 1945/05`. Refactor some confusing parsing code (and intro- duce some more of our own for ranges.)
- Loading branch information
Showing
18 changed files
with
294 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
ehri-io/src/main/java/eu/ehri/project/importers/util/DateRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package eu.ehri.project.importers.util; | ||
|
||
import eu.ehri.project.definitions.Ontology; | ||
import org.apache.jena.ext.com.google.common.collect.Maps; | ||
|
||
import java.time.*; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class DateRange { | ||
|
||
private static final DateTimeFormatter isoDateFormat = DateTimeFormatter.ISO_LOCAL_DATE; | ||
|
||
private final LocalDate start; | ||
private final LocalDate end; | ||
private final String description; | ||
|
||
public DateRange(LocalDate start, LocalDate end, String description) { | ||
if (start == null) { | ||
throw new IllegalArgumentException("DateRange start must not be null"); | ||
} | ||
this.start = start; | ||
this.end = end; | ||
this.description = description; | ||
} | ||
|
||
public static DateRange of(LocalDate start, LocalDate end, String description) { | ||
return new DateRange(start, end, description); | ||
} | ||
|
||
public DateRange(LocalDate start, String description) { | ||
this(start, null, description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return end != null | ||
? String.format("%s - %s", toLocalDateString(start), toLocalDateString(end)) | ||
: toLocalDateString(start); | ||
} | ||
|
||
/** | ||
* Debug constructor: creates a DateRange from a "YYYY-MM-DD - YYYY-MM-DD" | ||
* string. | ||
*/ | ||
public static DateRange fromString(String s, String description) { | ||
final String[] split = s.split("\\s-\\s"); | ||
final LocalDate d1 = LocalDate.parse(split[0]); | ||
final LocalDate d2 = split.length > 1 ? LocalDate.parse(split[1]) : null; | ||
return new DateRange(d1, d2, description); | ||
} | ||
|
||
public Map<String, Object> data() { | ||
Map<String, Object> data = Maps.newHashMap(); | ||
data.put(Ontology.DATE_PERIOD_START_DATE, toLocalDateString(start)); | ||
if (end != null) { | ||
data.put(Ontology.DATE_PERIOD_END_DATE, toLocalDateString(end)); | ||
} | ||
if (description != null) { | ||
data.put(Ontology.DATE_HAS_DESCRIPTION, description); | ||
} | ||
return data; | ||
} | ||
|
||
private String toLocalDateString(LocalDate instant) { | ||
return instant.format(isoDateFormat); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
DateRange dateRange = (DateRange) o; | ||
return start.equals(dateRange.start) | ||
&& Objects.equals(end, dateRange.end) | ||
&& description.equals(dateRange.description); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(start, end, description); | ||
} | ||
} |
Oops, something went wrong.