@Test public void testParseRecordedDate() { TemporalParser temporalParser = TemporalParser.create(); OccurrenceParseResult<TemporalAccessor> result; result = temporalParser.parseRecordedDate("2005", "1", "", "2005-01-01"); assertEquals(LocalDate.of(2005, 1, 1), result.getPayload()); assertEquals(0, result.getIssues().size()); result = temporalParser.parseRecordedDate("1996", "1", "26", "1996-01-26T01:00Z"); assertEquals( ZonedDateTime.of(LocalDateTime.of(1996, 1, 26, 1, 0), ZoneId.of("Z")), result.getPayload()); assertEquals(0, result.getIssues().size()); result = temporalParser.parseRecordedDate("2005", "1", "2", "2005-01-05"); assertNull(result.getPayload()); assertEquals(1, result.getIssues().size()); assertEquals(OccurrenceIssue.RECORDED_DATE_MISMATCH, result.getIssues().iterator().next()); }
public OccurrenceParseResult<TemporalAccessor> parseRecordedDate( String year, String month, String day, String dateString) { boolean atomizedDateProvided = StringUtils.isNotBlank(year) || StringUtils.isNotBlank(month) || StringUtils.isNotBlank(day); boolean dateStringProvided = StringUtils.isNotBlank(dateString); if (!atomizedDateProvided && !dateStringProvided) { return OccurrenceParseResult.fail(); } Set<OccurrenceIssue> issues = EnumSet.noneOf(OccurrenceIssue.class); TemporalAccessor parsedTemporalAccessor; ParseResult.CONFIDENCE confidence; ParseResult<TemporalAccessor> parsedYMDResult = atomizedDateProvided ? temporalParser.parse(year, month, day) : ParseResult.fail(); ParseResult<TemporalAccessor> parsedDateResult = dateStringProvided ? temporalParser.parse(dateString) : ParseResult.fail(); TemporalAccessor parsedYmdTa = parsedYMDResult.getPayload(); TemporalAccessor parsedDateTa = parsedDateResult.getPayload(); if (atomizedDateProvided && dateStringProvided && !TemporalAccessorUtils.sameOrContained(parsedYmdTa, parsedDateTa)) { boolean ambiguityResolved = false; if (parsedDateResult.getAlternativePayloads() != null) { for (TemporalAccessor possibleTa : parsedDateResult.getAlternativePayloads()) { if (TemporalAccessorUtils.sameOrContained(parsedYmdTa, possibleTa)) { parsedDateTa = possibleTa; ambiguityResolved = true; log.debug( "Ambiguous date {} matches year-month-day date {}-{}-{} for {}", dateString, year, month, day, parsedDateTa); } } } if (!ambiguityResolved) { issues.add(OccurrenceIssue.RECORDED_DATE_MISMATCH); log.debug("Date mismatch: [{} vs {}].", parsedYmdTa, parsedDateTa); } Optional<TemporalAccessor> bestResolution = TemporalAccessorUtils.bestResolution(parsedYmdTa, parsedDateTa); if (bestResolution.isPresent()) { parsedTemporalAccessor = bestResolution.get(); confidence = (parsedYmdTa == null || parsedDateTa == null) ? PROBABLE : DEFINITE; } else { return OccurrenceParseResult.fail(issues); } } else { parsedTemporalAccessor = TemporalAccessorUtils.bestResolution(parsedYmdTa, parsedDateTa).orElse(null); confidence = parsedDateTa != null ? parsedDateResult.getConfidence() : parsedYMDResult.getConfidence(); } if (!isValidDate(parsedTemporalAccessor)) { if (parsedTemporalAccessor == null) { issues.add(OccurrenceIssue.RECORDED_DATE_INVALID); } else { issues.add(OccurrenceIssue.RECORDED_DATE_UNLIKELY); } log.debug("Invalid date: [{}]].", parsedTemporalAccessor); return OccurrenceParseResult.fail(issues); } return OccurrenceParseResult.success(confidence, parsedTemporalAccessor, issues); }
TemporalParser implements Serializable { public OccurrenceParseResult<TemporalAccessor> parseRecordedDate( String year, String month, String day, String dateString) { boolean atomizedDateProvided = StringUtils.isNotBlank(year) || StringUtils.isNotBlank(month) || StringUtils.isNotBlank(day); boolean dateStringProvided = StringUtils.isNotBlank(dateString); if (!atomizedDateProvided && !dateStringProvided) { return OccurrenceParseResult.fail(); } Set<OccurrenceIssue> issues = EnumSet.noneOf(OccurrenceIssue.class); TemporalAccessor parsedTemporalAccessor; ParseResult.CONFIDENCE confidence; ParseResult<TemporalAccessor> parsedYMDResult = atomizedDateProvided ? temporalParser.parse(year, month, day) : ParseResult.fail(); ParseResult<TemporalAccessor> parsedDateResult = dateStringProvided ? temporalParser.parse(dateString) : ParseResult.fail(); TemporalAccessor parsedYmdTa = parsedYMDResult.getPayload(); TemporalAccessor parsedDateTa = parsedDateResult.getPayload(); if (atomizedDateProvided && dateStringProvided && !TemporalAccessorUtils.sameOrContained(parsedYmdTa, parsedDateTa)) { boolean ambiguityResolved = false; if (parsedDateResult.getAlternativePayloads() != null) { for (TemporalAccessor possibleTa : parsedDateResult.getAlternativePayloads()) { if (TemporalAccessorUtils.sameOrContained(parsedYmdTa, possibleTa)) { parsedDateTa = possibleTa; ambiguityResolved = true; log.debug( "Ambiguous date {} matches year-month-day date {}-{}-{} for {}", dateString, year, month, day, parsedDateTa); } } } if (!ambiguityResolved) { issues.add(OccurrenceIssue.RECORDED_DATE_MISMATCH); log.debug("Date mismatch: [{} vs {}].", parsedYmdTa, parsedDateTa); } Optional<TemporalAccessor> bestResolution = TemporalAccessorUtils.bestResolution(parsedYmdTa, parsedDateTa); if (bestResolution.isPresent()) { parsedTemporalAccessor = bestResolution.get(); confidence = (parsedYmdTa == null || parsedDateTa == null) ? PROBABLE : DEFINITE; } else { return OccurrenceParseResult.fail(issues); } } else { parsedTemporalAccessor = TemporalAccessorUtils.bestResolution(parsedYmdTa, parsedDateTa).orElse(null); confidence = parsedDateTa != null ? parsedDateResult.getConfidence() : parsedYMDResult.getConfidence(); } if (!isValidDate(parsedTemporalAccessor)) { if (parsedTemporalAccessor == null) { issues.add(OccurrenceIssue.RECORDED_DATE_INVALID); } else { issues.add(OccurrenceIssue.RECORDED_DATE_UNLIKELY); } log.debug("Invalid date: [{}]].", parsedTemporalAccessor); return OccurrenceParseResult.fail(issues); } return OccurrenceParseResult.success(confidence, parsedTemporalAccessor, issues); } }
TemporalParser implements Serializable { public OccurrenceParseResult<TemporalAccessor> parseRecordedDate( String year, String month, String day, String dateString) { boolean atomizedDateProvided = StringUtils.isNotBlank(year) || StringUtils.isNotBlank(month) || StringUtils.isNotBlank(day); boolean dateStringProvided = StringUtils.isNotBlank(dateString); if (!atomizedDateProvided && !dateStringProvided) { return OccurrenceParseResult.fail(); } Set<OccurrenceIssue> issues = EnumSet.noneOf(OccurrenceIssue.class); TemporalAccessor parsedTemporalAccessor; ParseResult.CONFIDENCE confidence; ParseResult<TemporalAccessor> parsedYMDResult = atomizedDateProvided ? temporalParser.parse(year, month, day) : ParseResult.fail(); ParseResult<TemporalAccessor> parsedDateResult = dateStringProvided ? temporalParser.parse(dateString) : ParseResult.fail(); TemporalAccessor parsedYmdTa = parsedYMDResult.getPayload(); TemporalAccessor parsedDateTa = parsedDateResult.getPayload(); if (atomizedDateProvided && dateStringProvided && !TemporalAccessorUtils.sameOrContained(parsedYmdTa, parsedDateTa)) { boolean ambiguityResolved = false; if (parsedDateResult.getAlternativePayloads() != null) { for (TemporalAccessor possibleTa : parsedDateResult.getAlternativePayloads()) { if (TemporalAccessorUtils.sameOrContained(parsedYmdTa, possibleTa)) { parsedDateTa = possibleTa; ambiguityResolved = true; log.debug( "Ambiguous date {} matches year-month-day date {}-{}-{} for {}", dateString, year, month, day, parsedDateTa); } } } if (!ambiguityResolved) { issues.add(OccurrenceIssue.RECORDED_DATE_MISMATCH); log.debug("Date mismatch: [{} vs {}].", parsedYmdTa, parsedDateTa); } Optional<TemporalAccessor> bestResolution = TemporalAccessorUtils.bestResolution(parsedYmdTa, parsedDateTa); if (bestResolution.isPresent()) { parsedTemporalAccessor = bestResolution.get(); confidence = (parsedYmdTa == null || parsedDateTa == null) ? PROBABLE : DEFINITE; } else { return OccurrenceParseResult.fail(issues); } } else { parsedTemporalAccessor = TemporalAccessorUtils.bestResolution(parsedYmdTa, parsedDateTa).orElse(null); confidence = parsedDateTa != null ? parsedDateResult.getConfidence() : parsedYMDResult.getConfidence(); } if (!isValidDate(parsedTemporalAccessor)) { if (parsedTemporalAccessor == null) { issues.add(OccurrenceIssue.RECORDED_DATE_INVALID); } else { issues.add(OccurrenceIssue.RECORDED_DATE_UNLIKELY); } log.debug("Invalid date: [{}]].", parsedTemporalAccessor); return OccurrenceParseResult.fail(issues); } return OccurrenceParseResult.success(confidence, parsedTemporalAccessor, issues); } private  TemporalParser(List<DateComponentOrdering> orderings); }
TemporalParser implements Serializable { public OccurrenceParseResult<TemporalAccessor> parseRecordedDate( String year, String month, String day, String dateString) { boolean atomizedDateProvided = StringUtils.isNotBlank(year) || StringUtils.isNotBlank(month) || StringUtils.isNotBlank(day); boolean dateStringProvided = StringUtils.isNotBlank(dateString); if (!atomizedDateProvided && !dateStringProvided) { return OccurrenceParseResult.fail(); } Set<OccurrenceIssue> issues = EnumSet.noneOf(OccurrenceIssue.class); TemporalAccessor parsedTemporalAccessor; ParseResult.CONFIDENCE confidence; ParseResult<TemporalAccessor> parsedYMDResult = atomizedDateProvided ? temporalParser.parse(year, month, day) : ParseResult.fail(); ParseResult<TemporalAccessor> parsedDateResult = dateStringProvided ? temporalParser.parse(dateString) : ParseResult.fail(); TemporalAccessor parsedYmdTa = parsedYMDResult.getPayload(); TemporalAccessor parsedDateTa = parsedDateResult.getPayload(); if (atomizedDateProvided && dateStringProvided && !TemporalAccessorUtils.sameOrContained(parsedYmdTa, parsedDateTa)) { boolean ambiguityResolved = false; if (parsedDateResult.getAlternativePayloads() != null) { for (TemporalAccessor possibleTa : parsedDateResult.getAlternativePayloads()) { if (TemporalAccessorUtils.sameOrContained(parsedYmdTa, possibleTa)) { parsedDateTa = possibleTa; ambiguityResolved = true; log.debug( "Ambiguous date {} matches year-month-day date {}-{}-{} for {}", dateString, year, month, day, parsedDateTa); } } } if (!ambiguityResolved) { issues.add(OccurrenceIssue.RECORDED_DATE_MISMATCH); log.debug("Date mismatch: [{} vs {}].", parsedYmdTa, parsedDateTa); } Optional<TemporalAccessor> bestResolution = TemporalAccessorUtils.bestResolution(parsedYmdTa, parsedDateTa); if (bestResolution.isPresent()) { parsedTemporalAccessor = bestResolution.get(); confidence = (parsedYmdTa == null || parsedDateTa == null) ? PROBABLE : DEFINITE; } else { return OccurrenceParseResult.fail(issues); } } else { parsedTemporalAccessor = TemporalAccessorUtils.bestResolution(parsedYmdTa, parsedDateTa).orElse(null); confidence = parsedDateTa != null ? parsedDateResult.getConfidence() : parsedYMDResult.getConfidence(); } if (!isValidDate(parsedTemporalAccessor)) { if (parsedTemporalAccessor == null) { issues.add(OccurrenceIssue.RECORDED_DATE_INVALID); } else { issues.add(OccurrenceIssue.RECORDED_DATE_UNLIKELY); } log.debug("Invalid date: [{}]].", parsedTemporalAccessor); return OccurrenceParseResult.fail(issues); } return OccurrenceParseResult.success(confidence, parsedTemporalAccessor, issues); } private  TemporalParser(List<DateComponentOrdering> orderings); static TemporalParser create(List<DateComponentOrdering> orderings); static TemporalParser create(); OccurrenceParseResult<TemporalAccessor> parseRecordedDate(
      String year, String month, String day, String dateString); OccurrenceParseResult<TemporalAccessor> parseRecordedDate(String dateString); OccurrenceParseResult<TemporalAccessor> parseLocalDate(
      String dateString, Range<LocalDate> likelyRange, OccurrenceIssue unlikelyIssue); }
