15289
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
* This file is available under and governed by the GNU General Public
|
|
28 |
* License version 2 only, as published by the Free Software Foundation.
|
|
29 |
* However, the following notice accompanied the original version of this
|
|
30 |
* file:
|
|
31 |
*
|
|
32 |
* Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
|
|
33 |
*
|
|
34 |
* All rights reserved.
|
|
35 |
*
|
|
36 |
* Redistribution and use in source and binary forms, with or without
|
|
37 |
* modification, are permitted provided that the following conditions are met:
|
|
38 |
*
|
|
39 |
* * Redistributions of source code must retain the above copyright notice,
|
|
40 |
* this list of conditions and the following disclaimer.
|
|
41 |
*
|
|
42 |
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
43 |
* this list of conditions and the following disclaimer in the documentation
|
|
44 |
* and/or other materials provided with the distribution.
|
|
45 |
*
|
|
46 |
* * Neither the name of JSR-310 nor the names of its contributors
|
|
47 |
* may be used to endorse or promote products derived from this software
|
|
48 |
* without specific prior written permission.
|
|
49 |
*
|
|
50 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
51 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
52 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
53 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
54 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
55 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
56 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
57 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
58 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
59 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
60 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
61 |
*/
|
|
62 |
package java.time.format;
|
|
63 |
|
17474
|
64 |
import java.time.Duration;
|
15658
|
65 |
import java.time.ZoneId;
|
|
66 |
import java.time.chrono.Chronology;
|
|
67 |
import java.time.chrono.IsoChronology;
|
15289
|
68 |
import java.time.temporal.TemporalField;
|
|
69 |
import java.util.ArrayList;
|
|
70 |
import java.util.Locale;
|
|
71 |
import java.util.Objects;
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Context object used during date and time parsing.
|
|
75 |
* <p>
|
|
76 |
* This class represents the current state of the parse.
|
|
77 |
* It has the ability to store and retrieve the parsed values and manage optional segments.
|
|
78 |
* It also provides key information to the parsing methods.
|
|
79 |
* <p>
|
16852
|
80 |
* Once parsing is complete, the {@link #toParsed()} is used to obtain the data.
|
|
81 |
* It contains a method to resolve the separate parsed fields into meaningful values.
|
15289
|
82 |
*
|
17474
|
83 |
* @implSpec
|
15289
|
84 |
* This class is a mutable context intended for use from a single thread.
|
|
85 |
* Usage of the class is thread-safe within standard parsing as a new instance of this class
|
|
86 |
* is automatically created for each parse and parsing is single-threaded
|
|
87 |
*
|
|
88 |
* @since 1.8
|
|
89 |
*/
|
16852
|
90 |
final class DateTimeParseContext {
|
15289
|
91 |
|
|
92 |
/**
|
|
93 |
* The formatter, not null.
|
|
94 |
*/
|
|
95 |
private DateTimeFormatter formatter;
|
|
96 |
/**
|
|
97 |
* Whether to parse using case sensitively.
|
|
98 |
*/
|
|
99 |
private boolean caseSensitive = true;
|
|
100 |
/**
|
|
101 |
* Whether to parse using strict rules.
|
|
102 |
*/
|
|
103 |
private boolean strict = true;
|
|
104 |
/**
|
|
105 |
* The list of parsed data.
|
|
106 |
*/
|
|
107 |
private final ArrayList<Parsed> parsed = new ArrayList<>();
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Creates a new instance of the context.
|
|
111 |
*
|
|
112 |
* @param formatter the formatter controlling the parse, not null
|
|
113 |
*/
|
|
114 |
DateTimeParseContext(DateTimeFormatter formatter) {
|
|
115 |
super();
|
|
116 |
this.formatter = formatter;
|
|
117 |
parsed.add(new Parsed());
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Creates a copy of this context.
|
17474
|
122 |
* This retains the case sensitive and strict flags.
|
15289
|
123 |
*/
|
|
124 |
DateTimeParseContext copy() {
|
17474
|
125 |
DateTimeParseContext newContext = new DateTimeParseContext(formatter);
|
|
126 |
newContext.caseSensitive = caseSensitive;
|
|
127 |
newContext.strict = strict;
|
|
128 |
return newContext;
|
15289
|
129 |
}
|
|
130 |
|
|
131 |
//-----------------------------------------------------------------------
|
|
132 |
/**
|
|
133 |
* Gets the locale.
|
|
134 |
* <p>
|
|
135 |
* This locale is used to control localization in the parse except
|
17474
|
136 |
* where localization is controlled by the DecimalStyle.
|
15289
|
137 |
*
|
|
138 |
* @return the locale, not null
|
|
139 |
*/
|
15658
|
140 |
Locale getLocale() {
|
15289
|
141 |
return formatter.getLocale();
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
17474
|
145 |
* Gets the DecimalStyle.
|
15289
|
146 |
* <p>
|
17474
|
147 |
* The DecimalStyle controls the numeric parsing.
|
15289
|
148 |
*
|
17474
|
149 |
* @return the DecimalStyle, not null
|
15289
|
150 |
*/
|
17474
|
151 |
DecimalStyle getDecimalStyle() {
|
|
152 |
return formatter.getDecimalStyle();
|
15289
|
153 |
}
|
|
154 |
|
15658
|
155 |
/**
|
|
156 |
* Gets the effective chronology during parsing.
|
|
157 |
*
|
|
158 |
* @return the effective parsing chronology, not null
|
|
159 |
*/
|
|
160 |
Chronology getEffectiveChronology() {
|
|
161 |
Chronology chrono = currentParsed().chrono;
|
|
162 |
if (chrono == null) {
|
|
163 |
chrono = formatter.getChronology();
|
|
164 |
if (chrono == null) {
|
|
165 |
chrono = IsoChronology.INSTANCE;
|
|
166 |
}
|
|
167 |
}
|
|
168 |
return chrono;
|
|
169 |
}
|
|
170 |
|
15289
|
171 |
//-----------------------------------------------------------------------
|
|
172 |
/**
|
|
173 |
* Checks if parsing is case sensitive.
|
|
174 |
*
|
|
175 |
* @return true if parsing is case sensitive, false if case insensitive
|
|
176 |
*/
|
15658
|
177 |
boolean isCaseSensitive() {
|
15289
|
178 |
return caseSensitive;
|
|
179 |
}
|
|
180 |
|
|
181 |
/**
|
|
182 |
* Sets whether the parsing is case sensitive or not.
|
|
183 |
*
|
|
184 |
* @param caseSensitive changes the parsing to be case sensitive or not from now on
|
|
185 |
*/
|
15658
|
186 |
void setCaseSensitive(boolean caseSensitive) {
|
15289
|
187 |
this.caseSensitive = caseSensitive;
|
|
188 |
}
|
|
189 |
|
15658
|
190 |
//-----------------------------------------------------------------------
|
15289
|
191 |
/**
|
|
192 |
* Helper to compare two {@code CharSequence} instances.
|
|
193 |
* This uses {@link #isCaseSensitive()}.
|
|
194 |
*
|
|
195 |
* @param cs1 the first character sequence, not null
|
|
196 |
* @param offset1 the offset into the first sequence, valid
|
|
197 |
* @param cs2 the second character sequence, not null
|
|
198 |
* @param offset2 the offset into the second sequence, valid
|
|
199 |
* @param length the length to check, valid
|
|
200 |
* @return true if equal
|
|
201 |
*/
|
15658
|
202 |
boolean subSequenceEquals(CharSequence cs1, int offset1, CharSequence cs2, int offset2, int length) {
|
15289
|
203 |
if (offset1 + length > cs1.length() || offset2 + length > cs2.length()) {
|
|
204 |
return false;
|
|
205 |
}
|
|
206 |
if (isCaseSensitive()) {
|
|
207 |
for (int i = 0; i < length; i++) {
|
|
208 |
char ch1 = cs1.charAt(offset1 + i);
|
|
209 |
char ch2 = cs2.charAt(offset2 + i);
|
|
210 |
if (ch1 != ch2) {
|
|
211 |
return false;
|
|
212 |
}
|
|
213 |
}
|
|
214 |
} else {
|
|
215 |
for (int i = 0; i < length; i++) {
|
|
216 |
char ch1 = cs1.charAt(offset1 + i);
|
|
217 |
char ch2 = cs2.charAt(offset2 + i);
|
|
218 |
if (ch1 != ch2 && Character.toUpperCase(ch1) != Character.toUpperCase(ch2) &&
|
|
219 |
Character.toLowerCase(ch1) != Character.toLowerCase(ch2)) {
|
|
220 |
return false;
|
|
221 |
}
|
|
222 |
}
|
|
223 |
}
|
|
224 |
return true;
|
|
225 |
}
|
|
226 |
|
15658
|
227 |
/**
|
|
228 |
* Helper to compare two {@code char}.
|
|
229 |
* This uses {@link #isCaseSensitive()}.
|
|
230 |
*
|
|
231 |
* @param ch1 the first character
|
|
232 |
* @param ch2 the second character
|
|
233 |
* @return true if equal
|
|
234 |
*/
|
|
235 |
boolean charEquals(char ch1, char ch2) {
|
|
236 |
if (isCaseSensitive()) {
|
|
237 |
return ch1 == ch2;
|
|
238 |
}
|
|
239 |
return charEqualsIgnoreCase(ch1, ch2);
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Compares two characters ignoring case.
|
|
244 |
*
|
|
245 |
* @param c1 the first
|
|
246 |
* @param c2 the second
|
|
247 |
* @return true if equal
|
|
248 |
*/
|
|
249 |
static boolean charEqualsIgnoreCase(char c1, char c2) {
|
|
250 |
return c1 == c2 ||
|
|
251 |
Character.toUpperCase(c1) == Character.toUpperCase(c2) ||
|
|
252 |
Character.toLowerCase(c1) == Character.toLowerCase(c2);
|
|
253 |
}
|
|
254 |
|
15289
|
255 |
//-----------------------------------------------------------------------
|
|
256 |
/**
|
|
257 |
* Checks if parsing is strict.
|
|
258 |
* <p>
|
|
259 |
* Strict parsing requires exact matching of the text and sign styles.
|
|
260 |
*
|
|
261 |
* @return true if parsing is strict, false if lenient
|
|
262 |
*/
|
15658
|
263 |
boolean isStrict() {
|
15289
|
264 |
return strict;
|
|
265 |
}
|
|
266 |
|
|
267 |
/**
|
|
268 |
* Sets whether parsing is strict or lenient.
|
|
269 |
*
|
|
270 |
* @param strict changes the parsing to be strict or lenient from now on
|
|
271 |
*/
|
15658
|
272 |
void setStrict(boolean strict) {
|
15289
|
273 |
this.strict = strict;
|
|
274 |
}
|
|
275 |
|
|
276 |
//-----------------------------------------------------------------------
|
|
277 |
/**
|
|
278 |
* Starts the parsing of an optional segment of the input.
|
|
279 |
*/
|
|
280 |
void startOptional() {
|
|
281 |
parsed.add(currentParsed().copy());
|
|
282 |
}
|
|
283 |
|
|
284 |
/**
|
|
285 |
* Ends the parsing of an optional segment of the input.
|
|
286 |
*
|
|
287 |
* @param successful whether the optional segment was successfully parsed
|
|
288 |
*/
|
|
289 |
void endOptional(boolean successful) {
|
|
290 |
if (successful) {
|
|
291 |
parsed.remove(parsed.size() - 2);
|
|
292 |
} else {
|
|
293 |
parsed.remove(parsed.size() - 1);
|
|
294 |
}
|
|
295 |
}
|
|
296 |
|
|
297 |
//-----------------------------------------------------------------------
|
|
298 |
/**
|
|
299 |
* Gets the currently active temporal objects.
|
|
300 |
*
|
|
301 |
* @return the current temporal objects, not null
|
|
302 |
*/
|
|
303 |
private Parsed currentParsed() {
|
|
304 |
return parsed.get(parsed.size() - 1);
|
|
305 |
}
|
|
306 |
|
16852
|
307 |
/**
|
|
308 |
* Gets the result of the parse.
|
|
309 |
*
|
|
310 |
* @return the result of the parse, not null
|
|
311 |
*/
|
|
312 |
Parsed toParsed() {
|
|
313 |
Parsed parsed = currentParsed();
|
|
314 |
parsed.effectiveChrono = getEffectiveChronology();
|
|
315 |
return parsed;
|
|
316 |
}
|
|
317 |
|
15289
|
318 |
//-----------------------------------------------------------------------
|
|
319 |
/**
|
|
320 |
* Gets the first value that was parsed for the specified field.
|
|
321 |
* <p>
|
|
322 |
* This searches the results of the parse, returning the first value found
|
|
323 |
* for the specified field. No attempt is made to derive a value.
|
|
324 |
* The field may have an out of range value.
|
|
325 |
* For example, the day-of-month might be set to 50, or the hour to 1000.
|
|
326 |
*
|
|
327 |
* @param field the field to query from the map, null returns null
|
|
328 |
* @return the value mapped to the specified field, null if field was not parsed
|
|
329 |
*/
|
15658
|
330 |
Long getParsed(TemporalField field) {
|
|
331 |
return currentParsed().fieldValues.get(field);
|
15289
|
332 |
}
|
|
333 |
|
|
334 |
/**
|
|
335 |
* Stores the parsed field.
|
|
336 |
* <p>
|
|
337 |
* This stores a field-value pair that has been parsed.
|
|
338 |
* The value stored may be out of range for the field - no checks are performed.
|
|
339 |
*
|
|
340 |
* @param field the field to set in the field-value map, not null
|
|
341 |
* @param value the value to set in the field-value map
|
15658
|
342 |
* @param errorPos the position of the field being parsed
|
|
343 |
* @param successPos the position after the field being parsed
|
|
344 |
* @return the new position
|
15289
|
345 |
*/
|
15658
|
346 |
int setParsedField(TemporalField field, long value, int errorPos, int successPos) {
|
15289
|
347 |
Objects.requireNonNull(field, "field");
|
15658
|
348 |
Long old = currentParsed().fieldValues.put(field, value);
|
|
349 |
return (old != null && old.longValue() != value) ? ~errorPos : successPos;
|
15289
|
350 |
}
|
|
351 |
|
|
352 |
/**
|
15658
|
353 |
* Stores the parsed chronology.
|
15289
|
354 |
* <p>
|
15658
|
355 |
* This stores the chronology that has been parsed.
|
|
356 |
* No validation is performed other than ensuring it is not null.
|
15289
|
357 |
*
|
15658
|
358 |
* @param chrono the parsed chronology, not null
|
15289
|
359 |
*/
|
15658
|
360 |
void setParsed(Chronology chrono) {
|
|
361 |
Objects.requireNonNull(chrono, "chrono");
|
|
362 |
currentParsed().chrono = chrono;
|
|
363 |
}
|
|
364 |
|
|
365 |
/**
|
|
366 |
* Stores the parsed zone.
|
|
367 |
* <p>
|
|
368 |
* This stores the zone that has been parsed.
|
|
369 |
* No validation is performed other than ensuring it is not null.
|
|
370 |
*
|
|
371 |
* @param zone the parsed zone, not null
|
|
372 |
*/
|
|
373 |
void setParsed(ZoneId zone) {
|
|
374 |
Objects.requireNonNull(zone, "zone");
|
|
375 |
currentParsed().zone = zone;
|
15289
|
376 |
}
|
|
377 |
|
17474
|
378 |
/**
|
|
379 |
* Stores the parsed leap second.
|
|
380 |
*/
|
|
381 |
void setParsedLeapSecond() {
|
|
382 |
currentParsed().leapSecond = true;
|
|
383 |
}
|
|
384 |
|
15289
|
385 |
//-----------------------------------------------------------------------
|
|
386 |
/**
|
|
387 |
* Returns a string version of the context for debugging.
|
|
388 |
*
|
|
389 |
* @return a string representation of the context data, not null
|
|
390 |
*/
|
|
391 |
@Override
|
|
392 |
public String toString() {
|
|
393 |
return currentParsed().toString();
|
|
394 |
}
|
|
395 |
|
|
396 |
}
|