Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java enum functionality  (Read 2955 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Posted 2005-11-01 03:59:59 »

The one thing that really bothers me is that you cannot do logical operations on enums.

IE:
1  
2  
3  
4  
5  
6  
7  
8  
9  
enum Test
{
   OPTION1,
   OPTION2;
}

[...]
option = OPTION1 | OPTION2;
[...]


The only way I can think of doing this is to create a method and give each option an integer internally so you can compare the options and then return the final enum constant based on the integer logic operations.

If they can represent enums as bit vectors within internal memory then I'm sure they would have no problems allowing internal logical operations on enums.

Anyone else think this should have been done?

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #1 - Posted 2005-11-01 07:09:44 »

oh, I would have thought they would support that feature - after all, thats doable by the existing integer based "enums"  Angry

Offline elias

Senior Devvie





« Reply #2 - Posted 2005-11-01 07:19:50 »

This is what EnumSet is supposed to do.

 - elias

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #3 - Posted 2005-11-01 07:58:56 »

OMG!
A shity wrapper around a crap implementation!
1  
2  
3  
4  
5  
6  
public static <E extends Enum<E>> EnumSet<E> of(E e)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2,  E e3)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2,  E e3,  E e4)
public static <E extends Enum<E>> EnumSet<E> of(E e1,  E e2,  E e3,  E e4,  E e5)
public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest)

Offline princec

« JGO Spiffy Duke »


Medals: 422
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2005-11-01 09:17:59 »

ORing enums is actually a bit of an abuse Wink Very poor programming. Seeing as enums can unexpectedly have a slightly different binary value if someone inserts a new one etc.

Cas Smiley

Offline GKW

Senior Devvie




Revenge is mine!


« Reply #5 - Posted 2005-11-01 16:39:48 »

Not the most useful part of the Java language.  I basically use them for error checking my int flags.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #6 - Posted 2005-11-01 19:42:19 »

Enums are NOT integers.  That is one of the things that is corrected over the C++ concept of enums when the Java version was created.

ORing together two enums simply does not make sense.  If you are thinking of Enums as simple constants then you've got it wrong.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2005-11-01 19:55:56 »

ORing together two enums simply does not make sense. 
Cohen-Sutherland line clipping anyone? </devils_advocate>

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #8 - Posted 2005-11-01 20:20:13 »

ORing together two enums simply does not make sense. 
Cohen-Sutherland line clipping anyone? </devils_advocate>

A specific algorithm isn't relevant to the general concept of an enumeration.

Offline cborders

Junior Devvie





« Reply #9 - Posted 2005-11-01 20:26:25 »

But it makes sense in places that can have multiple states simultainiously.  Java allows this internally!  For example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
addKeyListener(new KeyListener(){
     public void keyPressed(KeyEvent ke)
     {
          switch(ke.getKeyCode())
          {
               case KeyEvent.VK_UP:
                    if(ke.getModifiersEx() == (KeyEvent.SHIFT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK))
                    {
                         doSomething();
                    }
                    break;
          }
     }
});

I understand that these are not enums, but it's the same idea!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #10 - Posted 2005-11-01 21:45:07 »


A specific algorithm isn't relevant to the general concept of an enumeration.

It's relavent to your assertion that OR'ing enums doesn't make sense.

Said algorithm uses region codes, which are ideally represented with enums. The algo requires these region codes to be AND'ed together in the process. It's a well-known and simple example where logic operations on enums makes sense.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #11 - Posted 2005-11-01 23:41:18 »

It's the idea of storing the result of the OR back into an Enum is where it breaks down.  I get what you mean about something being the combination of other things... but the combination can no longer be created as an enum that belongs to the same set as the others, unless of course you include the combination in the original definition of the enumerated type....

e.g.  (psuedocode)  enum LampState { RED, GREEN, YELLOW }  where RED | GREEN = YELLOW...   the result of the operation is still an enum because it is explicitly defined... you can probably get close to this with Java... though it won't be as pretty as just using integer constants.

Is there a better way to do this and keep the type safety?

Offline princec

« JGO Spiffy Duke »


Medals: 422
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #12 - Posted 2005-11-02 09:45:46 »

Yes, a Set.

ORing enums makes no sense whatsoever. The type you want to manage ORing bits is.... ints!

Cas Smiley

Offline Mark Thornton

Senior Devvie





« Reply #13 - Posted 2005-11-02 10:17:02 »

OMG!
A shity wrapper around a crap implementation!
I don't see what your object to EnumSet is. It seems a reasonable approach, which provides the type safety that has always been lacking when using bit operations directly. While there is some performance cost, speed isn't that bad either.
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #14 - Posted 2005-11-02 10:28:49 »

My objection is that method signatures, and the fact that they're not OR'able and Type safe. Since they broke the language with a new keyword (enum) they might have implemented it *properly*.

* Matzon grumbles something about generics being a bad hack too

Offline Mark Thornton

Senior Devvie





« Reply #15 - Posted 2005-11-02 11:26:20 »

Using naked bit operations to represent set operations is a bad hack too, albeit one of long standing.

I concede that the recursive generic declaration takes some understanding, but most people will just gloss over it and accept that it does the right thing. however without generics we have a semi-staticly type checked language which seems intellectually unsatisfactory. A sort of half way house between the fully dynamic languages and a complete static type checking. While some type casts are always likely to be necessary, without generics I find I have just too many of them for my peace of mind.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (23 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (68 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!