17
17
import java .time .format .DateTimeParseException ;
18
18
import java .time .temporal .ChronoField ;
19
19
import java .time .temporal .TemporalAccessor ;
20
- import java .util .HashMap ;
20
+ import java .util .EnumMap ;
21
21
import java .util .Map ;
22
22
23
+ import org .eclipse .jgit .annotations .Nullable ;
23
24
import org .eclipse .jgit .internal .JGitText ;
24
25
25
26
/**
35
36
*/
36
37
public class GitTimeParser {
37
38
38
- private static final Map <ParseableSimpleDateFormat , DateTimeFormatter > formatCache = new HashMap <>();
39
+ private static final Map <ParseableSimpleDateFormat , DateTimeFormatter > formatCache = new EnumMap <>(
40
+ ParseableSimpleDateFormat .class );
39
41
40
42
// An enum of all those formats which this parser can parse with the help of
41
43
// a DateTimeFormatter. There are other formats (e.g. the relative formats
@@ -59,6 +61,10 @@ enum ParseableSimpleDateFormat {
59
61
}
60
62
}
61
63
64
+ private GitTimeParser () {
65
+ // This class is not supposed to be instantiated
66
+ }
67
+
62
68
/**
63
69
* Parses a string into a {@link java.time.LocalDateTime} using the default
64
70
* locale. Since this parser also supports relative formats (e.g.
@@ -95,25 +101,27 @@ public static LocalDateTime parse(String dateStr) throws ParseException {
95
101
static LocalDateTime parse (String dateStr , LocalDateTime now )
96
102
throws ParseException {
97
103
dateStr = dateStr .trim ();
98
- LocalDateTime ret ;
99
104
100
- if ("never" .equalsIgnoreCase (dateStr )) //$NON-NLS-1$
105
+ if (dateStr .equalsIgnoreCase ("never" )) { //$NON-NLS-1$
101
106
return LocalDateTime .MAX ;
102
- ret = parse_relative (dateStr , now );
103
- if (ret != null )
107
+ }
108
+ LocalDateTime ret = parseRelative (dateStr , now );
109
+ if (ret != null ) {
104
110
return ret ;
111
+ }
105
112
for (ParseableSimpleDateFormat f : ParseableSimpleDateFormat .values ()) {
106
113
try {
107
- return parse_simple (dateStr , f );
114
+ return parseSimple (dateStr , f );
108
115
} catch (DateTimeParseException e ) {
109
116
// simply proceed with the next parser
110
117
}
111
118
}
112
119
ParseableSimpleDateFormat [] values = ParseableSimpleDateFormat .values ();
113
120
StringBuilder allFormats = new StringBuilder ("\" " ) //$NON-NLS-1$
114
121
.append (values [0 ].formatStr );
115
- for (int i = 1 ; i < values .length ; i ++)
122
+ for (int i = 1 ; i < values .length ; i ++) {
116
123
allFormats .append ("\" , \" " ).append (values [i ].formatStr ); //$NON-NLS-1$
124
+ }
117
125
allFormats .append ("\" " ); //$NON-NLS-1$
118
126
throw new ParseException (
119
127
MessageFormat .format (JGitText .get ().cannotParseDate , dateStr ,
@@ -122,10 +130,11 @@ static LocalDateTime parse(String dateStr, LocalDateTime now)
122
130
}
123
131
124
132
// 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 ,
126
134
ParseableSimpleDateFormat f ) throws DateTimeParseException {
127
135
DateTimeFormatter dateFormat = formatCache .computeIfAbsent (f ,
128
- format -> DateTimeFormatter .ofPattern (f .formatStr )
136
+ format -> DateTimeFormatter
137
+ .ofPattern (f .formatStr )
129
138
.withLocale (SystemReader .getInstance ().getLocale ()));
130
139
TemporalAccessor parsed = dateFormat .parse (dateStr );
131
140
return parsed .isSupported (ChronoField .HOUR_OF_DAY )
@@ -135,25 +144,27 @@ private static LocalDateTime parse_simple(String dateStr,
135
144
136
145
// tries to parse a string with a relative time specification
137
146
@ SuppressWarnings ("nls" )
138
- private static LocalDateTime parse_relative (String dateStr ,
147
+ @ Nullable
148
+ private static LocalDateTime parseRelative (String dateStr ,
139
149
LocalDateTime now ) {
140
150
// check for the static words "yesterday" or "now"
141
- if ("now" .equals (dateStr )) {
151
+ if (dateStr .equals ("now" )) {
142
152
return now ;
143
153
}
144
154
145
- if ("yesterday" .equals (dateStr )) {
155
+ if (dateStr .equals ("yesterday" )) {
146
156
return now .minusDays (1 );
147
157
}
148
158
149
159
// parse constructs like "3 days ago", "5.week.2.day.ago"
150
- String [] parts = dateStr .split ("\\ .| " );
160
+ String [] parts = dateStr .split ("\\ .| " , - 1 );
151
161
int partsLength = parts .length ;
152
162
// check we have an odd number of parts (at least 3) and that the last
153
163
// part is "ago"
154
164
if (partsLength < 3 || (partsLength & 1 ) == 0
155
- || !"ago" . equals ( parts [parts .length - 1 ]))
165
+ || !parts [parts .length - 1 ]. equals ( "ago" )) {
156
166
return null ;
167
+ }
157
168
int number ;
158
169
for (int i = 0 ; i < parts .length - 2 ; i += 2 ) {
159
170
try {
0 commit comments