Java >> enum and switch case

by Antoine Junod » Sat, 13 May 2006 22:43:15 GMT

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


Java >> enum and switch case

by Stefan Schulz » Sun, 14 May 2006 03:45:19 GMT


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();
//...
}




Java >> enum and switch case

by Tobias Schrr » Sun, 14 May 2006 04:54:11 GMT

Stefan Schulz schrieb:

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


enum and switch case

by Jeffrey Schwab » Sun, 14 May 2006 21:06:56 GMT





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


enum and switch case

by Oliver Wong » Tue, 16 May 2006 04:24:57 GMT





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



enum and switch case

by Oliver Wong » Tue, 16 May 2006 04:27:11 GMT







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



enum and switch case

by Antoine Junod » Tue, 16 May 2006 13:54:46 GMT

Tobias Schrer < XXXX@XXXXX.COM > writes:


Same here.


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

-AJ


enum and switch case

by Tobias Schrr » Tue, 16 May 2006 14:54:30 GMT

Oliver Wong schrieb:




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


Similar Threads

1. [Switch()...case]From String to enum - Java

2. compiler oddity with switch/case & enum ...

I program in C for work (mostly embedded) and over time I adopted 
placing parenthesis around case statement constants to make them easier 
to read in the days before nice editors with syntax coloring (that is, 
"case (...):", where "..." represents the constant).  Anyway, I also use 
Java a lot for writing simulations for testing the embedded software. 
So my writing style doesn't really change rather I am writing in C or 
Java and I've never had a compiler complain about parenthesis in a case 
statement.  That is, until Java introduced enums.  This seems odd to me 
that the compiler would throw an error because the enumerated value in a 
case statement is surrounded by parenthesis and not be concerned if a 
value that is not an enumerated value is surrounded by parenthesis. 
Usually, extra parenthesis (which is what I am introducing for the 
purpose of making the software easier to read) are ignored by compilers 
(as long as they match up, that is, an opening and closing parenthesis 
exist).

Give it a try, frankly I think this a compiler error.  Especially, given 
the errors you get if you try it.  They are:

"an enum switch case label must be the unqualified name of an 
enumeration constant"

and

"duplicate case label"

These errors are more indicative of the error that occurs if you qualify 
the enum value.

Worthy of posting as an error to Sun?

3. enum and switch - Java

4. Enum, switch, and a null

Given the following (bunch of stuff missing for clarity):
---------------------------------------

enum Foo
{
  ONE, TWO;
}

...

switch(getFoo())
{
  default:
  ONE:
    doOne();
    break;

  TWO:
    doTwo();
    break;
}
---------------------------------------

If getFoo() returns a null, then an exception occurs.

Should not the default case be processed? It should be the equivalent 
of a "not found" value, I would think.

-- 
Wojtek :-)


5. switch or select case and code inside it

6. Switch - Case problem

mo wrote:
> Hi,
> 
> I have this code:
> 
> var var1 = eval(Request("Page"));
Warning: tautological use of eval
   var var1 = Request("Page");
suggested.
> Response.Write(">>"+var1);
Warning: unescaped HTML
   Response.Write(">>"+var1);
suggested.

> switch(var1)
> {
> case "Monthly":Response.Write("Monthly<BR>") %>	
Error: unterminated switch statement caused by return to HTML
Move %> to after:
> }
suggested.

<snip>
Hope it helps - you just know I havn't got ASP running :)

Dom



7. Switch as a Select Case

8. Switch..Case Statement Question.

Can someone tell me if the following Switch...Case construct is valid?
I'm wanting to check for multiple values in the Case statement without
explicitly listing each values.
So for example, will case 1-35: work? If this would work, will it
consider all numbers between 1 and 35 inclusive of 1 and 35? Please let
me know if the following will work below:

switch(state){
case 1-35:
case 37:
    do something_1;
    break;
case 36:
    do something_2;
    break;
case 38-50:
    do something_3;
    break;
}


Thank you.
Andy.