1717import java .time .format .DateTimeParseException ;
1818import java .time .temporal .ChronoField ;
1919import java .time .temporal .TemporalAccessor ;
20- import java .util .HashMap ;
20+ import java .util .EnumMap ;
2121import java .util .Map ;
2222
23+ import org .eclipse .jgit .annotations .Nullable ;
2324import org .eclipse .jgit .internal .JGitText ;
2425
2526/**
3536 */
3637public class GitTimeParser {
3738
38- private static final Map <ParseableSimpleDateFormat , DateTimeFormatter > formatCache = new HashMap <>();
39+ private static final Map <ParseableSimpleDateFormat , DateTimeFormatter > formatCache = new EnumMap <>(
40+ ParseableSimpleDateFormat .class );
3941
4042 // An enum of all those formats which this parser can parse with the help of
4143 // a DateTimeFormatter. There are other formats (e.g. the relative formats
@@ -59,6 +61,10 @@ enum ParseableSimpleDateFormat {
5961 }
6062 }
6163
64+ private GitTimeParser () {
65+ // This class is not supposed to be instantiated
66+ }
67+
6268 /**
6369 * Parses a string into a {@link java.time.LocalDateTime} using the default
6470 * locale. Since this parser also supports relative formats (e.g.
@@ -95,25 +101,27 @@ public static LocalDateTime parse(String dateStr) throws ParseException {
95101 static LocalDateTime parse (String dateStr , LocalDateTime now )
96102 throws ParseException {
97103 dateStr = dateStr .trim ();
98- LocalDateTime ret ;
99104
100- if ("never" .equalsIgnoreCase (dateStr )) //$NON-NLS-1$
105+ if (dateStr .equalsIgnoreCase ("never" )) { //$NON-NLS-1$
101106 return LocalDateTime .MAX ;
102- ret = parse_relative (dateStr , now );
103- if (ret != null )
107+ }
108+ LocalDateTime ret = parseRelative (dateStr , now );
109+ if (ret != null ) {
104110 return ret ;
111+ }
105112 for (ParseableSimpleDateFormat f : ParseableSimpleDateFormat .values ()) {
106113 try {
107- return parse_simple (dateStr , f );
114+ return parseSimple (dateStr , f );
108115 } catch (DateTimeParseException e ) {
109116 // simply proceed with the next parser
110117 }
111118 }
112119 ParseableSimpleDateFormat [] values = ParseableSimpleDateFormat .values ();
113120 StringBuilder allFormats = new StringBuilder ("\" " ) //$NON-NLS-1$
114121 .append (values [0 ].formatStr );
115- for (int i = 1 ; i < values .length ; i ++)
122+ for (int i = 1 ; i < values .length ; i ++) {
116123 allFormats .append ("\" , \" " ).append (values [i ].formatStr ); //$NON-NLS-1$
124+ }
117125 allFormats .append ("\" " ); //$NON-NLS-1$
118126 throw new ParseException (
119127 MessageFormat .format (JGitText .get ().cannotParseDate , dateStr ,
@@ -122,10 +130,11 @@ static LocalDateTime parse(String dateStr, LocalDateTime now)
122130 }
123131
124132 // tries to parse a string with the formats supported by DateTimeFormatter
125- private static LocalDateTime parse_simple (String dateStr ,
133+ private static LocalDateTime parseSimple (String dateStr ,
126134 ParseableSimpleDateFormat f ) throws DateTimeParseException {
127135 DateTimeFormatter dateFormat = formatCache .computeIfAbsent (f ,
128- format -> DateTimeFormatter .ofPattern (f .formatStr )
136+ format -> DateTimeFormatter
137+ .ofPattern (f .formatStr )
129138 .withLocale (SystemReader .getInstance ().getLocale ()));
130139 TemporalAccessor parsed = dateFormat .parse (dateStr );
131140 return parsed .isSupported (ChronoField .HOUR_OF_DAY )
@@ -135,25 +144,27 @@ private static LocalDateTime parse_simple(String dateStr,
135144
136145 // tries to parse a string with a relative time specification
137146 @ SuppressWarnings ("nls" )
138- private static LocalDateTime parse_relative (String dateStr ,
147+ @ Nullable
148+ private static LocalDateTime parseRelative (String dateStr ,
139149 LocalDateTime now ) {
140150 // check for the static words "yesterday" or "now"
141- if ("now" .equals (dateStr )) {
151+ if (dateStr .equals ("now" )) {
142152 return now ;
143153 }
144154
145- if ("yesterday" .equals (dateStr )) {
155+ if (dateStr .equals ("yesterday" )) {
146156 return now .minusDays (1 );
147157 }
148158
149159 // parse constructs like "3 days ago", "5.week.2.day.ago"
150- String [] parts = dateStr .split ("\\ .| " );
160+ String [] parts = dateStr .split ("\\ .| " , - 1 );
151161 int partsLength = parts .length ;
152162 // check we have an odd number of parts (at least 3) and that the last
153163 // part is "ago"
154164 if (partsLength < 3 || (partsLength & 1 ) == 0
155- || !"ago" . equals ( parts [parts .length - 1 ]))
165+ || !parts [parts .length - 1 ]. equals ( "ago" )) {
156166 return null ;
167+ }
157168 int number ;
158169 for (int i = 0 ; i < parts .length - 2 ; i += 2 ) {
159170 try {
0 commit comments