Path: blob/master/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/AbstractOptionSpec.java
40948 views
/*1* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* The MIT License32*33* Copyright (c) 2004-2015 Paul R. Holser, Jr.34*35* Permission is hereby granted, free of charge, to any person obtaining36* a copy of this software and associated documentation files (the37* "Software"), to deal in the Software without restriction, including38* without limitation the rights to use, copy, modify, merge, publish,39* distribute, sublicense, and/or sell copies of the Software, and to40* permit persons to whom the Software is furnished to do so, subject to41* the following conditions:42*43* The above copyright notice and this permission notice shall be44* included in all copies or substantial portions of the Software.45*46* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,47* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF48* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND49* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE50* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION51* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION52* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.53*/5455package jdk.internal.joptsimple;5657import java.util.ArrayList;58import java.util.List;5960import static java.util.Collections.*;6162import jdk.internal.joptsimple.internal.Reflection;63import jdk.internal.joptsimple.internal.ReflectionException;6465import static jdk.internal.joptsimple.internal.Strings.*;6667/**68* @param <V> represents the type of the arguments this option accepts69* @author <a href="mailto:[email protected]">Paul Holser</a>70*/71public abstract class AbstractOptionSpec<V> implements OptionSpec<V>, OptionDescriptor {72private final List<String> options = new ArrayList<>();73private final String description;74private boolean forHelp;7576AbstractOptionSpec( String option ) {77this( singletonList( option ), EMPTY );78}7980AbstractOptionSpec( List<String> options, String description ) {81arrangeOptions( options );8283this.description = description;84}8586public final List<String> options() {87return unmodifiableList( options );88}8990public final List<V> values( OptionSet detectedOptions ) {91return detectedOptions.valuesOf( this );92}9394public final V value( OptionSet detectedOptions ) {95return detectedOptions.valueOf( this );96}9798public String description() {99return description;100}101102public final AbstractOptionSpec<V> forHelp() {103forHelp = true;104return this;105}106107public final boolean isForHelp() {108return forHelp;109}110111public boolean representsNonOptions() {112return false;113}114115protected abstract V convert( String argument );116117protected V convertWith( ValueConverter<V> converter, String argument ) {118try {119return Reflection.convertWith( converter, argument );120} catch ( ReflectionException | ValueConversionException ex ) {121throw new OptionArgumentConversionException( this, argument, ex );122}123}124125protected String argumentTypeIndicatorFrom( ValueConverter<V> converter ) {126if ( converter == null )127return null;128129String pattern = converter.valuePattern();130return pattern == null ? converter.valueType().getName() : pattern;131}132133abstract void handleOption( OptionParser parser, ArgumentList arguments, OptionSet detectedOptions,134String detectedArgument );135136private void arrangeOptions( List<String> unarranged ) {137if ( unarranged.size() == 1 ) {138options.addAll( unarranged );139return;140}141142List<String> shortOptions = new ArrayList<>();143List<String> longOptions = new ArrayList<>();144145for ( String each : unarranged ) {146if ( each.length() == 1 )147shortOptions.add( each );148else149longOptions.add( each );150}151152sort( shortOptions );153sort( longOptions );154155options.addAll( shortOptions );156options.addAll( longOptions );157}158159@Override160public boolean equals( Object that ) {161if ( !( that instanceof AbstractOptionSpec<?> ) )162return false;163164AbstractOptionSpec<?> other = (AbstractOptionSpec<?>) that;165return options.equals( other.options );166}167168@Override169public int hashCode() {170return options.hashCode();171}172173@Override174public String toString() {175return options.toString();176}177}178179180