Java 1.5 Enums

C

Chas Douglass

I'm interested in using the new Java 1.5 Enum type, but I'm wondering if
it's the best way to represent certain values retrofitting into a current
application.

The problem I'm having is understanding how to create enums "on the fly".
That is, the underlying value is already stored in a database as an
integer.

I'd like to retrieve that integer value and "reconstruct" an appropriate
enum.

Perhaps I'm missing something obvious (it wouldn't be the first time) but
the design of the enum type seems resistant to this simple application, and
probably for good reason.

I don't beleive it's reasonable to store a serialized version of the enum
-- that would break SQL queries on the database.

So is there a reasonable way to implement this, or should I stick with
"static final int" constants.

Thanks.

Chas Douglass
 
J

Jj

The reason that you want to use Enum type is that you can refer to some
"types" by names. That means you already know that you are dealing with, not
to generate then dynamically on the fly. If that's indeed what you want to
do,that means your data are static, then you will not want to store those
data in the database (either from a configuation/resource file).

I don't see any reason why you want to do this :)
 
O

Oscar kind

Chas Douglass said:
I'm interested in using the new Java 1.5 Enum type, but I'm wondering if
it's the best way to represent certain values retrofitting into a current
application.

The problem I'm having is understanding how to create enums "on the fly".
That is, the underlying value is already stored in a database as an
integer. [...]
So is there a reasonable way to implement this, or should I stick with
"static final int" constants.

The latter, but preferably using Integer objects (you can compare them
directly with values from the DB).

If the enumerated values come from the database, they're probably stored
in an unchanging table. Or at least they should be: that way data
integrity for the enumeration is handled by the database, as for everything
else. Also, you can use an extra column with a description or whatever.

Now if possible, you just use some general characteristic (i.e. some flag
set or not) of the rows in that table. But in most real-life situations
this is too convoluted, and you end up using constants refering to the
primary keys.

Enums in this regards are nice, but it's just as easy to use immutable
objects. It's even easier to use the database to get a list of all values,
because you can use the same code as for all other lists. And as an added
bonus it even works when you're using 1.4.
 
T

Tony Morris

Chas Douglass said:
I'm interested in using the new Java 1.5 Enum type, but I'm wondering if
it's the best way to represent certain values retrofitting into a current
application.

The problem I'm having is understanding how to create enums "on the fly".
That is, the underlying value is already stored in a database as an
integer.

I'd like to retrieve that integer value and "reconstruct" an appropriate
enum.

Perhaps I'm missing something obvious (it wouldn't be the first time) but
the design of the enum type seems resistant to this simple application, and
probably for good reason.

I don't beleive it's reasonable to store a serialized version of the enum
-- that would break SQL queries on the database.

So is there a reasonable way to implement this, or should I stick with
"static final int" constants.

Thanks.

Chas Douglass

You want to create an enum and a reverse mapping.
Suppose you have 2 ints {0,1} that mean something {BLACK, WHITE}.

public enum X
{
BLACK(0),
WHITE(1);

private static java.util.Map<Integer, X> m;
private int i;

X(int i)
{
if(X.m == null)
{
X.m = new java.util.HashMap<Integer, X>();
}

X.m.put(i, this);
this.i = i;
}

public int getI()
{
return i;
}

public static X fromInt(int i)
{
return m.get(i);
}
}
 
C

Chas Douglass

Chas Douglass said:
I'm interested in using the new Java 1.5 Enum type, but I'm wondering
if it's the best way to represent certain values retrofitting into a
current application. [snip]
So is there a reasonable way to implement this, or should I stick
with "static final int" constants.

Thanks.

Chas Douglass

You want to create an enum and a reverse mapping.
Suppose you have 2 ints {0,1} that mean something {BLACK, WHITE}.
[snip]

Thanks, that solution was totally non-obvious to me, but it works
perfectly.

Chas Douglass
 
J

Jesper Nordenberg

Chas Douglass said:
Tony Morris said:
You want to create an enum and a reverse mapping.
Suppose you have 2 ints {0,1} that mean something {BLACK, WHITE}.
[snip]

Thanks, that solution was totally non-obvious to me, but it works
perfectly.

You can skip the map and the int by using the ordinal() method, for
example:

MyEnum getEnumFor(int value) {
for (MyEnum v : MyEnum.values())
if (v.ordinal() == value)
return v;
}

Just remember to store value.ordinal() in the database and don't
change the order of the enum constant declarations.

/Jesper Nordenberg
 
T

Tony Morris

Jesper Nordenberg said:
Chas Douglass <[email protected]> wrote in message
Tony Morris said:
You want to create an enum and a reverse mapping.
Suppose you have 2 ints {0,1} that mean something {BLACK, WHITE}.
[snip]

Thanks, that solution was totally non-obvious to me, but it works
perfectly.

You can skip the map and the int by using the ordinal() method, for
example:

MyEnum getEnumFor(int value) {
for (MyEnum v : MyEnum.values())
if (v.ordinal() == value)
return v;
}

Just remember to store value.ordinal() in the database and don't
change the order of the enum constant declarations.

/Jesper Nordenberg

Which incurs a performance overhead (O(n) search time instead of O(1)) and
the other disadvantages that you mention for what advantage?
 
J

Jesper Nordenberg

Tony Morris said:
Which incurs a performance overhead (O(n) search time instead of O(1)) and
the other disadvantages that you mention for what advantage?

You don't have to keep track of an additional int. If search time is
important you could combine ordinal() with a static map like the one
you used in your example.

/Jesper Nordenberg
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top