All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.it.unimi.dsi.lang.EnumStringParser Maven / Gradle / Ivy

package it.unimi.dsi.lang;

/*
 * DSI utilities
 *
 * Copyright (C) 2016-2017 Sebastiano Vigna
 *
 *  This library is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as published by the Free
 *  Software Foundation; either version 3 of the License, or (at your option)
 *  any later version.
 *
 *  This library is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, see .
 *
 */

import com.martiansoftware.jsap.ParseException;
import com.martiansoftware.jsap.StringParser;

/**
 * A {@link com.martiansoftware.jsap.StringParser StringParser} that makes the user choose among
 * items of a Java {@code enum}.
 *
 * 

Optionally, parsed strings can be normalized to upper case. * Thus, if the enum elements are defined in uppercase, the parser will be in practice * case-independent. * *

A typical usage example for an {@code ExampleEnum} with an item {@code A} that is going to be the default: *

 * new FlaggedOption("example",
 *     EnumStringParser.getParser(ExampleEnum.class, true),
 *     ExampleEnum.A.name(), JSAP.NOT_REQUIRED, 'e', "example",
 *     Arrays.toString(ExampleEnum.values()))
 * 
*/ public class EnumStringParser> extends StringParser { private final Class enumClass; private final boolean toUpper; /** Returns the enum item obtained by passing the argument to {@link Enum#valueOf(Class, String)}. * * @param s an enum item name. * @return the enum item returned by {@link Enum#valueOf(Class, String)} (possibly * after upper casing {@code s}). */ @Override @SuppressWarnings({ "unchecked" }) public E parse(String s) throws ParseException { try { return (E) enumClass.getMethod("valueOf", String.class).invoke(null, toUpper ? s.toUpperCase() : s); } catch (Exception e) { throw (new ParseException("Unknown value '" + s + "'.", e)); } } private EnumStringParser(Class enumClass, final boolean toUpper) { this.enumClass = enumClass; this.toUpper = toUpper; } /** * Returns an enum parser. * * @param enumClass an {@code enum} class whose values * @param toUpper tells the parser to upper case the strings to be parsed. */ public static > EnumStringParser getParser(final Class enumClass, final boolean toUpper) throws IllegalArgumentException { return new EnumStringParser<>(enumClass, toUpper); } /** * Returns an enum parser that does not normalize to upper case. * * @param enumClass an {@code enum} class whose values */ public static > EnumStringParser getParser(final Class enumClass) throws IllegalArgumentException { return getParser(enumClass, false); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy