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:org.apache.tajo.catalog.store.HCatalogUtil.java

public static TajoDataTypes.Type getTajoFieldType(String fieldType) {
    Preconditions.checkNotNull(fieldType);

    String typeStr = null;/*w  w  w  . j a  v  a  2 s  .co m*/

    if (fieldType.equalsIgnoreCase(serdeConstants.INT_TYPE_NAME))
        typeStr = "INT4";
    else if (fieldType.equalsIgnoreCase(serdeConstants.TINYINT_TYPE_NAME))
        typeStr = "INT1";
    else if (fieldType.equalsIgnoreCase(serdeConstants.SMALLINT_TYPE_NAME))
        typeStr = "INT2";
    else if (fieldType.equalsIgnoreCase(serdeConstants.BIGINT_TYPE_NAME))
        typeStr = "INT8";
    else if (fieldType.equalsIgnoreCase(serdeConstants.BOOLEAN_TYPE_NAME))
        typeStr = "BOOLEAN";
    else if (fieldType.equalsIgnoreCase(serdeConstants.FLOAT_TYPE_NAME))
        typeStr = "FLOAT4";
    else if (fieldType.equalsIgnoreCase(serdeConstants.DOUBLE_TYPE_NAME))
        typeStr = "FLOAT8";
    else if (fieldType.equalsIgnoreCase(serdeConstants.STRING_TYPE_NAME))
        typeStr = "TEXT";
    else if (fieldType.equalsIgnoreCase(serdeConstants.BINARY_TYPE_NAME))
        typeStr = "BLOB";

    try {
        return Enum.valueOf(TajoDataTypes.Type.class, typeStr);
    } catch (IllegalArgumentException iae) {
        throw new CatalogException("Cannot find a matched type against from '" + typeStr + "'");
    }
}

From source file:nebula.plugin.metrics.dispatcher.AbstractMetricsDispatcher.java

/**
 * Register Jackson module that maps enums as lowercase. Per http://stackoverflow.com/a/24173645.
 *//*from   w  w w .  j  a  v a2  s  .  c  om*/
@SuppressWarnings("rawtypes")
private static void registerEnumModule(ObjectMapper mapper) {
    SimpleModule module = new SimpleModule();
    module.setDeserializerModifier(new BeanDeserializerModifier() {
        @Override
        public JsonDeserializer<Enum> modifyEnumDeserializer(DeserializationConfig config, final JavaType type,
                BeanDescription beanDesc, final JsonDeserializer<?> deserializer) {
            return new JsonDeserializer<Enum>() {
                @Override
                public Enum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
                    @SuppressWarnings("unchecked")
                    Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass();
                    return Enum.valueOf(rawClass, jp.getValueAsString().toUpperCase());
                }
            };
        }
    });
    module.addSerializer(Enum.class, new StdSerializer<Enum>(Enum.class) {
        @Override
        public void serialize(Enum value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeString(value.name().toLowerCase());
        }
    });
    mapper.registerModule(module);
}

From source file:org.meveo.commons.utils.ReflectionUtils.java

public static <T extends Enum<T>> T getEnumFromString(Class<T> c, String string) {
    if (c != null && string != null) {
        try {//  ww  w .j  a  v  a 2s  . co m
            return Enum.valueOf(c, string.trim().toUpperCase());
        } catch (IllegalArgumentException ex) {
        }
    }
    return null;
}

From source file:org.apache.cassandra.config.ColumnDefinition.java

public org.apache.cassandra.config.avro.ColumnDef deflate() {
    org.apache.cassandra.config.avro.ColumnDef cd = new org.apache.cassandra.config.avro.ColumnDef();
    cd.name = ByteBuffer.wrap(name);
    cd.validation_class = new Utf8(validator.getClass().getName());
    cd.index_type = index_type == null ? null
            : Enum.valueOf(org.apache.cassandra.config.avro.IndexType.class, index_type.name());
    cd.index_name = index_name == null ? null : new Utf8(index_name);
    return cd;/* ww  w .jav  a 2  s .  com*/
}

From source file:be.fedict.eid.pkira.portal.util.TypeMapper.java

public ContractTypeWS map(ContractType contractType) {
    if (contractType == null)
        return null;
    return Enum.valueOf(ContractTypeWS.class, contractType.name());
}

