How to store an enum value as a corresponding integer?

R

Robin Wenger

Internally in a class I would like to store a value as integer value.
From outside of the class this value should be accessible as enumeration.

I am thinking about something like:


pubic class mycontainer {
....
private int myvalue;
public enum weekday { monday, tuesday, wednesday };

public weekday getcurrentweekday() {
return(positioninlist(myvalue)); }

public void setcurrentweekday(weekday) {
myvalue=positioninlist(weekday);
}
}


In the sample above 1 should be assigned to myvalue if monday is set as weekday:

mycontainer.setcurrentweekday(mycontainer.weekday.monday);

However the core function positioninlist() is not available.

How can I achive this otherwise in detail?

Robin
 
E

Eric Sosman

Internally in a class I would like to store a value as integer value.

Why? I suppose that if you have many millions of instances of
the class, and you're using a 64-bit JVM where references take twice
as much memory as ints, and if heap profiling shows that saving four
bytes per mycontainer instance would amount to a useful savings, then
maybe there might be a reason to do this. But unless all those hold
(and you've made actual measurements to see that they do), this smacks
of premature optimization, a.k.a. the root of all evil. Still...
From outside of the class this value should be accessible as enumeration.

I am thinking about something like:


pubic class mycontainer {
....
private int myvalue;
public enum weekday { monday, tuesday, wednesday };

public weekday getcurrentweekday() {
return(positioninlist(myvalue)); }

return weekday.values()[myvalue];

Note that this will create a brand-new array each time it's called
(so an outside caller can't meddle with the internals of the enums).
Since you wouldn't be playing these games unless you expected a very
large number of instances (and, presumably, calls), you might do better
to instantiate one `static final weekday[] VALUES = weekday.values();'
when the mycontainer class is first loaded.
public void setcurrentweekday(weekday) {
myvalue=positioninlist(weekday);

myvalue = weekday.ordinal();

Two observations: First, it will do you good to follow the usual
naming conventions for Java.

class mycontainer -> class MyContainer
public enum weekday -> public enum Weekday // or WeekDay
monday -> MONDAY
getcurrentweekday -> getCurrentWeekday // or ...WeekDay

.... and so on. Your code will be easier for other people to read
(If yOU thInk lEttEr cAsE dOEsn't AffEct rEAdAbIlIty, thAnk AgAIn),
and you'll find it'll also be easier for you yourself to read.

Second, as mentioned above, this whole business is probably a
poor idea unless you've already been backed into a desperate corner.
Don't go into that corner of your own free will.
 
L

Lew

Bad idea. Store it as an 'enum'.

--
Lew
Ceci n'est pas une fenêtre.
..___________.
|###] | [###|
|##/ | *\##|
|#/ * | \#|
|#----|----#|
|| | * ||
|o * | o|
|_____|_____|
|===========|
 
D

Daniele Futtorovic

Internally in a class I would like to store a value as integer value.
From outside of the class this value should be accessible as enumeration.

I am thinking about something like:


pubic class mycontainer {
....
private int myvalue;
public enum weekday { monday, tuesday, wednesday };

public weekday getcurrentweekday() {
return(positioninlist(myvalue)); }

public void setcurrentweekday(weekday) {
myvalue=positioninlist(weekday);
}
}


In the sample above 1 should be assigned to myvalue if monday is set as weekday:

mycontainer.setcurrentweekday(mycontainer.weekday.monday);

However the core function positioninlist() is not available.

How can I achive this otherwise in detail?

Robin

Many things wrong with your approach and your naming, as Eric pointed out.
Not to repeat what he said, please consider that you can also do the
following:

public enum DayOfWeek {

MONDAY(42),
TUESDAY(43),
WEDNESDAY(44),
...

;

final int dayIndex;
DayOfWeek( int dayindex ){
this.dayIndex = dayindex;
}

public int getDayIndex(){
return dayIndex;
}
}
 
L

Lew

Daniele said:
Many things wrong with your approach and your naming, as Eric pointed out.
Not to repeat what he said, please consider that you can also do the following:

public enum DayOfWeek {

MONDAY(42),
TUESDAY(43),
WEDNESDAY(44),
...

;

final int dayIndex;
DayOfWeek( int dayindex ){
this.dayIndex = dayindex;
}

public int getDayIndex(){
return dayIndex;
}
}

Yeah, or use 'ordinal()'

But surely early on the OP immediately rushed to read
http://download.oracle.com/javase/6/docs/api/java/lang/Enum.html
first thing, and thus already knows of this method, of course!

Either way, reliance on a number instead of a typed, compiler-enforced value
carries costs.
 
D

Daniele Futtorovic

L

Lew

Daniele said:
I would generally discourage from using ordinal(). Even if it might fit the
implementation at a particular point in time, chances are its use might have
to be rolled back when the contracts evolves. Isn't there an item on this in
'Effective Java'?

I agree completely, and also aver that the numeric code for an 'enum' instance
is wasted effort.


Daniele said:

This is one of those classic questions where you want to ask the OP why they
think they need the technique about which they're asking, but that you risk
offending their sensibilities if they happen to believe that the newsgroup
exists as their own personal unpaid help desk. But quite often, knowing the
goal can let you steer a hapless querent towards more productive approaches
than the idiosyncratic and safety-reducing idioms they initially favor.
 
S

Sebastian

Am 12.02.2011 23:52, schrieb Lew:
But do see Daniele Futtorovic's comment and link to information why
that's a bad idea before you go haring off with 'ordinal' or even your
original idea.

It's hard to give a good answer not knowing what purpose the OP had in
mind with his original idea. If it's for persisting enums, perhaps it's
worth looking at what Heinz Kabutz has to say about the "Enum Inversion
Problem" at http://www.javaspecialists.eu/archive/Issue113.html

-- Sebastian
 

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

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top