Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting an enum by using an int specific to that enum?

Tags:

java

enums

Hey all. So I have a set of enums and a db with integers corresponding to those enums. Something like this, for example:

public static enum Day {
    SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5), FRIDAY(6), SATURDAY(7);

    public final int fId;

    private Day(int id) {
        this.fId = id;
    }
}

I also have a database which only refers to these days by integers, which correspond to their int in the enum set above. What I am looking to do is query a database, which will return an integer, and then set an enumerator to an object based on that integer returned from the db. I could do something like this:

public static Day getDay(int i) {
    switch(i) {
    case 1:
        return Day.SUNDAY;
    case 2: 
        return Day.MONDAY;
    //And so on
    }
}

But for an enum set with over 100 enums inside this doesn't seem very practical.

So is there a way to do this? Again my goal is to simply insert an int value and get back an enumerator without having to create a new method for the many enums in this set. Maybe I should just make this its own class rather than an enumerator, but I was hoping to do it this way. Thanks!

like image 732
JMRboosties Avatar asked Jun 02 '11 21:06

JMRboosties


People also ask

How do I assign an int to an enum?

The correct syntax to use type casting is as follows. Copy YourEnum variableName = (YourEnum)yourInt; The program below shows how we can use the type casting to cast an int to enum in C#. We have cast our integer value to enum constant One .

Can we use integer in enum?

No, we can have only strings as elements in an enumeration.

How do you assign an integer to enum in Java?

We use a static map timeToDeliveryToEnumValuesMapping internally, which handles the mapping of time to deliver to its corresponding enum value. By using a static map and static method, we fetch the enum value corresponding to the time to deliver integer value.

How do I assign one enum to another in Java?

A cast operation is not possible, but you can write a static member function for enum1 that casts enum2 to enum1: public static Enum1 fromEnum2(Enum2 enum2) { ... } By the way, you can assign an ID to every constant of both enums which simplifies the implementation.


1 Answers

Here's a simple implementation that will work:

public static enum Day {
    SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5), FRIDAY(6), SATURDAY(7);

    public final int fId;

    private Day(int id) {
        this.fId = id;
    }

    public static Day forInt(int id) {
        for (Day day : values()) {
            if (day.fId == id) {
                return day;
            }
        }
        throw new IllegalArgumentException("Invalid Day id: " + id);
    }
}

If you know that your enums start counting from 1, you can simply use this instead:

public static Day forInt(int id) {
    return values()[id - 1];
}
like image 171
Bohemian Avatar answered Oct 15 '22 12:10

Bohemian