Coverage Report - joptsimple.util.DateConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
DateConverter
100%
22/22
100%
8/8
2
 
 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.text.DateFormat;
 29  
 import java.text.ParsePosition;
 30  
 import java.text.SimpleDateFormat;
 31  
 import java.util.Date;
 32  
 import java.util.Locale;
 33  
 
 34  
 import joptsimple.ValueConversionException;
 35  
 import joptsimple.ValueConverter;
 36  
 import joptsimple.internal.Messages;
 37  
 
 38  
 /**
 39  
  * Converts values to {@link Date}s using a {@link DateFormat} object.
 40  
  *
 41  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 42  
  */
 43  3
 public class DateConverter implements ValueConverter<Date> {
 44  
     private final DateFormat formatter;
 45  
 
 46  
     /**
 47  
      * Creates a converter that uses the given date formatter/parser.
 48  
      *
 49  
      * @param formatter the formatter/parser to use
 50  
      * @throws NullPointerException if {@code formatter} is {@code null}
 51  
      */
 52  16
     public DateConverter( DateFormat formatter ) {
 53  16
         if ( formatter == null )
 54  1
             throw new NullPointerException( "illegal null formatter" );
 55  
 
 56  15
         this.formatter = formatter;
 57  15
     }
 58  
 
 59  
     /**
 60  
      * Creates a converter that uses a {@link SimpleDateFormat} with the given date/time pattern.  The date formatter
 61  
      * created is not {@link SimpleDateFormat#setLenient(boolean) lenient}.
 62  
      *
 63  
      * @param pattern expected date/time pattern
 64  
      * @return the new converter
 65  
      * @throws NullPointerException if {@code pattern} is {@code null}
 66  
      * @throws IllegalArgumentException if {@code pattern} is invalid
 67  
      */
 68  
     public static DateConverter datePattern( String pattern ) {
 69  9
         SimpleDateFormat formatter = new SimpleDateFormat( pattern );
 70  8
         formatter.setLenient( false );
 71  
 
 72  8
         return new DateConverter( formatter );
 73  
     }
 74  
 
 75  
     public Date convert( String value ) {
 76  8
         ParsePosition position = new ParsePosition( 0 );
 77  
 
 78  8
         Date date = formatter.parse( value, position );
 79  8
         if ( position.getIndex() != value.length() )
 80  4
             throw new ValueConversionException( message( value ) );
 81  
 
 82  4
         return date;
 83  
     }
 84  
 
 85  
     public Class<Date> valueType() {
 86  1
         return Date.class;
 87  
     }
 88  
 
 89  
     public String valuePattern() {
 90  6
         return formatter instanceof SimpleDateFormat
 91  
             ? ( (SimpleDateFormat) formatter ).toPattern()
 92  
             : "";
 93  
     }
 94  
 
 95  
     private String message( String value ) {
 96  
         String key;
 97  
         Object[] arguments;
 98  
 
 99  4
         if ( formatter instanceof SimpleDateFormat ) {
 100  3
             key = "with.pattern.message";
 101  3
             arguments = new Object[] { value, ( (SimpleDateFormat) formatter ).toPattern() };
 102  
         } else {
 103  1
             key = "without.pattern.message";
 104  1
             arguments = new Object[] { value };
 105  
         }
 106  
 
 107  4
         return Messages.message(
 108  
             Locale.getDefault(),
 109  
             "joptsimple.ExceptionMessages",
 110  
             DateConverter.class,
 111  
             key,
 112  
             arguments );
 113  
     }
 114  
 }