Coverage Report - joptsimple.AbstractOptionSpec
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractOptionSpec
100%
45/45
100%
12/12
1.824
 
 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.List;
 30  
 
 31  
 import static java.util.Collections.*;
 32  
 
 33  
 import joptsimple.internal.Reflection;
 34  
 import joptsimple.internal.ReflectionException;
 35  
 
 36  
 import static joptsimple.internal.Strings.*;
 37  
 
 38  
 /**
 39  
  * @param <V> represents the type of the arguments this option accepts
 40  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 41  
  */
 42  
 abstract class AbstractOptionSpec<V> implements OptionSpec<V>, OptionDescriptor {
 43  1946
     private final List<String> options = new ArrayList<String>();
 44  
     private final String description;
 45  
     private boolean forHelp;
 46  
 
 47  
     protected AbstractOptionSpec( String option ) {
 48  281
         this( singletonList( option ), EMPTY );
 49  281
     }
 50  
 
 51  1946
     protected AbstractOptionSpec( List<String> options, String description ) {
 52  1946
         arrangeOptions( options );
 53  
 
 54  1946
         this.description = description;
 55  1946
     }
 56  
 
 57  
     public final List<String> options() {
 58  4678
         return unmodifiableList( options );
 59  
     }
 60  
 
 61  
     public final List<V> values( OptionSet detectedOptions ) {
 62  84
         return detectedOptions.valuesOf( this );
 63  
     }
 64  
 
 65  
     public final V value( OptionSet detectedOptions ) {
 66  24
         return detectedOptions.valueOf( this );
 67  
     }
 68  
 
 69  
     public String description() {
 70  522
         return description;
 71  
     }
 72  
 
 73  
     public final AbstractOptionSpec<V> forHelp() {
 74  33
         forHelp = true;
 75  33
         return this;
 76  
     }
 77  
 
 78  
     public final boolean isForHelp() {
 79  909
         return forHelp;
 80  
     }
 81  
 
 82  
     public boolean representsNonOptions() {
 83  132
         return false;
 84  
     }
 85  
 
 86  
     protected abstract V convert( String argument );
 87  
 
 88  
     protected V convertWith( ValueConverter<V> converter, String argument ) {
 89  
         try {
 90  635
             return Reflection.convertWith( converter, argument );
 91  
         }
 92  5
         catch ( ReflectionException ex ) {
 93  5
             throw new OptionArgumentConversionException( this, argument, ex );
 94  
         }
 95  2
         catch ( ValueConversionException ex ) {
 96  2
             throw new OptionArgumentConversionException( this, argument, ex );
 97  
         }
 98  
     }
 99  
 
 100  
     protected String argumentTypeIndicatorFrom( ValueConverter<V> converter ) {
 101  125
         if ( converter == null )
 102  77
             return null;
 103  
 
 104  48
         String pattern = converter.valuePattern();
 105  48
         return pattern == null ? converter.valueType().getName() : pattern;
 106  
     }
 107  
 
 108  
     abstract void handleOption( OptionParser parser, ArgumentList arguments, OptionSet detectedOptions,
 109  
         String detectedArgument );
 110  
 
 111  
     private void arrangeOptions( List<String> unarranged ) {
 112  1946
         if ( unarranged.size() == 1 ) {
 113  1751
             options.addAll( unarranged );
 114  1751
             return;
 115  
         }
 116  
 
 117  195
         List<String> shortOptions = new ArrayList<String>();
 118  195
         List<String> longOptions = new ArrayList<String>();
 119  
 
 120  195
         for ( String each : unarranged ) {
 121  422
             if ( each.length() == 1 )
 122  126
                 shortOptions.add( each );
 123  
             else
 124  296
                 longOptions.add( each );
 125  422
         }
 126  
 
 127  195
         sort( shortOptions );
 128  195
         sort( longOptions );
 129  
 
 130  195
         options.addAll( shortOptions );
 131  195
         options.addAll( longOptions );
 132  195
     }
 133  
 
 134  
     @Override
 135  
     public boolean equals( Object that ) {
 136  2082
         if ( !( that instanceof AbstractOptionSpec<?> ) )
 137  516
             return false;
 138  
 
 139  1566
         AbstractOptionSpec<?> other = (AbstractOptionSpec<?>) that;
 140  1566
         return options.equals( other.options );
 141  
     }
 142  
 
 143  
     @Override
 144  
     public int hashCode() {
 145  1416
         return options.hashCode();
 146  
     }
 147  
 
 148  
     @Override
 149  
     public String toString() {
 150  1
         return options.toString();
 151  
     }
 152  
 }