From source file:edu.utexas.cs.tactex.servercustomers.factoredcustomer.ProbabilityDistribution.java

ProbabilityDistribution(FactoredCustomerService service, Element xml) {
    if (null == randomSeedRepo)
        randomSeedRepo = (RandomSeedRepo) SpringApplicationContext.getBean("randomSeedRepo");

    type = Enum.valueOf(DistType.class, xml.getAttribute("distribution"));
    switch (type) {
    case POINTMASS:
    case DEGENERATE:
        param1 = Double.parseDouble(xml.getAttribute("value"));
        sampler = new DegenerateSampler(param1);
        break;/*from  ww w. j ava 2 s  .c  om*/
    case UNIFORM:
        param1 = Double.parseDouble(xml.getAttribute("low"));
        param2 = Double.parseDouble(xml.getAttribute("high"));
        sampler = new UniformSampler(param1, param2);
        break;
    case INTERVAL:
        param1 = Double.parseDouble(xml.getAttribute("mean"));
        param2 = Double.parseDouble(xml.getAttribute("stdDev"));
        param3 = Double.parseDouble(xml.getAttribute("low"));
        param4 = Double.parseDouble(xml.getAttribute("high"));
        sampler = new IntervalSampler(param1, param2, param3, param4);
        break;
    case NORMAL:
    case GAUSSIAN:
        param1 = Double.parseDouble(xml.getAttribute("mean"));
        param2 = Double.parseDouble(xml.getAttribute("stdDev"));
        sampler = new ContinuousSampler(new NormalDistributionImpl(param1, param2));
        break;
    case STDNORMAL:
        param1 = 0;
        param2 = 1;
        sampler = new ContinuousSampler(new NormalDistributionImpl(param1, param2));
        break;
    case LOGNORMAL:
        param1 = Double.parseDouble(xml.getAttribute("expMean"));
        param2 = Double.parseDouble(xml.getAttribute("expStdDev"));
        sampler = new LogNormalSampler(param1, param2);
        break;
    case CAUCHY:
        param1 = Double.parseDouble(xml.getAttribute("median"));
        param2 = Double.parseDouble(xml.getAttribute("scale"));
        sampler = new ContinuousSampler(new CauchyDistributionImpl(param1, param2));
        break;
    case BETA:
        param1 = Double.parseDouble(xml.getAttribute("alpha"));
        param2 = Double.parseDouble(xml.getAttribute("beta"));
        sampler = new ContinuousSampler(new BetaDistributionImpl(param1, param2));
        break;
    case BINOMIAL:
        param1 = Double.parseDouble(xml.getAttribute("trials"));
        param2 = Double.parseDouble(xml.getAttribute("success"));
        sampler = new DiscreteSampler(new BinomialDistributionImpl((int) param1, param2));
        break;
    case POISSON:
        param1 = Double.parseDouble(xml.getAttribute("lambda"));
        sampler = new DiscreteSampler(new PoissonDistributionImpl(param1));
        break;
    case CHISQUARED:
        param1 = Double.parseDouble(xml.getAttribute("dof"));
        sampler = new ContinuousSampler(new ChiSquaredDistributionImpl(param1));
        break;
    case EXPONENTIAL:
        param1 = Double.parseDouble(xml.getAttribute("mean"));
        sampler = new ContinuousSampler(new ExponentialDistributionImpl(param1));
        break;
    case GAMMA:
        param1 = Double.parseDouble(xml.getAttribute("alpha"));
        param2 = Double.parseDouble(xml.getAttribute("beta"));
        sampler = new ContinuousSampler(new GammaDistributionImpl(param1, param2));
        break;
    case WEIBULL:
        param1 = Double.parseDouble(xml.getAttribute("alpha"));
        param2 = Double.parseDouble(xml.getAttribute("beta"));
        sampler = new ContinuousSampler(new WeibullDistributionImpl(param1, param2));
        break;
    case STUDENT:
        param1 = Double.parseDouble(xml.getAttribute("dof"));
        sampler = new ContinuousSampler(new TDistributionImpl(param1));
        break;
    case SNEDECOR:
        param1 = Double.parseDouble(xml.getAttribute("d1"));
        param2 = Double.parseDouble(xml.getAttribute("d2"));
        sampler = new ContinuousSampler(new FDistributionImpl(param1, param2));
        break;
    default:
        throw new Error("Invalid probability distribution type!");
    }
    sampler.reseedRandomGenerator(service.getRandomSeedRepo()
            .getRandomSeed("factoredcustomer.ProbabilityDistribution", SeedIdGenerator.getId(), "Sampler")
            .getValue());
}

