Example usage for java.lang Enum valueOf

List of usage examples for java.lang Enum valueOf

Introduction

In this page you can find the example usage for java.lang Enum valueOf.

Prototype

public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) 

Source Link

Document

Returns the enum constant of the specified enum type with the specified name.

Usage

From source file:Main.java

public static void main(String[] args) {
    Class enumType = RetentionPolicy.class;
    String name = "SOURCE";

    Enum e = Enum.valueOf(enumType, name);
    System.out.println(e);//w  ww .  jav a 2s .c  o  m
}

From source file:com.artistech.tuio.dispatch.TuioPublish.java

public static void main(String[] args) throws InterruptedException {
    //read off the TUIO port from the command line
    int tuio_port = 3333;
    int zeromq_port = 5565;
    TuioSink.SerializeType serialize_method = TuioSink.SerializeType.PROTOBUF;

    Options options = new Options();
    options.addOption("t", "tuio-port", true, "TUIO Port to listen on. (Default = 3333)");
    options.addOption("z", "zeromq-port", true, "ZeroMQ Port to publish on. (Default = 5565)");
    options.addOption("s", "serialize-method", true,
            "Serialization Method (JSON, OBJECT, Default = PROTOBUF).");
    options.addOption("h", "help", false, "Show this message.");
    HelpFormatter formatter = new HelpFormatter();

    try {//from   www . j  a  v a2 s .  c  om
        CommandLineParser parser = new org.apache.commons.cli.BasicParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("help")) {
            formatter.printHelp("tuio-zeromq-publish", options);
            return;
        } else {
            if (cmd.hasOption("t") || cmd.hasOption("tuio-port")) {
                tuio_port = Integer.parseInt(cmd.getOptionValue("t"));
            }
            if (cmd.hasOption("z") || cmd.hasOption("zeromq-port")) {
                zeromq_port = Integer.parseInt(cmd.getOptionValue("z"));
            }
            if (cmd.hasOption("s") || cmd.hasOption("serialize-method")) {
                serialize_method = (TuioSink.SerializeType) Enum.valueOf(TuioSink.SerializeType.class,
                        cmd.getOptionValue("s"));
            }
        }
    } catch (ParseException | IllegalArgumentException ex) {
        System.err.println("Error Processing Command Options:");
        formatter.printHelp("tuio-zeromq-publish", options);
        return;
    }

    //start up the zmq publisher
    ZMQ.Context context = ZMQ.context(1);
    // We send updates via this socket
    try (ZMQ.Socket publisher = context.socket(ZMQ.PUB)) {
        // We send updates via this socket
        publisher.bind("tcp://*:" + Integer.toString(zeromq_port));

        //create a new TUIO sink connected at the specified port
        TuioSink sink = new TuioSink();
        sink.setSerializationType(serialize_method);
        TuioClient client = new TuioClient(tuio_port);

        System.out.println(
                MessageFormat.format("Listening to TUIO message at port: {0}", Integer.toString(tuio_port)));
        System.out.println(
                MessageFormat.format("Publishing to ZeroMQ at port: {0}", Integer.toString(zeromq_port)));
        System.out.println(MessageFormat.format("Serializing as: {0}", serialize_method));
        client.addTuioListener(sink);
        client.connect();

        //while not halted (infinite loop...)
        //read any available messages and publish
        while (!sink.mailbox.isHalted()) {
            ImmutablePair<String, byte[]> msg = sink.mailbox.getMessage();
            publisher.sendMore(msg.left + "." + serialize_method.toString());
            publisher.send(msg.right, 0);
        }

        //cleanup
    }
    context.term();
}

From source file:edu.usc.pgroup.floe.client.commands.Scale.java

/**
 * Entry point for Scale command./*  w w  w  .ja v a  2 s  . c  om*/
 * @param args command line arguments sent by the floe.py script.
 */