TemporalParser implements Serializable { public OccurrenceParseResult<TemporalAccessor> parseRecordedDate( String year, String month, String day, String dateString) { boolean atomizedDateProvided = StringUtils.isNotBlank(year) || StringUtils.isNotBlank(month) || StringUtils.isNotBlank(day); boolean dateStringProvided = StringUtils.isNotBlank(dateString); if (!atomizedDateProvided && !dateStringProvided) { return OccurrenceParseResult.fail(); } Set<OccurrenceIssue> issues = EnumSet.noneOf(OccurrenceIssue.class); TemporalAccessor parsedTemporalAccessor; ParseResult.CONFIDENCE confidence; ParseResult<TemporalAccessor> parsedYMDResult = atomizedDateProvided ? temporalParser.parse(year, month, day) : ParseResult.fail(); ParseResult<TemporalAccessor> parsedDateResult = dateStringProvided ? temporalParser.parse(dateString) : ParseResult.fail(); TemporalAccessor parsedYmdTa = parsedYMDResult.getPayload(); TemporalAccessor parsedDateTa = parsedDateResult.getPayload(); if (atomizedDateProvided && dateStringProvided && !TemporalAccessorUtils.sameOrContained(parsedYmdTa, parsedDateTa)) { boolean ambiguityResolved = false; if (parsedDateResult.getAlternativePayloads() != null) { for (TemporalAccessor possibleTa : parsedDateResult.getAlternativePayloads()) { if (TemporalAccessorUtils.sameOrContained(parsedYmdTa, possibleTa)) { parsedDateTa = possibleTa; ambiguityResolved = true; log.debug( "Ambiguous date {} matches year-month-day date {}-{}-{} for {}", dateString, year, month, day, parsedDateTa); } } } if (!ambiguityResolved) { issues.add(OccurrenceIssue.RECORDED_DATE_MISMATCH); log.debug("Date mismatch: [{} vs {}].", parsedYmdTa, parsedDateTa); } Optional<TemporalAccessor> bestResolution = TemporalAccessorUtils.bestResolution(parsedYmdTa, parsedDateTa); if (bestResolution.isPresent()) { parsedTemporalAccessor = bestResolution.get(); confidence = (parsedYmdTa == null || parsedDateTa == null) ? PROBABLE : DEFINITE; } else { return OccurrenceParseResult.fail(issues); } } else { parsedTemporalAccessor = TemporalAccessorUtils.bestResolution(parsedYmdTa, parsedDateTa).orElse(null); confidence = parsedDateTa != null ? parsedDateResult.getConfidence() : parsedYMDResult.getConfidence(); } if (!isValidDate(parsedTemporalAccessor)) { if (parsedTemporalAccessor == null) { issues.add(OccurrenceIssue.RECORDED_DATE_INVALID); } else { issues.add(OccurrenceIssue.RECORDED_DATE_UNLIKELY); } log.debug("Invalid date: [{}]].", parsedTemporalAccessor); return OccurrenceParseResult.fail(issues); } return OccurrenceParseResult.success(confidence, parsedTemporalAccessor, issues); } private  TemporalParser(List<DateComponentOrdering> orderings); static TemporalParser create(List<DateComponentOrdering> orderings); static TemporalParser create(); OccurrenceParseResult<TemporalAccessor> parseRecordedDate(
      String year, String month, String day, String dateString); OccurrenceParseResult<TemporalAccessor> parseRecordedDate(String dateString); OccurrenceParseResult<TemporalAccessor> parseLocalDate(
      String dateString, Range<LocalDate> likelyRange, OccurrenceIssue unlikelyIssue);  }