Coverage Report - joptsimple.OptionException
 
Classes in this File Line Coverage Branch Coverage Complexity
OptionException
100%
30/30
100%
6/6
1.214
 
 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;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.Collection;
 30  
 import java.util.Iterator;
 31  
 import java.util.LinkedHashSet;
 32  
 import java.util.List;
 33  
 import java.util.Locale;
 34  
 import java.util.Set;
 35  
 
 36  
 import joptsimple.internal.Strings;
 37  
 
 38  
 import static java.util.Collections.*;
 39  
 import static joptsimple.internal.Messages.*;
 40  
 
 41  
 /**
 42  
  * Thrown when a problem occurs during option parsing.
 43  
  *
 44  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 45  
  */
 46  
 public abstract class OptionException extends RuntimeException {
 47  
     private static final long serialVersionUID = -1L;
 48  
 
 49  80
     private final List<String> options = new ArrayList<String>();
 50  
 
 51  43
     protected OptionException( List<String> options ) {
 52  43
         this.options.addAll( options );
 53  43
     }
 54  
 
 55  28
     protected OptionException( Collection<? extends OptionSpec<?>> options ) {
 56  28
         this.options.addAll( specsToStrings( options ) );
 57  28
     }
 58  
 
 59  
     protected OptionException( Collection<? extends OptionSpec<?>> options, Throwable cause ) {
 60  9
         super( cause );
 61  9
         this.options.addAll( specsToStrings( options ) );
 62  9
     }
 63  
 
 64  
     private List<String> specsToStrings( Collection<? extends OptionSpec<?>> options ) {
 65  37
         List<String> strings = new ArrayList<String>();
 66  37
         for ( OptionSpec<?> each : options )
 67  47
             strings.add( specToString( each ) );
 68  37
         return strings;
 69  
     }
 70  
 
 71  
     private String specToString( OptionSpec<?> option ) {
 72  47
         return Strings.join( new ArrayList<String>( option.options() ), "/" );
 73  
     }
 74  
 
 75  
     /**
 76  
      * Gives the option being considered when the exception was created.
 77  
      *
 78  
      * @return the option being considered when the exception was created
 79  
      */
 80  
     public List<String> options() {
 81  30
         return unmodifiableList( options );
 82  
     }
 83  
 
 84  
     protected final String singleOptionString() {
 85  21
         return singleOptionString( options.get( 0 ) );
 86  
     }
 87  
 
 88  
     protected final String singleOptionString( String option ) {
 89  46
         return option;
 90  
     }
 91  
 
 92  
     protected final String multipleOptionString() {
 93  13
         StringBuilder buffer = new StringBuilder( "[" );
 94  
 
 95  13
         Set<String> asSet = new LinkedHashSet<String>( options );
 96  13
         for ( Iterator<String> iter = asSet.iterator(); iter.hasNext(); ) {
 97  25
             buffer.append( singleOptionString(iter.next()) );
 98  25
             if ( iter.hasNext() )
 99  12
                 buffer.append( ", " );
 100  
         }
 101  
 
 102  13
         buffer.append( ']' );
 103  
 
 104  13
         return buffer.toString();
 105  
     }
 106  
 
 107  
     static OptionException unrecognizedOption( String option ) {
 108  15
         return new UnrecognizedOptionException( option );
 109  
     }
 110  
 
 111  
     @Override
 112  
     public final String getMessage() {
 113  26
         return localizedMessage( Locale.getDefault() );
 114  
     }
 115  
 
 116  
     final String localizedMessage( Locale locale ) {
 117  34
         return formattedMessage( locale );
 118  
     }
 119  
 
 120  
     private String formattedMessage( Locale locale ) {
 121  34
         return message( locale, "joptsimple.ExceptionMessages", getClass(), "message", messageArguments() );
 122  
     }
 123  
 
 124  
     abstract Object[] messageArguments();
 125  
 }