enum and switch case

A

Antoine Junod

Hello group,

Is there a way to simulate the C enum type without Java 1.5 such that
i can use it in a switch case?

Thanks for your reply
-AJ
 
S

Stefan Schulz

Sure, usually i would implement it like this:

class Foo {
private int ord;

public final Foo ONE_CASE = new Foo(0);
public final Foo ANOTHER_CASE= new Foo(1);
// ...
public final Foo MOGRIFY = new Foo(717);

private Foo(int ord) { this.ord = ord; }

public int getOrdinal(){return ord; }
}

Switch it like this:

switch (anEnum.getOrdinal()) {
case ONE.getOrdinal();
//...
}
 
?

=?ISO-8859-15?Q?Tobias_Schr=F6er?=

Stefan said:
Sure, usually i would implement it like this:

class Foo {
private int ord;

public final Foo ONE_CASE = new Foo(0);
public final Foo ANOTHER_CASE= new Foo(1);
// ...
public final Foo MOGRIFY = new Foo(717);

private Foo(int ord) { this.ord = ord; }

public int getOrdinal(){return ord; }
}

Switch it like this:

switch (anEnum.getOrdinal()) {
case ONE.getOrdinal();
//...
}

For me, this results in a compiler error, which says, that case
expressions must be constant expressions (Tested with JDK 1.5.0 and
Eclipse 3.1.2).

I don't know an exact way to do the simulation, only a rough workaround:

- declare a number of int based constants in your class
- provide a static getInstance member for an int value

<code>
public class EnumType {

public static final int VALUE_1 = 1;
public static final int VALUE_2 = 2;
// ... more

public static EnumType getEnumType(int val) {
switch (val) {
case EnumType.VALUE1 : return new EnumType(1);
// ... and so on
default : throw new IllegalArgumentException("Unpupportet type: "
+ val);
}
}

public EnumType(int type) {
// construcor code
}
}
</code>

Use it like follows:

<code>
// ...
int myType = ...
EnumType eType = EnumType.getEnumType(mytype);
</code>

The actual switching is done in the getEnumType member.
This is, as said, a rough workaround, not an exact emulation.

hth,
Tobi
 
J

Jeffrey Schwab

Antoine said:
Is there a way to simulate the C enum type without Java 1.5 such that
i can use it in a switch case?

Use a bunch of integer constants, or google the Typesafe Enum Pattern.
 
O

Oliver Wong

Tobias Schröer said:
For me, this results in a compiler error, which says, that case
expressions must be constant expressions (Tested with JDK 1.5.0 and
Eclipse 3.1.2).

I don't know an exact way to do the simulation, only a rough workaround:

- declare a number of int based constants in your class
- provide a static getInstance member for an int value

<code>
public class EnumType {

public static final int VALUE_1 = 1;
public static final int VALUE_2 = 2;
// ... more

public static EnumType getEnumType(int val) {
switch (val) {
case EnumType.VALUE1 : return new EnumType(1);
// ... and so on
default : throw new IllegalArgumentException("Unpupportet type: " +
val);
}
}

public EnumType(int type) {
// construcor code
}
}
</code>

Use it like follows:

<code>
// ...
int myType = ...
EnumType eType = EnumType.getEnumType(mytype);
</code>

The actual switching is done in the getEnumType member.
This is, as said, a rough workaround, not an exact emulation.

You should make the constructor of EnumType private so that clients cna
only get instances via the factory method, rather than directly invoking the
constructor.

- Oliver
 
O

Oliver Wong

Oliver Wong said:
You should make the constructor of EnumType private so that clients cna
only get instances via the factory method, rather than directly invoking
the constructor.

Sorry, one more thing. Don't create a new instance each time in the
getEnumType method. Instead, always return the same instance, given the same
ordinal index. This will allow your custom workaround to behave more like
1.5's enum class. If you *DO* decide to return a new instance every time,
you should probably override the equals() (and thus the hashcode()) method
so that two different instances with the same ordinal are considered equal.

- Oliver
 
A

Antoine Junod

Tobias Schröer said:
For me, this results in a compiler error, which says, that case
expressions must be constant expressions (Tested with JDK 1.5.0 and
Eclipse 3.1.2).

Same here.
I don't know an exact way to do the simulation, only a rough
workaround:
[snipped code]

It works for me. Thanks to all for your answers.

-AJ
 
?

=?ISO-8859-15?Q?Tobias_Schr=F6er?=

Oliver said:
Sorry, one more thing. Don't create a new instance each time in the
getEnumType method. Instead, always return the same instance, given the
same ordinal index. This will allow your custom workaround to behave
more like 1.5's enum class. If you *DO* decide to return a new instance
every time, you should probably override the equals() (and thus the
hashcode()) method so that two different instances with the same ordinal
are considered equal.

- Oliver
Yes, you're right. The solution above was rather a quick shot :\
 

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,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top