java.lang.Enum类的使用及代码示例

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

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

Enum介绍

[英]The superclass of all enumerated types. Actual enumeration types inherit from this class, but extending this class does not make a class an enumeration type, since the compiler needs to generate special information for it.
[中]所有枚举类型的超类。实际枚举类型继承自此类,但扩展此类不会使类成为枚举类型,因为编译器需要为其生成特殊信息。

代码示例

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

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

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

@Override
protected String doBackward(T enumValue) {
 return enumValue.name();
}

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

private String convertToString (Enum e) {
  return enumNames ? e.name() : e.toString();
}

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

/**
 * For the given Enum value {@code rank}, returns the value's {@code "EnumClass.name"}, which is
 * used in exception and warning output.
 */
private static String getLockName(Enum<?> rank) {
 return rank.getDeclaringClass().getSimpleName() + "." + rank.name();
}

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

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

代码示例来源: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: redisson/redisson

@Override
  public String toString() {
    return enumeration.toString();
  }
}

代码示例来源:origin: apache/flink

/**
 * Return value matching this enumerated type.
 * Note that the returned value is trimmed by this method.
 * @param name Property name
 * @param defaultValue Value returned if no mapping exists
 * @throws IllegalArgumentException If mapping is illegal for the type
 * provided
 */
public <T extends Enum<T>> T getEnum(String name, T defaultValue) {
 final String val = getTrimmed(name);
 return null == val
  ? defaultValue
  : Enum.valueOf(defaultValue.getDeclaringClass(), val);
}

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

public static EnumValues constructFromToString(MapperConfig<?> config, Class<Enum<?>> enumClass)
{
  Class<? extends Enum<?>> cls = ClassUtil.findEnumType(enumClass);
  Enum<?>[] values = cls.getEnumConstants();
  if (values != null) {
    SerializableString[] textual = new SerializableString[values.length];
    for (Enum<?> en : values) {
      textual[en.ordinal()] = config.compileString(en.toString());
    }
    return new EnumValues(enumClass, textual);
  }
  throw new IllegalArgumentException("Cannot determine enum constants for Class "+enumClass.getName());
}

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

@Override
public Integer convert(Enum<?> source) {
  return source.ordinal();
}

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

private static <V extends Enum<V>> Class<V> inferValueType(Map<?, V> map) {
 if (map instanceof EnumBiMap) {
  return ((EnumBiMap<?, V>) map).valueType;
 }
 checkArgument(!map.isEmpty());
 return map.values().iterator().next().getDeclaringClass();
}

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

/**
 * For the given Enum value {@code rank}, returns the value's {@code "EnumClass.name"}, which is
 * used in exception and warning output.
 */
private static String getLockName(Enum<?> rank) {
 return rank.getDeclaringClass().getSimpleName() + "." + rank.name();
}

代码示例来源:origin: pxb1988/dex2jar

@Override
public String toString() {
  return name == null ? super.toString() : name;
}

代码示例来源:origin: apache/flink

/**
 * Return value matching this enumerated type.
 * Note that the returned value is trimmed by this method.
 * @param name Property name
 * @param defaultValue Value returned if no mapping exists
 * @throws IllegalArgumentException If mapping is illegal for the type
 * provided
 */
public <T extends Enum<T>> T getEnum(String name, T defaultValue) {
  final String val = getTrimmed(name);
  return null == val
      ? defaultValue
      : Enum.valueOf(defaultValue.getDeclaringClass(), val);
}

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

public static EnumValues constructFromToString(MapperConfig<?> config, Class<Enum<?>> enumClass)
{
  Class<? extends Enum<?>> cls = ClassUtil.findEnumType(enumClass);
  Enum<?>[] values = cls.getEnumConstants();
  if (values != null) {
    SerializableString[] textual = new SerializableString[values.length];
    for (Enum<?> en : values) {
      textual[en.ordinal()] = config.compileString(en.toString());
    }
    return new EnumValues(enumClass, textual);
  }
  throw new IllegalArgumentException("Cannot determine enum constants for Class "+enumClass.getName());
}

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

/**
 * 获取e的总频次
 *
 * @param e
 * @return
 */
public int getTotalFrequency(E e)
{
  return total[e.ordinal()];
}

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

protected static String formatFeatureSet(Set<? extends Feature<?>> features) {
  List<String> temp = new ArrayList<>();
  for (Feature<?> feature : features) {
   Object featureAsObject = feature; // to work around bogus JDK warning
   if (featureAsObject instanceof Enum) {
    Enum<?> f = (Enum<?>) featureAsObject;
    temp.add(f.getDeclaringClass().getSimpleName() + "." + feature);
   } else {
    temp.add(feature.toString());
   }
  }
  return temp.toString();
 }
}

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

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

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

@Override
public String convert(Enum<?> source) {
  return source.name();
}

相关文章