public static void main(final String[] args) {

    Options options = new Options();

    Option dirOption = OptionBuilder.withArgName("direction").hasArg().isRequired()
            .withDescription("Scale Direction.").create("dir");

    Option appOption = OptionBuilder.withArgName("name").hasArg().isRequired()
            .withDescription("Application Name").create("app");

    Option pelletNameOption = OptionBuilder.withArgName("name").hasArg().isRequired()
            .withDescription("Pellet Name").create("pellet");

    Option cntOption = OptionBuilder.withArgName("num").hasArg().withType(new String())
            .withDescription("Number of instances to scale up/down").create("cnt");

    options.addOption(dirOption);
    options.addOption(appOption);
    options.addOption(pelletNameOption);
    options.addOption(cntOption);

    CommandLineParser parser = new BasicParser();
    CommandLine line;

    try {
        line = parser.parse(options, args);

    } catch (ParseException e) {
        LOGGER.error("Invalid command: " + e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("scale options", options);
        return;
    }

    String dir = line.getOptionValue("dir");
    String app = line.getOptionValue("app");
    String pellet = line.getOptionValue("pellet");
    String cnt = line.getOptionValue("cnt");

    LOGGER.info("direction: {}", dir);
    LOGGER.info("Application: {}", app);
    LOGGER.info("Pellet: {}", pellet);
    LOGGER.info("count: {}", cnt);

    ScaleDirection direction = Enum.valueOf(ScaleDirection.class, dir);
    int count = Integer.parseInt(cnt);
    try {
        FloeClient.getInstance().getClient().scale(direction, app, pellet, count);
    } catch (TException e) {
        LOGGER.error("Error while connecting to the coordinator: {}", e);
    }
}

From source file:Main.java

public static <X extends Enum<X>> X enumFind(String name, X fallback) {
    try {//from w w w. ja v  a2 s  .  c  om
        return Enum.valueOf(fallback.getDeclaringClass(), name);
    } catch (Exception e) {
        return fallback;
    }

}

From source file:Main.java

public static <E extends Enum<E>> boolean contains(Class<E> _enumClass, String value) {
    try {/* ww  w.j  ava  2s  . c  o  m*/
        return EnumSet.allOf(_enumClass).contains(Enum.valueOf(_enumClass, value));
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

protected static void setEnumField(Object obj, String name, String value)
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    final Field f = obj.getClass().getField(name);
    f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value));
}

From source file:Main.java

/**
 * Tries to find Enum of specified name in specified class. If name's null, returns null.
 *
 * @param <E>// w  ww .  j  a  va  2  s .  c  om
 * @param clazz
 * @param name
 * @return Parsed object or null
 */
public static <E extends Enum<E>> E findEnum(Class<E> clazz, String name) {
    if (name == null) {
        return null;
    }
    try {
        return Enum.valueOf(clazz, name.toUpperCase());
    } catch (IllegalArgumentException iae) {
        return null;
    }
}

From source file:Main.java

/**
 * Parses a String val through an Enum class and returns a match or returns defaultValue
 * //from w ww.j a  v a  2s .c o m
 * @param value The value to attempt to match
 * @param enuma Enum class to match value in
 * @param defaultValue Value to return if a match isn't found
 * @return A parsed object
 */
public static <T extends Enum<T>> T stringToEnum(String value, Class<T> enuma, T defaultValue) {
    if (value == null || enuma == null)
        return defaultValue;
    T valueFound = Enum.valueOf(enuma, value.replace(" ", "_").toUpperCase());
    if (valueFound != null)
        return valueFound;
    return defaultValue;
}

From source file:Main.java

public static Enum<?> newEnum(Class<?> enumClass, String enumStr) {
    @SuppressWarnings({ "rawtypes", "unchecked" })
    Enum en = Enum.valueOf(enumClass.asSubclass(Enum.class), enumStr);
    return en;/*from  www  .  j a va 2s. c  o m*/
}

From source file:org.dspace.authority.util.EnumUtils.java

public static <E extends Enum<E>> E lookup(Class<E> enumClass, String enumName) {
    try {/*from   w  ww. j  av  a  2 s.  c om*/
        return Enum.valueOf(enumClass, getEnumName(enumName));
    } catch (Exception ex) {
        log.warn("Did not find an " + enumClass.getSimpleName() + " for value '" + enumName + "'");
        return null;
    }
}