public static IntegerRange parseRange ( String stringRange , int minValue ) { final String sepPattern = "[,;\\-:]" ; //$NON-NLS-1$ try { final Matcher matcher = Pattern . compile ( "^\\s*" //$NON-NLS-1$ + "(?:(?<left>[0-9]+)\\s*(?:(?<sep1>" + sepPattern + ")\\s*(?<right1>[0-9]+)?)?)" //$NON-NLS-1$ //$NON-NLS-2$ + "|(?:(?<sep2>" + sepPattern + ")\\s*(?<right2>[0-9]+))" //$NON-NLS-1$ //$NON-NLS-2$ + "\\s*$" ) //$NON-NLS-1$ . matcher ( stringRange ) ; if ( matcher . matches ( ) ) { final String left = matcher . group ( "left" ) ; //$NON-NLS-1$ final String sep = select ( matcher . group ( "sep1" ) , matcher . group ( "sep2" ) ) ; //$NON-NLS-1$ //$NON-NLS-2$ final String right = select ( matcher . group ( "right1" ) , matcher . group ( "right2" ) ) ; //$NON-NLS-1$ //$NON-NLS-2$ if ( Strings . isEmpty ( left ) ) { if ( ! Strings . isEmpty ( sep ) && ! Strings . isEmpty ( right ) ) { return new IntegerRange ( minValue , Math . max ( minValue , Integer . valueOf ( right ) ) ) ; } } else { final int leftValue = Math . max ( minValue , Integer . valueOf ( left ) ) ; if ( Strings . isEmpty ( sep ) ) { return new IntegerRange ( leftValue , leftValue ) ; } if ( Strings . isEmpty ( right ) ) { return new IntegerRange ( leftValue , Integer . MAX_VALUE ) ; } final int rightValue = Math . max ( minValue , Integer . valueOf ( right ) ) ; if ( rightValue < leftValue ) { return new IntegerRange ( rightValue , leftValue ) ; } return new IntegerRange ( leftValue , rightValue ) ; } } } catch ( Throwable exception ) { throw new IllegalArgumentException ( MessageFormat . format ( Messages . GenerateMojo_4 , stringRange ) , exception ) ; } throw new IllegalArgumentException ( MessageFormat . format ( Messages . GenerateMojo_4 , stringRange ) ) ; }
Parse a range of integers .