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

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

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

Enum.ordinal介绍

[英]The ordinal of this enumeration constant (its position in the enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this field. It is designed for use by sophisticated enum-based data structures, such as java.util.EnumSet and java.util.EnumMap.
[中]此枚举常量的序号(其在枚举声明中的位置,初始常量的序号为零)。大多数程序员对这个领域毫无用处。它设计用于复杂的基于枚举的数据结构,如java。util。EnumSet和java。util。枚举图。

代码示例

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

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

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

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

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

/**
 * 获取转移频次
 *
 * @param from
 * @param to
 * @return
 */
public int getFrequency(E from, E to)
{
  return matrix[from.ordinal()][to.ordinal()];
}

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

public SerializableString serializedValueFor(Enum<?> key) {
  return _textual[key.ordinal()];
}

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

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

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

private boolean isActuallyE(@Nullable Object o) {
 if (o instanceof Enum) {
  Enum<?> e = (Enum<?>) o;
  int index = e.ordinal();
  return index < enumConstants.length && enumConstants[index] == e;
 }
 return false;
}

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

@Override
  protected void saveValue(EnumItem<E> item, DataOutputStream out) throws IOException
  {
    out.writeInt(item.labelMap.size());
    for (Map.Entry<E, Integer> entry : item.labelMap.entrySet())
    {
      out.writeInt(entry.getKey().ordinal());
      out.writeInt(entry.getValue());
    }
  }
}

代码示例来源:origin: stackoverflow.com

public enum AwesomeEnum {
 Something, Other;
 private static final String name = AwesomeEnum.class.getName();
 public void attachTo(Intent intent) {
  intent.putExtra(name, ordinal());
 }
 public static AwesomeEnum detachFrom(Intent intent) {
  if(!intent.hasExtra(name)) throw new IllegalStateException();
  return values()[intent.getIntExtra(name, -1)];
 }
}

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

private boolean isActuallyE(@NullableDecl Object o) {
 if (o instanceof Enum) {
  Enum<?> e = (Enum<?>) o;
  int index = e.ordinal();
  return index < enumConstants.length && enumConstants[index] == e;
 }
 return false;
}

代码示例来源:origin: stackoverflow.com

public enum Direction {
  NORTH, EAST, SOUTH, WEST;

  private static final Direction[] cachedValues = values();// to avoid recreating array

  public Direction getOppositeDirection() {
    return cachedValues[(ordinal() + 2) % 4];
  }
}

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

private boolean isActuallyE(@NullableDecl Object o) {
 if (o instanceof Enum) {
  Enum<?> e = (Enum<?>) o;
  int index = e.ordinal();
  return index < enumConstants.length && enumConstants[index] == e;
 }
 return false;
}

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

@Override
public int count(@Nullable Object element) {
 if (element == null || !isActuallyE(element)) {
  return 0;
 }
 Enum<?> e = (Enum<?>) element;
 return counts[e.ordinal()];
}

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

@Override
public int count(@NullableDecl Object element) {
 if (element == null || !isActuallyE(element)) {
  return 0;
 }
 Enum<?> e = (Enum<?>) element;
 return counts[e.ordinal()];
}

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

@CanIgnoreReturnValue
@Override
public int setCount(E element, int count) {
 checkIsE(element);
 checkNonnegative(count, "count");
 int index = element.ordinal();
 int oldCount = counts[index];
 counts[index] = count;
 size += count - oldCount;
 if (oldCount == 0 && count > 0) {
  distinctElements++;
 } else if (oldCount > 0 && count == 0) {
  distinctElements--;
 }
 return oldCount;
}

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

@CanIgnoreReturnValue
@Override
public int add(E element, int occurrences) {
 checkIsE(element);
 checkNonnegative(occurrences, "occurrences");
 if (occurrences == 0) {
  return count(element);
 }
 int index = element.ordinal();
 int oldCount = counts[index];
 long newCount = (long) oldCount + occurrences;
 checkArgument(newCount <= Integer.MAX_VALUE, "too many occurrences: %s", newCount);
 counts[index] = (int) newCount;
 if (oldCount == 0) {
  distinctElements++;
 }
 size += occurrences;
 return oldCount;
}

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

@Override
 public void remove() {
  checkRemove(toRemove >= 0);
  if (counts[toRemove] > 0) {
   distinctElements--;
   size -= counts[toRemove];
   counts[toRemove] = 0;
  }
  toRemove = -1;
 }
}

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

@Override
 public void remove() {
  checkRemove(toRemove >= 0);
  if (counts[toRemove] > 0) {
   distinctElements--;
   size -= counts[toRemove];
   counts[toRemove] = 0;
  }
  toRemove = -1;
 }
}

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

@Override
 public void remove() {
  checkRemove(toRemove >= 0);
  if (counts[toRemove] > 0) {
   distinctElements--;
   size -= counts[toRemove];
   counts[toRemove] = 0;
  }
  toRemove = -1;
 }
}

相关文章