Coverage Report - joptsimple.util.RegexMatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
RegexMatcher
100%
12/12
100%
2/2
1.333
 
 1  
 /*
 2  
  The MIT License
 3  
 
 4  
  Copyright (c) 2004-2015 Paul R. Holser, Jr.
 5  
 
 6  
  Permission is hereby granted, free of charge, to any person obtaining
 7  
  a copy of this software and associated documentation files (the
 8  
  "Software"), to deal in the Software without restriction, including
 9  
  without limitation the rights to use, copy, modify, merge, publish,
 10  
  distribute, sublicense, and/or sell copies of the Software, and to
 11  
  permit persons to whom the Software is furnished to do so, subject to
 12  
  the following conditions:
 13  
 
 14  
  The above copyright notice and this permission notice shall be
 15  
  included in all copies or substantial portions of the Software.
 16  
 
 17  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 18  
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 19  
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 20  
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 21  
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 22  
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 23  
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 24  
 */
 25  
 
 26  
 package joptsimple.util;
 27  
 
 28  
 import java.util.Locale;
 29  
 import java.util.regex.Pattern;
 30  
 
 31  
 import static java.util.regex.Pattern.*;
 32  
 import static joptsimple.internal.Messages.message;
 33  
 
 34  
 import joptsimple.ValueConversionException;
 35  
 import joptsimple.ValueConverter;
 36  
 
 37  
 /**
 38  
  * Ensures that values entirely match a regular expression.
 39  
  *
 40  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 41  
  */
 42  2
 public class RegexMatcher implements ValueConverter<String> {
 43  
     private final Pattern pattern;
 44  
 
 45  
     /**
 46  
      * Creates a matcher that uses the given regular expression, modified by the given flags.
 47  
      *
 48  
      * @param pattern the regular expression pattern
 49  
      * @param flags modifying regex flags
 50  
      * @throws IllegalArgumentException if bit values other than those corresponding to the defined match flags are
 51  
      * set in {@code flags}
 52  
      * @throws java.util.regex.PatternSyntaxException if the expression's syntax is invalid
 53  
      */
 54  9
     public RegexMatcher( String pattern, int flags ) {
 55  9
         this.pattern = compile( pattern, flags );
 56  9
     }
 57  
 
 58  
     /**
 59  
      * Gives a matcher that uses the given regular expression.
 60  
      *
 61  
      * @param pattern the regular expression pattern
 62  
      * @return the new converter
 63  
      * @throws java.util.regex.PatternSyntaxException if the expression's syntax is invalid
 64  
      */
 65  
     public static ValueConverter<String> regex( String pattern ) {
 66  2
         return new RegexMatcher( pattern, 0 );
 67  
     }
 68  
 
 69  
     public String convert( String value ) {
 70  5
         if ( !pattern.matcher( value ).matches() ) {
 71  2
             raiseValueConversionFailure( value );
 72  
         }
 73  
 
 74  3
         return value;
 75  
     }
 76  
 
 77  
     public Class<String> valueType() {
 78  1
         return String.class;
 79  
     }
 80  
 
 81  
     public String valuePattern() {
 82  1
         return pattern.pattern();
 83  
     }
 84  
 
 85  
     private void raiseValueConversionFailure( String value ) {
 86  2
         String message = message(
 87  
             Locale.getDefault(),
 88  
             "joptsimple.ExceptionMessages",
 89  
             RegexMatcher.class,
 90  
             "message",
 91  
             value,
 92  
             pattern.pattern() );
 93  2
         throw new ValueConversionException( message );
 94  
     }
 95  
 }