public static final long parseDuration ( String durationStr , long defaultValue ) { durationStr = durationStr . toLowerCase ( ) . trim ( ) ; Matcher matcher = DURATION_NUMBER_AND_UNIT_PATTERN . matcher ( durationStr ) ; long millis = 0 ; boolean matched = false ; while ( matcher . find ( ) ) { long number = Long . valueOf ( matcher . group ( 1 ) ) . longValue ( ) ; String unit = matcher . group ( 2 ) ; long multiplier = 0 ; for ( int j = 0 ; j < DURATION_UNTIS . length ; j ++ ) { if ( unit . equals ( DURATION_UNTIS [ j ] ) ) { multiplier = DURATION_MULTIPLIERS [ j ] ; break ; } } if ( multiplier == 0 ) { LOG . warn ( "parseDuration: Unknown unit " + unit ) ; } else { matched = true ; } millis += number * multiplier ; } if ( ! matched ) { millis = defaultValue ; } return millis ; }
Parses a duration and returns the corresponding number of milliseconds .