jdk/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/AbstractOptionSpec.java
changeset 35377 462f93ab37f2
equal deleted inserted replaced
35361:4a652e4ca952 35377:462f93ab37f2
       
     1 /*
       
     2  * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * This file is available under and governed by the GNU General Public
       
    28  * License version 2 only, as published by the Free Software Foundation.
       
    29  * However, the following notice accompanied the original version of this
       
    30  * file:
       
    31  *
       
    32  * The MIT License
       
    33  *
       
    34  * Copyright (c) 2004-2014 Paul R. Holser, Jr.
       
    35  *
       
    36  * Permission is hereby granted, free of charge, to any person obtaining
       
    37  * a copy of this software and associated documentation files (the
       
    38  * "Software"), to deal in the Software without restriction, including
       
    39  * without limitation the rights to use, copy, modify, merge, publish,
       
    40  * distribute, sublicense, and/or sell copies of the Software, and to
       
    41  * permit persons to whom the Software is furnished to do so, subject to
       
    42  * the following conditions:
       
    43  *
       
    44  * The above copyright notice and this permission notice shall be
       
    45  * included in all copies or substantial portions of the Software.
       
    46  *
       
    47  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    48  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    49  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
       
    50  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
       
    51  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
       
    52  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
       
    53  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    54  */
       
    55 
       
    56 package jdk.internal.joptsimple;
       
    57 
       
    58 import java.util.ArrayList;
       
    59 import java.util.Collection;
       
    60 import java.util.List;
       
    61 
       
    62 import static java.util.Collections.*;
       
    63 
       
    64 import jdk.internal.joptsimple.internal.Reflection;
       
    65 import jdk.internal.joptsimple.internal.ReflectionException;
       
    66 
       
    67 import static jdk.internal.joptsimple.internal.Strings.*;
       
    68 
       
    69 /**
       
    70  * @param <V> represents the type of the arguments this option accepts
       
    71  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
       
    72  */
       
    73 abstract class AbstractOptionSpec<V> implements OptionSpec<V>, OptionDescriptor {
       
    74     private final List<String> options = new ArrayList<String>();
       
    75     private final String description;
       
    76     private boolean forHelp;
       
    77 
       
    78     protected AbstractOptionSpec( String option ) {
       
    79         this( singletonList( option ), EMPTY );
       
    80     }
       
    81 
       
    82     protected AbstractOptionSpec( Collection<String> options, String description ) {
       
    83         arrangeOptions( options );
       
    84 
       
    85         this.description = description;
       
    86     }
       
    87 
       
    88     public final Collection<String> options() {
       
    89         return unmodifiableList( options );
       
    90     }
       
    91 
       
    92     public final List<V> values( OptionSet detectedOptions ) {
       
    93         return detectedOptions.valuesOf( this );
       
    94     }
       
    95 
       
    96     public final V value( OptionSet detectedOptions ) {
       
    97         return detectedOptions.valueOf( this );
       
    98     }
       
    99 
       
   100     public String description() {
       
   101         return description;
       
   102     }
       
   103 
       
   104     public final AbstractOptionSpec<V> forHelp() {
       
   105         forHelp = true;
       
   106         return this;
       
   107     }
       
   108 
       
   109     public final boolean isForHelp() {
       
   110         return forHelp;
       
   111     }
       
   112 
       
   113     public boolean representsNonOptions() {
       
   114         return false;
       
   115     }
       
   116 
       
   117     protected abstract V convert( String argument );
       
   118 
       
   119     protected V convertWith( ValueConverter<V> converter, String argument ) {
       
   120         try {
       
   121             return Reflection.convertWith( converter, argument );
       
   122         }
       
   123         catch ( ReflectionException ex ) {
       
   124             throw new OptionArgumentConversionException( options(), argument, ex );
       
   125         }
       
   126         catch ( ValueConversionException ex ) {
       
   127             throw new OptionArgumentConversionException( options(), argument, ex );
       
   128         }
       
   129     }
       
   130 
       
   131     protected String argumentTypeIndicatorFrom( ValueConverter<V> converter ) {
       
   132         if ( converter == null )
       
   133             return null;
       
   134 
       
   135         String pattern = converter.valuePattern();
       
   136         return pattern == null ? converter.valueType().getName() : pattern;
       
   137     }
       
   138 
       
   139     abstract void handleOption( OptionParser parser, ArgumentList arguments, OptionSet detectedOptions,
       
   140         String detectedArgument );
       
   141 
       
   142     private void arrangeOptions( Collection<String> unarranged ) {
       
   143         if ( unarranged.size() == 1 ) {
       
   144             options.addAll( unarranged );
       
   145             return;
       
   146         }
       
   147 
       
   148         List<String> shortOptions = new ArrayList<String>();
       
   149         List<String> longOptions = new ArrayList<String>();
       
   150 
       
   151         for ( String each : unarranged ) {
       
   152             if ( each.length() == 1 )
       
   153                 shortOptions.add( each );
       
   154             else
       
   155                 longOptions.add( each );
       
   156         }
       
   157 
       
   158         sort( shortOptions );
       
   159         sort( longOptions );
       
   160 
       
   161         options.addAll( shortOptions );
       
   162         options.addAll( longOptions );
       
   163     }
       
   164 
       
   165     @Override
       
   166     public boolean equals( Object that ) {
       
   167         if ( !( that instanceof AbstractOptionSpec<?> ) )
       
   168             return false;
       
   169 
       
   170         AbstractOptionSpec<?> other = (AbstractOptionSpec<?>) that;
       
   171         return options.equals( other.options );
       
   172     }
       
   173 
       
   174     @Override
       
   175     public int hashCode() {
       
   176         return options.hashCode();
       
   177     }
       
   178 
       
   179     @Override
       
   180     public String toString() {
       
   181         return options.toString();
       
   182     }
       
   183 }