| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
package joptsimple; |
| 7 | |
|
| 8 | |
import java.util.ArrayList; |
| 9 | |
import java.util.Collections; |
| 10 | |
import java.util.Iterator; |
| 11 | |
import java.util.List; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
abstract class OptionSpec { |
| 21 | 1128 | private final List options = new ArrayList(); |
| 22 | |
private final String description; |
| 23 | |
|
| 24 | |
protected OptionSpec( String option ) { |
| 25 | 194 | this( Collections.singletonList( option ), "" ); |
| 26 | 194 | } |
| 27 | |
|
| 28 | 1128 | protected OptionSpec( List options, String description ) { |
| 29 | 1128 | arrangeOptions( options ); |
| 30 | 1128 | this.description = description; |
| 31 | 1128 | } |
| 32 | |
|
| 33 | |
List options() { |
| 34 | 2134 | return options; |
| 35 | |
} |
| 36 | |
|
| 37 | |
String description() { |
| 38 | 82 | return description; |
| 39 | |
} |
| 40 | |
|
| 41 | |
abstract void handleOption( OptionParser parser, ArgumentList arguments, |
| 42 | |
OptionSet detectedOptions, String detectedArgument ); |
| 43 | |
|
| 44 | |
abstract boolean acceptsArguments(); |
| 45 | |
|
| 46 | |
abstract boolean requiresArgument(); |
| 47 | |
|
| 48 | |
abstract void accept( OptionSpecVisitor visitor ); |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public boolean equals( Object that ) { |
| 54 | 2430 | if ( this == that ) |
| 55 | 562 | return true; |
| 56 | |
|
| 57 | 1868 | if ( that == null || !getClass().equals( that.getClass() ) ) |
| 58 | 148 | return false; |
| 59 | |
|
| 60 | 1720 | OptionSpec other = (OptionSpec) that; |
| 61 | 1720 | return options.equals( other.options ); |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public int hashCode() { |
| 68 | 540 | return options.hashCode(); |
| 69 | |
} |
| 70 | |
|
| 71 | |
private void arrangeOptions( List unarranged ) { |
| 72 | 1128 | if ( unarranged.size() == 1 ) { |
| 73 | 994 | this.options.addAll( unarranged ); |
| 74 | 994 | return; |
| 75 | |
} |
| 76 | |
|
| 77 | 134 | List shortOptions = new ArrayList(); |
| 78 | 134 | List longOptions = new ArrayList(); |
| 79 | |
|
| 80 | 134 | for ( Iterator iter = unarranged.iterator(); iter.hasNext(); ) { |
| 81 | 316 | String next = (String) iter.next(); |
| 82 | 316 | if ( next.length() == 1 ) |
| 83 | 138 | shortOptions.add( next ); |
| 84 | |
else |
| 85 | 178 | longOptions.add( next ); |
| 86 | 316 | } |
| 87 | |
|
| 88 | 134 | Collections.sort( shortOptions ); |
| 89 | 134 | Collections.sort( longOptions ); |
| 90 | |
|
| 91 | 134 | this.options.addAll( shortOptions ); |
| 92 | 134 | this.options.addAll( longOptions ); |
| 93 | 134 | } |
| 94 | |
} |