Path: blob/master/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/util/EnumConverter.java
40951 views
/*1* Copyright (c) 2018, 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-2014 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.util;5657import java.text.MessageFormat;58import java.util.EnumSet;59import java.util.Iterator;60import java.util.ResourceBundle;6162import jdk.internal.joptsimple.ValueConversionException;63import jdk.internal.joptsimple.ValueConverter;6465/**66* Converts values to {@link java.lang.Enum}s.67*68* @author <a href="mailto:[email protected]">Christian Ohr</a>69*/70public abstract class EnumConverter<E extends Enum<E>> implements ValueConverter<E> {71private final Class<E> clazz;7273private String delimiters = "[,]";7475/**76* This constructor must be called by subclasses, providing the enum class as the parameter.77*78* @param clazz enum class79*/80protected EnumConverter( Class<E> clazz ) {81this.clazz = clazz;82}8384@Override85public E convert( String value ) {86for ( E each : valueType().getEnumConstants() ) {87if ( each.name().equalsIgnoreCase( value ) ) {88return each;89}90}9192throw new ValueConversionException( message( value ) );93}9495@Override96public Class<E> valueType() {97return clazz;98}99100/**101* Sets the delimiters for the message string. Must be a 3-letter string,102* where the first character is the prefix, the second character is the103* delimiter between the values, and the 3rd character is the suffix.104*105* @param delimiters delimiters for message string. Default is [,]106*/107public void setDelimiters( String delimiters ) {108this.delimiters = delimiters;109}110111@Override112public String valuePattern() {113EnumSet<E> values = EnumSet.allOf( valueType() );114115StringBuilder builder = new StringBuilder();116builder.append( delimiters.charAt(0) );117for ( Iterator<E> i = values.iterator(); i.hasNext(); ) {118builder.append( i.next().toString() );119if ( i.hasNext() )120builder.append( delimiters.charAt( 1 ) );121}122builder.append( delimiters.charAt( 2 ) );123124return builder.toString();125}126127private String message( String value ) {128ResourceBundle bundle = ResourceBundle.getBundle( "jdk.internal.joptsimple.ExceptionMessages" );129Object[] arguments = new Object[] { value, valuePattern() };130String template = bundle.getString( EnumConverter.class.getName() + ".message" );131return new MessageFormat( template ).format( arguments );132}133}134135136