public static final int parse ( String durationString ) { durationString = durationString . toLowerCase ( ) ; Matcher matcher = durationPattern . matcher ( durationString ) ; int currentUnitIndex = - 1 ; int seconds = 0 ; Boolean validDuration = null ; while ( matcher . find ( ) && validDuration != Boolean . FALSE ) { validDuration = true ; int numGroups = matcher . groupCount ( ) ; if ( numGroups == 3 ) { String unit = matcher . group ( 3 ) ; int nextUnitIndex = getUnitIndex ( unit ) ; if ( nextUnitIndex > currentUnitIndex ) { currentUnitIndex = nextUnitIndex ; try { seconds += Long . parseLong ( matcher . group ( 2 ) ) * TIME_UNIT_MULTIPLIERS [ nextUnitIndex ] ; } catch ( NumberFormatException nfe ) { validDuration = false ; } } else { validDuration = false ; } } else { validDuration = false ; } } if ( validDuration != Boolean . TRUE ) { throw new IllegalArgumentException ( String . format ( "'%s' is not a valid duration" , durationString ) ) ; } return ( seconds ) ; }
Parses a human readable duration string and calculates the number of seconds it represents .