Convert from Enum Ordinal to Enum Type

Convert from enum ordinal to enum type

To convert an ordinal into its enum representation you might want to do this:

ReportTypeEnum value = ReportTypeEnum.values()[ordinal];

Please notice the array bounds.

Note that every call to values() returns a newly cloned array which might impact performance in a negative way. You may want to cache the array if it's going to be called often.

Code example on how to cache values().


This answer was edited to include the feedback given inside the comments

Select enum by ordinal; java

You could simply access the array returned by values() using the randomly generated number as index (which represents the ordinal):

// i.e.: if someRandomNumber = 0, then randomCard will be "As"
SimpleJackCards randomCard = SimpleJackCards.values()[someRandomNumber];

How can I get a enum member by the ordinal value in kotlin?

Just use values() function which will return the Array of enum values and use ordinal as an index

Example

val bOrdinal=3

val yourColor : Color = Color.values()[bOrdinal]

Getting enum value by passing preset ordinal

Add a field to the enum, and a constructor:

public enum OrderStatus {
private Integer codice;

public Integer getCodice() {
return codice;
}

private OrderStatus(Integer codice) {
this.codice = codice;
}

OPEN(0),
DELIVERED(1),
CANCELLED(3),
PARTIALLY(4)
}

and then you can define a method like this:

public static OrderStatus getByCodice(int codice) {
for (OrderStatus tipo : values()) {
if (tipo.codice == codice) {
return tipo;
}
}
throw new IllegalArgumentException("Invalid codice: " + codice);
}

Jooq enum converter uses the ordinal number. How can I switch to use the enum value number instead?

You can of course implement a custom converter from scratch, as you suggested in your own answer. But do note that starting from jOOQ 3.16 and https://github.com/jOOQ/jOOQ/issues/12423, you can simplify that implementation by extending the org.jooq.impl.EnumConverter like this:

class RoleConverter : EnumConverter<Int, Role>(
Int::class.java,
Role::class.java,
Role::value
)

Can I specify ordinal for enum in Java?

You can't set it. It is always the ordinal of the constant definition. See the documentation for Enum.ordinal():

Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap.

And actually - you should not need to. If you want some integer property, define one.

Is it good practice to use ordinal of enum?

TLDR: No, you should not!

If you refer to the javadoc for ordinal method in Enum.java:

Most programmers will have no use for this method. It is
designed for use by sophisticated enum-based data structures, such
as java.util.EnumSet and java.util.EnumMap.

Firstly - read the manual (javadoc in this case).

Secondly - don't write brittle code. The enum values may change in future and your second code example is much more clear and maintainable.

You definitely don't want to create problems for the future if a new enum value is (say) inserted between PARENT and GRANDPARENT.

Is there a .NET construct like Java's enum.ordinal()?

Enums in .NET are just a layer on top of an integral type, in this case your values are int.

Per the docs, you can do a simple conversion to get that value, for example:

let value = int Note.B

Where value in this case would be 11.

To find the ordinal, as you can make use of a built-in function that gives you an array of all the values in order and then find the index of the one you're after. For example:

let getNoteOrdinal (note: Note) =
let values = Enum.GetValues(typeof<Note>)
Array.IndexOf(values, note) + 1

let value = getNoteOrdinal Note.E

Where value in this case would be 3.

You can take this one step further as @ReedCopsey suggests and make a generic function that will give you the ordinal for any enum value:

let getEnumOrdinal (e: 'a when 'a : enum<'b> and 'a : equality) =    
let values = Enum.GetValues(e.GetType())
Array.IndexOf(values, e) + 1


Related Topics



Leave a reply



Submit