From source file:org.itest.impl.ITestValueConverterImpl.java

@Override
public <T> T convert(Class<T> clazz, String value) {
    Object res;//  w ww.  j a  v a 2 s  . c o  m
    if (null == value) {
        res = null;
    } else if (String.class == clazz || Object.class == clazz) {
        res = StringEscapeUtils.unescapeJava(value);
    } else if (Integer.class == clazz || int.class == clazz) {
        res = Integer.valueOf(value);
    } else if (Long.class == clazz || long.class == clazz) {
        res = Long.valueOf(value);
    } else if (Double.class == clazz || double.class == clazz) {
        res = Double.valueOf(value);
    } else if (Float.class == clazz || float.class == clazz) {
        res = Float.valueOf(value);
    } else if (Boolean.class == clazz || boolean.class == clazz) {
        res = Boolean.valueOf(value);
    } else if (Character.class == clazz || char.class == clazz) {
        value = StringEscapeUtils.unescapeJava(value);
        if (value.length() > 1) {
            throw new ITestInitializationException(
                    "Character expected, found (" + value + ") " + value.length() + " characters.", null);
        }
        res = value.charAt(0);
    } else if (Date.class == clazz || Time.class == clazz || Timestamp.class == clazz
            || java.sql.Date.class == clazz) {
        Long milis = Long.valueOf(value);
        try {
            res = clazz.getConstructor(long.class).newInstance(milis);
        } catch (Exception e) {
            throw new ITestException("", e);
        }
    } else if (clazz.isEnum()) {
        res = Enum.valueOf((Class<Enum>) clazz, value);
    } else if (Class.class == clazz) {
        try {
            res = ClassUtils.getClass(getClass().getClassLoader(), value);
        } catch (ClassNotFoundException e) {
            throw new ITestInitializationException(value, e);
        }
    } else {
        throw new ITestInitializationException(clazz.getName() + "(" + value + ")", null);
    }
    return (T) res;
}

From source file:technology.tikal.customers.model.name.NombrePersonaMxIndexed.java

@Override
public String getIndex(String idIndex) {
    NamePriorityFilterValues type = Enum.valueOf(NamePriorityFilterValues.class, idIndex);
    if (type == NamePriorityFilterValues.PersonName) {
        return this.normalizedPersonName;
    }//  w w  w.j  a va  2  s  .c  o  m
    if (type == NamePriorityFilterValues.PersonSurname) {
        return this.normalizedPersonSurname;
    }
    if (type == NamePriorityFilterValues.Name) {
        return this.normalizedPersonName;
    }
    throw new IllegalArgumentException("tipo no soportado:" + type.toString());
}

From source file:org.ovirt.engine.api.common.util.EnumValidator.java

public static <E extends Enum<E>> String validateEnum(String reason, String detail, Class<E> clz,
        Set<String> externalValues, String name, boolean toUppercase) {
    for (String externalValue : externalValues) {
        if (externalValue.equalsIgnoreCase(name)) {
            return name;
        }//  www . j av a2s.c om
    }

    try {
        return Enum.valueOf(clz, toUppercase ? name.toUpperCase() : name).name();
    } catch (IllegalArgumentException | NullPointerException e) {
        detail = detail + getPossibleValues(clz, OsTypeUtils.getAllValues());
        throw new WebApplicationException(
                response(reason, MessageFormat.format(detail, name, clz.getSimpleName())));
    }
}

From source file:eu.smeny.jpapercut.smtp.Processor.java

private void processCommandLine(CommandLine commandLine) {
    String command = commandLine.getCommand();

    CommandEnum commandEnum;//from w ww.  ja v  a2  s  .  co m
    try {
        commandEnum = Enum.valueOf(CommandEnum.class, command);
    } catch (IllegalArgumentException iae) {
        // Command is invalid
        logger.error("Error while retrieving SMTP command " + command, iae);
        commandEnum = CommandEnum.UNKNOWN;
    }

    Handler handler = commandEnum.getHandler();
    handler.setConnection(connection);
    handler.setLine(commandLine);
    handler.execute();
}