77import java .time .OffsetDateTime ;
88import java .time .ZoneOffset ;
99import java .time .temporal .ChronoUnit ;
10- import java .util .Collection ;
1110import java .util .Map ;
1211import java .util .regex .Pattern ;
1312
@@ -58,36 +57,27 @@ public DateWrapper parse(final String textualDate) throws ParsingException {
5857 }
5958 }
6059
61- int timeAgoAmount ;
60+ return getResultFor (parseTimeAgoAmount (textualDate ), parseChronoUnit (textualDate ));
61+ }
62+
63+ private int parseTimeAgoAmount (final String textualDate ) {
6264 try {
63- timeAgoAmount = parseTimeAgoAmount (textualDate );
64- } catch (final NumberFormatException e ) {
65+ return Integer . parseInt (textualDate . replaceAll ( " \\ D+" , "" ) );
66+ } catch (final NumberFormatException ignored ) {
6567 // If there is no valid number in the textual date,
6668 // assume it is 1 (as in 'a second ago').
67- timeAgoAmount = 1 ;
69+ return 1 ;
6870 }
69-
70- final ChronoUnit chronoUnit = parseChronoUnit (textualDate );
71- return getResultFor (timeAgoAmount , chronoUnit );
72- }
73-
74- private int parseTimeAgoAmount (final String textualDate ) throws NumberFormatException {
75- return Integer .parseInt (textualDate .replaceAll ("\\ D+" , "" ));
7671 }
7772
7873 private ChronoUnit parseChronoUnit (final String textualDate ) throws ParsingException {
79- for (final Map .Entry <ChronoUnit , Collection <String >> entry
80- : patternsHolder .asMap ().entrySet ()) {
81- final ChronoUnit chronoUnit = entry .getKey ();
82-
83- for (final String agoPhrase : entry .getValue ()) {
84- if (textualDateMatches (textualDate , agoPhrase )) {
85- return chronoUnit ;
86- }
87- }
88- }
89-
90- throw new ParsingException ("Unable to parse the date: " + textualDate );
74+ return patternsHolder .asMap ().entrySet ().stream ()
75+ .filter (e -> e .getValue ().stream ()
76+ .anyMatch (agoPhrase -> textualDateMatches (textualDate , agoPhrase )))
77+ .map (Map .Entry ::getKey )
78+ .findFirst ()
79+ .orElseThrow (() ->
80+ new ParsingException ("Unable to parse the date: " + textualDate ));
9181 }
9282
9383 private boolean textualDateMatches (final String textualDate , final String agoPhrase ) {
@@ -97,24 +87,21 @@ private boolean textualDateMatches(final String textualDate, final String agoPhr
9787
9888 if (patternsHolder .wordSeparator ().isEmpty ()) {
9989 return textualDate .toLowerCase ().contains (agoPhrase .toLowerCase ());
100- } else {
101- final String escapedPhrase = Pattern . quote ( agoPhrase . toLowerCase ());
102- final String escapedSeparator ;
103- if ( patternsHolder .wordSeparator ().equals (" " )) {
90+ }
91+
92+ final String escapedPhrase = Pattern . quote ( agoPhrase . toLowerCase ()) ;
93+ final String escapedSeparator = patternsHolder .wordSeparator ().equals (" " )
10494 // From JDK8 → \h - Treat horizontal spaces as a normal one
10595 // (non-breaking space, thin space, etc.)
106- escapedSeparator = "[ \\ t\\ xA0\\ u1680\\ u180e\\ u2000-\\ u200a\\ u202f\\ u205f\\ u3000]" ;
107- } else {
108- escapedSeparator = Pattern .quote (patternsHolder .wordSeparator ());
109- }
96+ ? "[ \\ t\\ xA0\\ u1680\\ u180e\\ u2000-\\ u200a\\ u202f\\ u205f\\ u3000]"
97+ : Pattern .quote (patternsHolder .wordSeparator ());
11098
111- // (^|separator)pattern($|separator)
112- // Check if the pattern is surrounded by separators or start/end of the string.
113- final String pattern =
114- "(^|" + escapedSeparator + ")" + escapedPhrase + "($|" + escapedSeparator + ")" ;
99+ // (^|separator)pattern($|separator)
100+ // Check if the pattern is surrounded by separators or start/end of the string.
101+ final String pattern =
102+ "(^|" + escapedSeparator + ")" + escapedPhrase + "($|" + escapedSeparator + ")" ;
115103
116- return Parser .isMatch (pattern , textualDate .toLowerCase ());
117- }
104+ return Parser .isMatch (pattern , textualDate .toLowerCase ());
118105 }
119106
120107 private DateWrapper getResultFor (final int timeAgoAmount , final ChronoUnit chronoUnit ) {
0 commit comments