tags:

views:

165

answers:

5

Is it possible to design your own Java Type, similar to an extensible enum?

For instance, I have user roles that a certain module uses and then a sub-package provides additional roles.

What would be involved on the JDK side of things?

+4  A: 

Since Enums can't be extended, you have to fake it.

Make a class with a protected constructor.

Then you can create public static final FakeEnum instances in your class.

public class FakeEnum {

    private String name;
    private Object something;

    protected FakeEnum(String name, Object otherParam) {
        this.name = name;
        this.something = otherParam;
    }

    // public getters

    public static final FakeEnum ONE = new FakeEnum("one", null);
    public static final FakeEnum TWO = new FakeEnum("two", null);
    public static final FakeEnum THRE = new FakeEnum("thre", null);
}

And then you can extend it and add some more things to it like so:

public class ExtendedFakeEnum extends FakeEnum {

    public static final FakeEnum EXTENDED_ONE = new FakeEnum("extended_one", null);
    public static final FakeEnum EXTENDED_TWO = new FakeEnum("extended_two", null);

}
jjnguy
I am aware of that, but still don't like that approach.
Ok, I guess the best approach for what I want to do is not hard-code it. I need to notify administrators, I currently do that based on the group they're in (WHERE g.name = 'ADMINISTRATOR'). I think what I will do is provide several filters to find the correct people to notify. Those filters will be configurable via XML or a properties file.
@Walter, unfortunately, I'm not sure if there is a better option. What is wrong with the above method?
jjnguy
A: 

The concept of an extensible enum doesn't make sense. An enum is used to declare statically the entire set of instances that will ever be made for its own type. Allowing it to be extended would make that impossible to ensure.

Mark Peters
I agree, the application would/could throw an IllegalStateException if a type is undefined.
People, a negative vote means you think what I said was wrong or particularly unhelpful. If you think what I said was wrong, leave a comment so I can fix it if it is in fact wrong. We're not doing this for rep, we're doing it for the entire community. A downvote by itself does not help the public at large.
Mark Peters
A: 

Designing your own type in Java is impossible. Anything you need to do can be done using various design patterns.

Max
I believe everything is possible, you just have to think it up, down, left, right, or sideways :). However, it may not be feasible or practical.
@Walter White: Not in this case. You are talking about Java, Java means JVM, JVM means a predefined set of types you can use, as JVM handles the types internally. If you modify JVM - it won't be Java, it will become your own programming language **based** on Java. So I don't really get why did I get a downvote for giving a proper answer to your **main** question.
Max
+ to Max since you're right. But, it would be nice if you mentioned that you could simulate a Type by using a Class, it's clunky but it would work.
CheesePls
@CheesePls: Probably, but since I would never use that technique myself and would rather implement it some prettier way - I did not mention it.
Max
+1  A: 

Ok,

What I will do is write an interface and then several implementations for how to find users to notify in a particular event. The correct implementation will get injected at run-time and then it will do whatever it needs to do to find the correct users. That implementation may simply take arguments to configure the group name to look for and then return a list of users.

I am learning to use interfaces / design by contract more. Most of my development in the past has only ever had a single implementation so I saw this as a moot point and forgot about that tool / means.

Thanks,

Walter

A: 

If you need an "extensible enum" it might be that a dictionary would suit you better, look at java.util.Dictionary<K,V> where K is the keyname (how you would refer to the particular value, and V is the value/object that should be returned by said Key.

I think thats the closest I've ever come to an extensible Enum.

Also, have a look at this question on SO, this might solve it too.

Mauro
java.util.Dictionary is deprecated by java.util.Map.
ooops, i'm not a java developer - was more answering the principal than the language.
Mauro