Java and naming of method to get enum from int

I have an enum, where each enum maps to an integer, which doesn't match with enum's ordinal(). And now I need a method to convert integer to enum. I think have two options:

  1. A new method with unique name, such as (idiomatic?):

    public static MyEnum fromInt(int i) {}
    
  2. Overload of valueOf():

    public static MyEnum valueOf(int i) {}
    

Are both ok, or is one of them clearly bad or good?

Would it be good or bad to provide both? If yes, should they be identical or behave subtly differently, such as behave differently if i is not valid value.

Or is there some 3rd option which is superior to both of these?

I'm looking for a short answer telling how this should be done to conform to established Java coding practices. The enum will be used by others and maintained for a long time. Please answer with some kind of authoritative reference, or strong experience, not purely opinion or feeling of what is better.

Also, this question is not about how to implement this method, it's only about how it should be named, and if the name depends on exact behaviour, to be most consistent with rest of the "Java world".

Jon Skeet
people
quotationmark

Presumably the number means something - so I'd create a method which uses that information. For example:

public enum HttpStatus {
    OK(200),
    ...
    NOT_FOUND(404);

    private int statusCode;

    private HttpStatus(int statusCode) {
        this.statusCode = statusCode;
    }

    public static HttpStatus fromStatusCode(int statusCode) {
        ...
    }
}

Just fromInt doesn't give any indication of the expected meaning of the integer... and it also doesn't allow for alternative int-based lookup later.

people

See more on this question at Stackoverflow