Convert int to enum value

Discussion in 'Java' started by Derek Fountain, Nov 26, 2008.

  1. Newbie one: I have an integer that I want to convert into an enum of
    type MyEnum. A cast, like this:

    enum MyEnum { INIT, WORKING, FINAL, COMPLETE };
    MyEnum value = (MyEnum)intValue;

    gives a compile time error. I'd like to use an int of 0 as "INIT", 1 as
    "WORKING" and so on. An exception if the int is not in the enumeration
    would be logical. My enumeration will contain several hundred values and
    this needs to be fast.

    Java 5.0 enums can do some pretty clever stuff - surely it can do this?
    I can't figure out how though... :eek:}

    --
    Aches and pains at your desk? Try a little exercise:
    http://www.daily-exercise.com/
    Derek Fountain, Nov 26, 2008
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Derek Fountain schreef:
    > Newbie one: I have an integer that I want to convert into an enum of
    > type MyEnum. A cast, like this:
    >
    > enum MyEnum { INIT, WORKING, FINAL, COMPLETE };
    > MyEnum value = (MyEnum)intValue;
    >
    > gives a compile time error. I'd like to use an int of 0 as "INIT", 1 as
    > "WORKING" and so on. An exception if the int is not in the enumeration
    > would be logical. My enumeration will contain several hundred values and
    > this needs to be fast.
    >
    > Java 5.0 enums can do some pretty clever stuff - surely it can do this?
    > I can't figure out how though... :eek:}


    Rethink your design. Casting an integer to an enum almost always means
    you are using the concept of enum wrongly. It is not a coincidence that
    Java enums do not allow this. If you really, really, really need it,
    you can add a fromInt(int someInt) to your Enum.

    H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iEYEARECAAYFAkkthPkACgkQBGFP0CTku6MjKACeOcJjkWwC3CNKi7oJvqvR+ysH
    szMAoMQdRUZyyq6Mr5kOWAYSozSI0rHC
    =rnkl
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Nov 26, 2008
    #2
    1. Advertising

  3. Derek Fountain <> wrote:
    > I'd like to use an int of 0 as "INIT", 1 as "WORKING" and so on.


    For the best solution, you'd ask yourself, why you come from
    ints in the first place. Where do the int values come from,
    and why can you not use enums throughout the whole application?

    Now, while I don't see a good use for it, I also don't exclude the
    possibility of a good use.

    To get the conversion principially, you could use this idiom:
    MyEnum.values()[ intValue ]
    but this creates a clone of a privately (in MyEnum) kept array
    each time you call this method, and I'd be really surprised if
    the optimiser was able to optimize that.

    If you need to do this conversion many times, then you may consider
    saving a copy of MyEnum.values() once into a MyEnum[] typed variable
    and use that for indexing.

    If you do a performance comparison, I'd appreciate if you shared
    the results.
    Andreas Leitgeb, Nov 26, 2008
    #3
  4. Derek Fountain

    Roedy Green Guest

    On Wed, 26 Nov 2008 17:06:12 +0000, Derek Fountain
    <> wrote, quoted or indirectly quoted someone
    who said :

    >Newbie one: I have an integer that I want to convert into an enum of
    >type MyEnum. A cast, like this:


    see http://mindprod.com/jgloss/enum.html

    the trick is to index values().
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    "Humanity is conducting an unintended, uncontrolled, globally pervasive experiment
    whose ultimate consequences could be second only to global nuclear war."
    ~ Environment Canada (The Canadian equivalent of the EPA on global warming)
    Roedy Green, Nov 26, 2008
    #4
  5. Derek Fountain

    Mark Space Guest

    rossum wrote:

    > System.out.println(i + " -> " + EnumMap.intValue(i));


    EnumMap probably isn't the bestest name ever, though. I was scanning
    this and thought "Wth? Why isn't he using get(i)?"

    <http://java.sun.com/javase/6/docs/api/java/util/EnumMap.html>

    ;)

    Here's my little attempt.

    class EnumValues<E extends Enum<E>>
    {
    E[] values;

    public EnumValues( Class<E> enumType )
    {
    values = enumType.getEnumConstants();
    }
    public E intToValue( int i ) {
    return values;
    }
    }
    Mark Space, Nov 27, 2008
    #5
  6. Derek Fountain

    Roedy Green Guest

    On Wed, 26 Nov 2008 22:47:45 +0000, rossum <>
    wrote, quoted or indirectly quoted someone who said :

    >As others have pointed out you should be using enums *instead of* ints
    >rather than alongside them. Go through your program and replace every
    >int that represents a member of your enumeration with the relevant
    >enum. Then got hold of a copy of Joshua Bloch's "Effective Java -
    >Second Edition" and read item 30 - "Use enums instead of int
    >constants".


    You can't very well store enums in an SQL database. You need some sort
    of external representation, a string or int. For strings, you can
    build a HashMap lookup to convert them back to enum, and a an instance
    method on the enum constants to cough up a value from the enum
    constructor.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    "Humanity is conducting an unintended, uncontrolled, globally pervasive experiment
    whose ultimate consequences could be second only to global nuclear war."
    ~ Environment Canada (The Canadian equivalent of the EPA on global warming)
    Roedy Green, Nov 27, 2008
    #6
  7. Derek Fountain

    Wojtek Guest

    Roedy Green wrote :
    > On Wed, 26 Nov 2008 22:47:45 +0000, rossum <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> As others have pointed out you should be using enums *instead of* ints
    >> rather than alongside them. Go through your program and replace every
    >> int that represents a member of your enumeration with the relevant
    >> enum. Then got hold of a copy of Joshua Bloch's "Effective Java -
    >> Second Edition" and read item 30 - "Use enums instead of int
    >> constants".

    >
    > You can't very well store enums in an SQL database. You need some sort
    > of external representation, a string or int. For strings, you can
    > build a HashMap lookup to convert them back to enum, and a an instance
    > method on the enum constants to cough up a value from the enum
    > constructor.


    Not a hashmap. Just make a static method within the enum which looks up
    the database value (by scanning through the values) and passes back the
    corresponding enum:

    -----------------------
    enum Type
    {
    PROJECT(1), //
    PERSON(2), //
    EQUIPMENT(3); //

    private char ivTypeID;

    private Type( char typeID )
    {
    ivTypeID = typeID;
    }

    public char getTypeID()
    {
    return ivTypeID;
    }

    /**
    * Returns the Type for a given id number<br><br>
    * Used to find a Type from a Web page field or database column
    */
    public static Type getType( int typeID ) throws EnumNotFoundException
    {
    for (Type type : Type.values())
    if (type.getTypeID() == typeID)
    return type;

    throw new EnumNotFoundException( Logic.class.getName(), typeID );
    }
    }
    -----------------------

    And for text ids you would use the .equals method in the test.

    --
    Wojtek :)
    Wojtek, Nov 27, 2008
    #7
  8. Derek Fountain

    Wojtek Guest

    Wojtek wrote :
    > throw new EnumNotFoundException( Logic.class.getName(), typeID );


    Um, that should be:
    throw new EnumNotFoundException( Type.class.getName(), typeID );

    (stupid copy/paste...)

    --
    Wojtek :)
    Wojtek, Nov 27, 2008
    #8
  9. Derek Fountain

    Arne Vajhøj Guest

    Roedy Green wrote:
    > On Wed, 26 Nov 2008 22:47:45 +0000, rossum <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> As others have pointed out you should be using enums *instead of* ints
    >> rather than alongside them. Go through your program and replace every
    >> int that represents a member of your enumeration with the relevant
    >> enum. Then got hold of a copy of Joshua Bloch's "Effective Java -
    >> Second Edition" and read item 30 - "Use enums instead of int
    >> constants".

    >
    > You can't very well store enums in an SQL database. You need some sort
    > of external representation, a string or int. For strings, you can
    > build a HashMap lookup to convert them back to enum, and a an instance
    > method on the enum constants to cough up a value from the enum
    > constructor.


    Why all that trouble. ENum already have toString and valueOf
    with all the required functionality.

    Arne
    Arne Vajhøj, Nov 27, 2008
    #9
  10. Derek Fountain

    Lew Guest

    Roedy Green wrote:
    > You can't very well store enums in an SQL database. You need some sort
    > of external representation, a string or int. For strings, you can
    > build a HashMap lookup to convert them back to enum, and a an instance
    > method on the enum constants to cough up a value from the enum
    > constructor.


    This is generally a much better strategy than storing ordinals in a database.
    First, the enum will need a lookup for a string value to serve any
    human-involved interaction with the data anyway. Second, ordinals might not
    remain the same in the face of code refactoring, but string representations
    are far less mutable, so serve better in the long run.

    As for string-to-=enum conversion, careful choice of string representations
    would allow use of the automatically generated enum method 'valueOf(String)'.
    If that default implementation isn't suitable, then the Java enum probably
    needs a custom 'fromString()' anyway, in which case one might well use an
    internal HashMap.

    --
    Lew
    Lew, Nov 28, 2008
    #10
  11. Derek Fountain

    Lew Guest

    Wojtek wrote:
    > And for text ids you would use the .equals method in the test.


    Or just use the enum's 'valueOf(String)'.

    --
    Lew
    Lew, Nov 28, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,208
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,629
    Old Wolf
    Jan 20, 2004
  3. Eric Sosman
    Replies:
    3
    Views:
    778
    Mayeul
    Feb 26, 2010
  4. Wojtek
    Replies:
    1
    Views:
    495
  5. Lew
    Replies:
    0
    Views:
    534
Loading...

Share This Page