src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/OptionException.java
changeset 50428 8c88df2e8a78
parent 47216 71c04702a3d5
equal deleted inserted replaced
50427:b06f330492cd 50428:8c88df2e8a78
    29  * However, the following notice accompanied the original version of this
    29  * However, the following notice accompanied the original version of this
    30  * file:
    30  * file:
    31  *
    31  *
    32  * The MIT License
    32  * The MIT License
    33  *
    33  *
    34  * Copyright (c) 2004-2014 Paul R. Holser, Jr.
    34  * Copyright (c) 2004-2015 Paul R. Holser, Jr.
    35  *
    35  *
    36  * Permission is hereby granted, free of charge, to any person obtaining
    36  * Permission is hereby granted, free of charge, to any person obtaining
    37  * a copy of this software and associated documentation files (the
    37  * a copy of this software and associated documentation files (the
    38  * "Software"), to deal in the Software without restriction, including
    38  * "Software"), to deal in the Software without restriction, including
    39  * without limitation the rights to use, copy, modify, merge, publish,
    39  * without limitation the rights to use, copy, modify, merge, publish,
    56 package jdk.internal.joptsimple;
    56 package jdk.internal.joptsimple;
    57 
    57 
    58 import java.util.ArrayList;
    58 import java.util.ArrayList;
    59 import java.util.Collection;
    59 import java.util.Collection;
    60 import java.util.Iterator;
    60 import java.util.Iterator;
       
    61 import java.util.LinkedHashSet;
    61 import java.util.List;
    62 import java.util.List;
       
    63 import java.util.Locale;
       
    64 import java.util.Set;
       
    65 
       
    66 import jdk.internal.joptsimple.internal.Strings;
    62 
    67 
    63 import static java.util.Collections.*;
    68 import static java.util.Collections.*;
    64 
    69 import static jdk.internal.joptsimple.internal.Messages.*;
    65 import static jdk.internal.joptsimple.internal.Strings.*;
       
    66 
    70 
    67 /**
    71 /**
    68  * Thrown when a problem occurs during option parsing.
    72  * Thrown when a problem occurs during option parsing.
    69  *
    73  *
    70  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
    74  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
    71  */
    75  */
    72 public abstract class OptionException extends RuntimeException {
    76 public abstract class OptionException extends RuntimeException {
    73     private static final long serialVersionUID = -1L;
    77     private static final long serialVersionUID = -1L;
    74 
    78 
    75     private final List<String> options = new ArrayList<String>();
    79     private final List<String> options = new ArrayList<>();
    76 
    80 
    77     protected OptionException( Collection<String> options ) {
    81     protected OptionException( List<String> options ) {
    78         this.options.addAll( options );
    82         this.options.addAll( options );
    79     }
    83     }
    80 
    84 
    81     protected OptionException( Collection<String> options, Throwable cause ) {
    85     protected OptionException( Collection<? extends OptionSpec<?>> options ) {
       
    86         this.options.addAll( specsToStrings( options ) );
       
    87     }
       
    88 
       
    89     protected OptionException( Collection<? extends OptionSpec<?>> options, Throwable cause ) {
    82         super( cause );
    90         super( cause );
       
    91         this.options.addAll( specsToStrings( options ) );
       
    92     }
    83 
    93 
    84         this.options.addAll( options );
    94     private List<String> specsToStrings( Collection<? extends OptionSpec<?>> options ) {
       
    95         List<String> strings = new ArrayList<>();
       
    96         for ( OptionSpec<?> each : options )
       
    97             strings.add( specToString( each ) );
       
    98         return strings;
       
    99     }
       
   100 
       
   101     private String specToString( OptionSpec<?> option ) {
       
   102         return Strings.join( new ArrayList<>( option.options() ), "/" );
    85     }
   103     }
    86 
   104 
    87     /**
   105     /**
    88      * Gives the option being considered when the exception was created.
   106      * Gives the option being considered when the exception was created.
    89      *
   107      *
    90      * @return the option being considered when the exception was created
   108      * @return the option being considered when the exception was created
    91      */
   109      */
    92     public Collection<String> options() {
   110     public List<String> options() {
    93         return unmodifiableCollection( options );
   111         return unmodifiableList( options );
    94     }
   112     }
    95 
   113 
    96     protected final String singleOptionMessage() {
   114     protected final String singleOptionString() {
    97         return singleOptionMessage( options.get( 0 ) );
   115         return singleOptionString( options.get( 0 ) );
    98     }
   116     }
    99 
   117 
   100     protected final String singleOptionMessage( String option ) {
   118     protected final String singleOptionString( String option ) {
   101         return SINGLE_QUOTE + option + SINGLE_QUOTE;
   119         return option;
   102     }
   120     }
   103 
   121 
   104     protected final String multipleOptionMessage() {
   122     protected final String multipleOptionString() {
   105         StringBuilder buffer = new StringBuilder( "[" );
   123         StringBuilder buffer = new StringBuilder( "[" );
   106 
   124 
   107         for ( Iterator<String> iter = options.iterator(); iter.hasNext(); ) {
   125         Set<String> asSet = new LinkedHashSet<String>( options );
   108             buffer.append( singleOptionMessage( iter.next() ) );
   126         for ( Iterator<String> iter = asSet.iterator(); iter.hasNext(); ) {
       
   127             buffer.append( singleOptionString(iter.next()) );
   109             if ( iter.hasNext() )
   128             if ( iter.hasNext() )
   110                 buffer.append( ", " );
   129                 buffer.append( ", " );
   111         }
   130         }
   112 
   131 
   113         buffer.append( ']' );
   132         buffer.append( ']' );
   116     }
   135     }
   117 
   136 
   118     static OptionException unrecognizedOption( String option ) {
   137     static OptionException unrecognizedOption( String option ) {
   119         return new UnrecognizedOptionException( option );
   138         return new UnrecognizedOptionException( option );
   120     }
   139     }
       
   140 
       
   141     @Override
       
   142     public final String getMessage() {
       
   143         return localizedMessage( Locale.getDefault() );
       
   144     }
       
   145 
       
   146     final String localizedMessage( Locale locale ) {
       
   147         return formattedMessage( locale );
       
   148     }
       
   149 
       
   150     private String formattedMessage( Locale locale ) {
       
   151         return message( locale, "jdk.internal.joptsimple.ExceptionMessages", getClass(), "message", messageArguments() );
       
   152     }
       
   153 
       
   154     abstract Object[] messageArguments();
   121 }
   155 }