Coverage Report - joptsimple.OptionSet
 
Classes in this File Line Coverage Branch Coverage Complexity
OptionSet
98%
40/41
88%
22/25
1.812
 
 1  
 /*
 2  
  Copyright 2004-2008 Paul R. Holser, Jr.  All rights reserved.
 3  
  Licensed under the Academic Free License version 3.0
 4  
  */
 5  
 
 6  
 package joptsimple;
 7  
 
 8  
 import java.util.ArrayList;
 9  
 import java.util.Collections;
 10  
 import java.util.HashMap;
 11  
 import java.util.Iterator;
 12  
 import java.util.List;
 13  
 import java.util.Map;
 14  
 
 15  
 /**
 16  
  * <p>Representation of a group of detected command line options, their arguments, and
 17  
  * non-option arguments.</p>
 18  
  *
 19  
  * @since 1.0
 20  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 21  
  * @version $Id: OptionSet.java,v 1.4 2008/04/15 02:19:07 pholser Exp $
 22  
  */
 23  
 public class OptionSet {
 24  344
     private final Map detectedOptions = new HashMap();
 25  344
     private final List nonOptionArguments = new ArrayList();
 26  
 
 27  344
     OptionSet() {
 28  
         // Empty on purpose.  Package-private because clients don't create these.
 29  344
     }
 30  
 
 31  
     /**
 32  
      * <p>Tells whether the given option was detected.</p>
 33  
      *
 34  
      * @since 1.0
 35  
      * @param option the option to search for
 36  
      * @return <code>true</code> if the option was detected
 37  
      * @deprecated Use {@link #has(String) has} instead.
 38  
      */
 39  
     public boolean wasDetected( String option ) {
 40  406
         return detectedOptions.containsKey( option );
 41  
     }
 42  
 
 43  
     /**
 44  
      * <p>Tells whether the given option was detected.</p>
 45  
      *
 46  
      * @since 2.4
 47  
      * @param option the option to search for
 48  
      * @return <code>true</code> if the option was detected
 49  
      */
 50  
     public boolean has( String option ) {
 51  406
         return wasDetected( option );
 52  
     }
 53  
 
 54  
     /**
 55  
      * <p>Tells whether there are any arguments associated with the given option.</p>
 56  
      *
 57  
      * @since 1.0
 58  
      * @param option the option to search for
 59  
      * @return <code>true</code> if the option was detected and at least one argument was
 60  
      * detected for the option
 61  
      */
 62  
     public boolean hasArgument( String option ) {
 63  6
         return !valuesOf( option ).isEmpty();
 64  
     }
 65  
 
 66  
     /**
 67  
      * <p>Gives the argument associated with the given option.</p>
 68  
      *
 69  
      * @since 1.0
 70  
      * @param option the option to search for
 71  
      * @return the argument of the given option as a {@link String}; <code>null</code> if
 72  
      * no argument is present, or that option was not detected
 73  
      * @throws OptionException if more than one argument was detected for the option
 74  
      * @throws ClassCastException if the argument was given a type other than
 75  
      * {@link String}
 76  
      * @see #valueOf(String)
 77  
      */
 78  
     public String argumentOf( String option ) {
 79  30
         return (String) valueOf( option );
 80  
     }
 81  
 
 82  
     /**
 83  
      * <p>Gives any arguments associated with the given option.</p>
 84  
      *
 85  
      * @since 1.0
 86  
      * @param option the option to search for
 87  
      * @return the arguments associated with the option, as a list of objects of the
 88  
      * type given to the arguments; an empty list if no such arguments are present, or if
 89  
      * the option was not detected
 90  
      * @see #valuesOf(String)
 91  
      */
 92  
     public List argumentsOf( String option ) {
 93  98
         return valuesOf( option );
 94  
     }
 95  
 
 96  
     /**
 97  
      * <p>Gives the argument associated with the given option.  If the argument was
 98  
      * given a type, it will take on that type; otherwise, use {@link #argumentOf(String)
 99  
      * argumentOf} to get the argument as a {@link String}.</p>
 100  
      *
 101  
      * @since 2.0
 102  
      * @param option the option to search for
 103  
      * @return the argument of the given option; <code>null</code> if no argument is
 104  
      * present, or that option was not detected
 105  
      * @throws OptionException if more than one argument was detected for the option
 106  
      */
 107  
     public Object valueOf( String option ) {
 108  98
         List values = valuesOf( option );
 109  
 
 110  98
         switch ( values.size() ) {
 111  
             case 0:
 112  38
                 return null;
 113  
             case 1:
 114  58
                 return values.get( 0 );
 115  
             default:
 116  2
                 throw new MultipleArgumentsForOptionException( option );
 117  
         }
 118  
     }
 119  
 
 120  
     /**
 121  
      * <p>Gives any arguments associated with the given option.</p>
 122  
      *
 123  
      * @since 2.0
 124  
      * @param option the option to search for
 125  
      * @return the arguments associated with the option, as a list of objects of the
 126  
      * type given to the arguments; an empty list if no such arguments are present, or if
 127  
      * the option was not detected
 128  
      */
 129  
     public List valuesOf( String option ) {
 130  376
         List values = (List) detectedOptions.get( option );
 131  376
         return values == null
 132  
             ? Collections.EMPTY_LIST
 133  
             : Collections.unmodifiableList( values );
 134  
     }
 135  
 
 136  
     /**
 137  
      * <p>Gives the detected non-option arguments.</p>
 138  
      *
 139  
      * @since 2.1
 140  
      * @return the detected non-option arguments as a list of {@link String}s.
 141  
      */
 142  
     public List nonOptionArguments() {
 143  414
         return Collections.unmodifiableList( nonOptionArguments );
 144  
     }
 145  
 
 146  
     /**
 147  
      * {@inheritDoc}
 148  
      */
 149  
     public boolean equals( Object that ) {
 150  676
         if ( this == that )
 151  168
             return true;
 152  
 
 153  508
         if ( that == null || !getClass().equals( that.getClass() ) )
 154  0
             return false;
 155  
 
 156  508
         OptionSet other = (OptionSet) that;
 157  508
         return detectedOptions.equals( other.detectedOptions )
 158  
             && nonOptionArguments.equals( other.nonOptionArguments() );
 159  
     }
 160  
 
 161  
     /**
 162  
      * {@inheritDoc}
 163  
      */
 164  
     public int hashCode() {
 165  180
         return detectedOptions.hashCode() ^ nonOptionArguments.hashCode();
 166  
     }
 167  
 
 168  
     void add( String option ) {
 169  188
         addWithArgument( option, null );
 170  188
     }
 171  
 
 172  
     void addWithArgument( String option, Object argument ) {
 173  566
         List optionArguments = (List) detectedOptions.get( option );
 174  
 
 175  566
         if ( optionArguments == null ) {
 176  442
             optionArguments = new ArrayList();
 177  442
             detectedOptions.put( option, optionArguments );
 178  
         }
 179  
 
 180  566
         if ( argument != null )
 181  378
             optionArguments.add( argument );
 182  566
     }
 183  
 
 184  
     void addNonOptionArgument( String argument ) {
 185  242
         nonOptionArguments.add( argument );
 186  242
     }
 187  
 
 188  
     void addAll( List options ) {
 189  150
         for ( Iterator iter = options.iterator(); iter.hasNext(); )
 190  178
             add( (String) iter.next() );
 191  150
     }
 192  
 
 193  
     void addAllWithArgument( List options, Object argument ) {
 194  256
         for ( Iterator iter = options.iterator(); iter.hasNext(); )
 195  304
             addWithArgument( (String) iter.next(), argument );
 196  256
     }
 197  
 }