Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
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  
  Type-safe way to access grouped constants  (Read 830 times)
0 Members and 1 Guest are viewing this topic.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Posted 2013-12-29 08:49:17 »

Edit: My solution is pretty useless since as quew8 pointed out you can use a private constructor.

I was wondering how to group ints in a way which would allow me to create a type-safe argument for a constructor/method. At first I didn't think this was possible without using a switch statement with an enum, so I just grouped them in nested classes, but today I was playing with enums (I never really use them much) and found that I could do this. E.g, if you want to group OpenGL data types you can do this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public enum DataType {
   BYTE {
      @Override
      int getType() {
         return GL_BYTE;
      }
   },
   SHORT {
      @Override
      int getType() {
         return GL_SHORT;
      }
     
   } // etc...;

   abstract int getType();
}

If you want to use it as an argument for a method, e.g uselessly returning the type from another method, you can do:

1  
2  
3  
public int returnType(DataType type) {
   return type.getType();
}

And to access it you'd pass in, e.g:

1  
returnType(DataType.BYTE);

Hopefully this could save someone a days headache. Smiley

Why are all OpenGL tutorials written in Brainf**k?
Offline quew8

JGO Coder


Medals: 31



« Reply #1 - Posted 2013-12-29 13:41:46 »

Out of interest, what do you mean that this is type safe? As opposed to what which wasn't?
Offline Roquen
« Reply #2 - Posted 2013-12-29 13:46:02 »

You don't need to have an anon class per enum entry in this case...just pass in the target value to a constructor.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #3 - Posted 2013-12-29 13:54:22 »

Out of interest, what do you mean that this is type safe? As opposed to what which wasn't?

The function
glBindBuffer(int target, int handle)
accepts an int value as the target. You could pass in 6382 which would result in an OpenGL error, as it only accepts a selection of OpenGL values, e.g GL_ARRAY_BUFFER. If you encapsulated that function in a method which only accepts those values (which is what the enum is for), then it's effectively (though not truely) making it type-safe. You cannot pass in a value such as 6382 as it would result in a compile time error.

You don't need to have an anon class per enum entry in this case...just pass in the target value to a constructor.

I thought of doing that but this approach allows you to name the entries whatever you like which is what I wanted to do. EDIT: That would also eliminate the type-safety as you could similarly pass in 343278 to the constructor which would result in a runtime error.

Why are all OpenGL tutorials written in Brainf**k?
Offline quew8

JGO Coder


Medals: 31



« Reply #4 - Posted 2013-12-29 14:00:50 »

Oh I see. Maybe call it GLEnum safe or something rather than type safe to avoid confusion with Java generics.

And what @Roquen means is:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public enum DataType
    BYTE(GL_BYTE),
    SHORT(GL_SHORT);
   
    private Axis(int enum) {
        this.enum = enum;
    }

    private final int enum;
   
    public int getEnum() {
        return enum;
    }
}

You can still name the entries whatever you like.

I think the problem will come when you need to be able to reuse an GLEnum in more than one function (which I'm sure you have to occasionally although I can't think of one now). Also wouldn't work for bit fields. Don't get me wrong I'm not trying to pick holes, just encouraging preemptive problem solving.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #5 - Posted 2013-12-29 14:06:09 »

Enums in java create type safety; not just generics, thus the correct term is type safe.

Doing that would just move the type-safety issue to the enum. The user could just as easily pass in 4233281 or any random int value to the enum constructor.

Why are all OpenGL tutorials written in Brainf**k?
Offline quew8

JGO Coder


Medals: 31



« Reply #6 - Posted 2013-12-29 14:10:16 »

Enums use a private constructor. They are a fixed set of compile time values so the "user" cannot construct different ones without going into the source, changing it and recompiling. If he did that, I don't think he'd have to look far to solve the problem.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #7 - Posted 2013-12-29 14:17:40 »

Oh I can't believe that I've never came across an enum with a private constructor before. Every enum I've encountered before has either had a public constructor or hasn't declared one. My solution is pretty useless then.

Why are all OpenGL tutorials written in Brainf**k?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2013-12-29 14:26:32 »

Every enum I've encountered before has either had a public constructor or hasn't declared one.
enums never have public constructors Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #9 - Posted 2013-12-29 14:29:52 »

Every enum I've encountered before has either had a public constructor or hasn't declared one.
enums never have public constructors Pointing

Oops I meant package-private.

Why are all OpenGL tutorials written in Brainf**k?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline quew8

JGO Coder


Medals: 31



« Reply #10 - Posted 2013-12-29 14:42:45 »

Why does private constructors make your solution useless? It works doesn't it?
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #11 - Posted 2013-12-29 14:51:27 »

It works, but as you pointed out it has limitations in the way that it can't be used for bit fields. It also adds unnecessary code. Therefore it's a nobrainer to use a private constructor.

Why are all OpenGL tutorials written in Brainf**k?
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.

TehJavaDev (33 views)
2014-10-27 03:28:38

TehJavaDev (27 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (59 views)
2014-10-16 15:22:06

Norakomi (48 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
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!