src/java.base/share/classes/java/time/Duration.java

Print this page
rev 10594 : 8055251: Re-examine Integer.parseInt and Long.parseLong methods


 407                         return create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos);
 408                     } catch (ArithmeticException ex) {
 409                         throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: overflow", text, 0).initCause(ex);
 410                     }
 411                 }
 412             }
 413         }
 414         throw new DateTimeParseException("Text cannot be parsed to a Duration", text, 0);
 415     }
 416 
 417     private static boolean charMatch(CharSequence text, int start, int end, char c) {
 418         return (start >= 0 && end == start + 1 && text.charAt(start) == c);
 419     }
 420 
 421     private static long parseNumber(CharSequence text, int start, int end, int multiplier, String errorText) {
 422         // regex limits to [-+]?[0-9]+
 423         if (start < 0 || end < 0) {
 424             return 0;
 425         }
 426         try {
 427             long val = Long.parseLong(text, 10, start, end);
 428             return Math.multiplyExact(val, multiplier);
 429         } catch (NumberFormatException | ArithmeticException ex) {
 430             throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: " + errorText, text, 0).initCause(ex);
 431         }
 432     }
 433 
 434     private static int parseFraction(CharSequence text, int start, int end, int negate) {
 435         // regex limits to [0-9]{0,9}
 436         if (start < 0 || end < 0 || end - start == 0) {
 437             return 0;
 438         }
 439         try {
 440             int fraction = Integer.parseInt(text, 10, start, end);
 441 
 442             // for number strings smaller than 9 digits, interpret as if there
 443             // were trailing zeros
 444             for (int i = end - start; i < 9; i++) {
 445                 fraction *= 10;
 446             }
 447             return fraction * negate;
 448         } catch (NumberFormatException | ArithmeticException ex) {
 449             throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: fraction", text, 0).initCause(ex);
 450         }
 451     }
 452 
 453     private static Duration create(boolean negate, long daysAsSecs, long hoursAsSecs, long minsAsSecs, long secs, int nanos) {
 454         long seconds = Math.addExact(daysAsSecs, Math.addExact(hoursAsSecs, Math.addExact(minsAsSecs, secs)));
 455         if (negate) {
 456             return ofSeconds(seconds, nanos).negated();
 457         }
 458         return ofSeconds(seconds, nanos);
 459     }
 460 




 407                         return create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos);
 408                     } catch (ArithmeticException ex) {
 409                         throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: overflow", text, 0).initCause(ex);
 410                     }
 411                 }
 412             }
 413         }
 414         throw new DateTimeParseException("Text cannot be parsed to a Duration", text, 0);
 415     }
 416 
 417     private static boolean charMatch(CharSequence text, int start, int end, char c) {
 418         return (start >= 0 && end == start + 1 && text.charAt(start) == c);
 419     }
 420 
 421     private static long parseNumber(CharSequence text, int start, int end, int multiplier, String errorText) {
 422         // regex limits to [-+]?[0-9]+
 423         if (start < 0 || end < 0) {
 424             return 0;
 425         }
 426         try {
 427             long val = Long.parseLong(text, start, end, 10);
 428             return Math.multiplyExact(val, multiplier);
 429         } catch (NumberFormatException | ArithmeticException ex) {
 430             throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: " + errorText, text, 0).initCause(ex);
 431         }
 432     }
 433 
 434     private static int parseFraction(CharSequence text, int start, int end, int negate) {
 435         // regex limits to [0-9]{0,9}
 436         if (start < 0 || end < 0 || end - start == 0) {
 437             return 0;
 438         }
 439         try {
 440             int fraction = Integer.parseInt(text, start, end, 10);
 441 
 442             // for number strings smaller than 9 digits, interpret as if there
 443             // were trailing zeros
 444             for (int i = end - start; i < 9; i++) {
 445                 fraction *= 10;
 446             }
 447             return fraction * negate;
 448         } catch (NumberFormatException | ArithmeticException ex) {
 449             throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: fraction", text, 0).initCause(ex);
 450         }
 451     }
 452 
 453     private static Duration create(boolean negate, long daysAsSecs, long hoursAsSecs, long minsAsSecs, long secs, int nanos) {
 454         long seconds = Math.addExact(daysAsSecs, Math.addExact(hoursAsSecs, Math.addExact(minsAsSecs, secs)));
 455         if (negate) {
 456             return ofSeconds(seconds, nanos).negated();
 457         }
 458         return ofSeconds(seconds, nanos);
 459     }
 460