java.lang.Enum.valueOf()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(5.3k)|赞(0)|评价(0)|浏览(321)

本文整理了Java中java.lang.Enum.valueOf()方法的一些代码示例,展示了Enum.valueOf()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Enum.valueOf()方法的具体详情如下:
包路径:java.lang.Enum
类名称:Enum
方法名:valueOf

Enum.valueOf介绍

[英]Returns the constant with the specified name of the specified enum type.
[中]返回具有指定枚举类型的指定名称的常量。

代码示例

代码示例来源:origin: google/guava

@Override
protected T doForward(String value) {
 return Enum.valueOf(enumClass, value);
}

代码示例来源:origin: hankcs/HanLP

protected E convert(String label)
{
  return Enum.valueOf(enumType, label);
}

代码示例来源:origin: redisson/redisson

/**
   * {@inheritDoc}
   */
  public <T extends Enum<T>> T load(Class<T> type) {
    return Enum.valueOf(type, value);
  }
}

代码示例来源:origin: prestodb/presto

@Override
protected T doForward(String value) {
 return Enum.valueOf(enumClass, value);
}

代码示例来源:origin: springside/springside4

/**
   * String转换为Enum
   */
  public static <T extends Enum<T>> T fromString(Class<T> enumClass, String value) {
    return Enum.valueOf(enumClass, value);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
  public T convert(String source) {
    if (source.isEmpty()) {
      // It's an empty enum identifier: reset the enum value to null.
      return null;
    }
    return (T) Enum.valueOf(this.enumType, source.trim());
  }
}

代码示例来源:origin: jenkinsci/jenkins

/**
   * Parses the {@link TimeScale} from the query parameter.
   */
  public static TimeScale parse(String type) {
    if(type==null)   return TimeScale.MIN;
    return Enum.valueOf(TimeScale.class, type.toUpperCase(Locale.ENGLISH));
  }
}

代码示例来源:origin: hankcs/HanLP

@Override
  public int ordinal(String tag)
  {
    return Enum.valueOf(enumType, tag).ordinal();
  }
}

代码示例来源:origin: hankcs/HanLP

@SuppressWarnings({"unchecked", "rawtypes"})
  public Object createValue(Class type, String value)
  {
    if (Enum.class.isAssignableFrom(type))
    {
      return Enum.valueOf(type, value);
    }
    return null;
  }
};

代码示例来源:origin: jenkinsci/jenkins

public Object convert(Class aClass, Object object) {
    return Enum.valueOf(aClass,object.toString());
  }
}

代码示例来源:origin: redisson/redisson

/**
   * {@inheritDoc}
   */
  @SuppressWarnings("unchecked")
  public <T extends Enum<T>> T load(Class<T> type) {
    return value.getDeclaringClass() == type
        ? (T) value
        : Enum.valueOf(type, value.name());
  }
}

代码示例来源:origin: neo4j/neo4j

private static <E extends Enum<E>> E enumProperty( Class<E> enumClass, String name, E defaultValue )
  {
    try
    {
      return Enum.valueOf( enumClass, System.getProperty( name, defaultValue.name() ) );
    }
    catch ( IllegalArgumentException e )
    {
      return defaultValue;
    }
  }
}

代码示例来源:origin: alibaba/fastjson

@SuppressWarnings({ "unchecked", "rawtypes" })
public Enum<?> scanEnum(Class<?> enumClass, final SymbolTable symbolTable, char serperator) {
  String name = scanSymbolWithSeperator(symbolTable, serperator);
  if (name == null) {
    return null;
  }
  return Enum.valueOf((Class<? extends Enum>) enumClass, name);
}

代码示例来源:origin: apache/incubator-dubbo

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Object readValue(Class<?> c, Object jv) throws IOException {
  if (jv == null) {
    return null;
  }
  Decoder decoder = GlobalDecoderMap.get(c);
  if (decoder != null) {
    return decoder.decode(jv);
  }
  if (c.isEnum()) {
    return Enum.valueOf((Class<Enum>) c, String.valueOf(jv));
  }
  return jv;
}

代码示例来源:origin: apache/incubator-dubbo

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Object readValue(Class<?> c, Object jv) throws IOException {
  if (jv == null) {
    return null;
  }
  Decoder decoder = GlobalDecoderMap.get(c);
  if (decoder != null) {
    return decoder.decode(jv);
  }
  if (c.isEnum()) {
    return Enum.valueOf((Class<Enum>) c, String.valueOf(jv));
  }
  return jv;
}

代码示例来源:origin: apache/incubator-shardingsphere

@SuppressWarnings("unchecked")
private static Collection<? extends Enum> getDatabaseTypes(final String databaseTypes, final Collection<? extends Enum> allDatabaseTypes, final Class<? extends Enum> enumType) {
  if (Strings.isNullOrEmpty(databaseTypes)) {
    return allDatabaseTypes;
  }
  Set<Enum> result = new HashSet<>(allDatabaseTypes.size());
  for (String each : databaseTypes.split(",")) {
    result.add(Enum.valueOf(enumType, each));
  }
  return result;
}

代码示例来源:origin: redisson/redisson

/**
   * {@inheritDoc}
   */
  public <T extends Enum<T>> T load(Class<T> type) {
    if (!enumerationType.represents(type)) {
      throw new IllegalArgumentException(type + " does not represent " + enumerationType);
    }
    return Enum.valueOf(type, value);
  }
}

代码示例来源:origin: apache/incubator-druid

@JsonCreator
 public static CacheExecutorFactory from(String str)
 {
  return Enum.valueOf(CacheExecutorFactory.class, StringUtils.toUpperCase(str));
 }
}

代码示例来源:origin: ctripcorp/apollo

@Override
public <T extends Enum<T>> T getEnumProperty(String key, Class<T> enumType, T defaultValue) {
 try {
  String value = getProperty(key, null);
  if (value != null) {
   return Enum.valueOf(enumType, value);
  }
 } catch (Throwable ex) {
  Tracer.logError(new ApolloConfigException(
    String.format("getEnumProperty for %s failed, return default value %s", key,
      defaultValue), ex));
 }
 return defaultValue;
}

代码示例来源:origin: redisson/redisson

@Override
  public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception {
    int len = in.readInt();
    Class elemCl = FSTUtil.getRealEnumClass(in.getClassForName( in.readStringUTF() ));
    EnumSet enSet = EnumSet.noneOf(elemCl);
    in.registerObject(enSet,streamPosition,serializationInfo, referencee); // IMPORTANT, else tracking double objects will fail
    for (int i = 0; i < len; i++) {
      String val = in.readStringUTF();
      enSet.add(Enum.valueOf(elemCl,val));
    }
    return enSet;
  }
}

相关文章